ant-design/components/typography/Editable.tsx
二货机器人 395c549049
fix: Typography error on context ellipsis (#33725)
* chore: init measure

* chore: out of space

* refactor: Multiple render

* chore: auto cut

* feat: render split

* fix: ellipsis logic of suffix

* fix: ref missing

* fix: Tooltip missing

* test: snapshot

* chore: opt for textarea

* test: back part of ellipsis

* chore: back of ellipsis logic

* ellipsis logic

* fix: init ellipsis measure

* fix: ellipsis event

* chore: clean up

* test: Update snapshot

* fix: test

* test: Update snapshot

* chore: lazy ellipsis

* fix: check css ellipsis logic

* test: Update snapshot

* test: back of coverage

* chore: clean up

* test: ignore else

* test: clean up
2022-01-18 14:13:41 +08:00

144 lines
3.4 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import KeyCode from 'rc-util/lib/KeyCode';
import EnterOutlined from '@ant-design/icons/EnterOutlined';
import { AutoSizeType } from 'rc-textarea/lib/ResizableTextArea';
import TextArea from '../input/TextArea';
import { DirectionType } from '../config-provider';
import { cloneElement } from '../_util/reactNode';
interface EditableProps {
prefixCls?: string;
value: string;
['aria-label']?: string;
onSave: (value: string) => void;
onCancel: () => void;
onEnd?: () => void;
className?: string;
style?: React.CSSProperties;
direction?: DirectionType;
maxLength?: number;
autoSize?: boolean | AutoSizeType;
enterIcon?: React.ReactNode;
}
const Editable: React.FC<EditableProps> = ({
prefixCls,
'aria-label': ariaLabel,
className,
style,
direction,
maxLength,
autoSize = true,
value,
onSave,
onCancel,
onEnd,
enterIcon = <EnterOutlined />,
}) => {
const ref = React.useRef<any>();
const inComposition = React.useRef(false);
const lastKeyCode = React.useRef<number>();
const [current, setCurrent] = React.useState(value);
React.useEffect(() => {
setCurrent(value);
}, [value]);
React.useEffect(() => {
if (ref.current && ref.current.resizableTextArea) {
const { textArea } = ref.current.resizableTextArea;
textArea.focus();
const { length } = textArea.value;
textArea.setSelectionRange(length, length);
}
}, []);
const onChange: React.ChangeEventHandler<HTMLTextAreaElement> = ({ target }) => {
setCurrent(target.value.replace(/[\n\r]/g, ''));
};
const onCompositionStart = () => {
inComposition.current = true;
};
const onCompositionEnd = () => {
inComposition.current = false;
};
const onKeyDown: React.KeyboardEventHandler<HTMLTextAreaElement> = ({ keyCode }) => {
// We don't record keyCode when IME is using
if (inComposition.current) return;
lastKeyCode.current = keyCode;
};
const confirmChange = () => {
onSave(current.trim());
};
const onKeyUp: React.KeyboardEventHandler<HTMLTextAreaElement> = ({
keyCode,
ctrlKey,
altKey,
metaKey,
shiftKey,
}) => {
// Check if it's a real key
if (
lastKeyCode.current === keyCode &&
!inComposition.current &&
!ctrlKey &&
!altKey &&
!metaKey &&
!shiftKey
) {
if (keyCode === KeyCode.ENTER) {
confirmChange();
onEnd?.();
} else if (keyCode === KeyCode.ESC) {
onCancel();
}
}
};
const onBlur: React.FocusEventHandler<HTMLTextAreaElement> = () => {
confirmChange();
};
const textAreaClassName = classNames(
prefixCls,
`${prefixCls}-edit-content`,
{
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
return (
<div className={textAreaClassName} style={style}>
<TextArea
ref={ref as any}
maxLength={maxLength}
value={current}
onChange={onChange}
onKeyDown={onKeyDown}
onKeyUp={onKeyUp}
onCompositionStart={onCompositionStart}
onCompositionEnd={onCompositionEnd}
onBlur={onBlur}
aria-label={ariaLabel}
rows={1}
autoSize={autoSize}
/>
{enterIcon !== null
? cloneElement(enterIcon, { className: `${prefixCls}-edit-content-confirm` })
: null}
</div>
);
};
export default Editable;