mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 19:19:57 +08:00
c673cde7de
* feat: add onCancel and onEnd option for editable (#29615) * feature: add onCancel and onEnd option for editable * doc: editable * doc: add EN doc * optimize: code Co-authored-by: chenliang <chenliang9@xiaomi.com> * feat: add parent class for different notification types (#29634) close #29417 * refactor: Upload use origin behavior (#29737) * refactor: Fallback of events * test: Fix test case * fix: Start file update logic * fix: remove status update * test: Remove wrapTest * test: Fix test case * chore: bump rc-upload * docs: About desc * feat: tab support moreIcon (#29744) * feat: Tabs support moreIcon * docs: Tabs support moreIcon * style: lint * docs: add english document * Update components/tabs/index.zh-CN.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.en-US.md Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> * Update components/tabs/index.zh-CN.md * Update components/tabs/index.en-US.md Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: jueinin <1014397160@qq.com> Co-authored-by: chenliang <chenliang9@xiaomi.com> Co-authored-by: 不吃猫的鱼 <michael2ib1989@gmail.com> Co-authored-by: 二货机器人 <smith3816@gmail.com> Co-authored-by: Tianyuan Zhang <tianyuan233.zhang@gmail.com> Co-authored-by: zty <zty.dev@outlook.com> Co-authored-by: zty <zty.dev@icloud.com> Co-authored-by: xrkffgg <xrkffgg@vip.qq.com>
138 lines
3.2 KiB
TypeScript
138 lines
3.2 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';
|
|
|
|
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;
|
|
}
|
|
|
|
const Editable: React.FC<EditableProps> = ({
|
|
prefixCls,
|
|
'aria-label': ariaLabel,
|
|
className,
|
|
style,
|
|
direction,
|
|
maxLength,
|
|
autoSize = true,
|
|
value,
|
|
onSave,
|
|
onCancel,
|
|
onEnd,
|
|
}) => {
|
|
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}
|
|
autoSize={autoSize}
|
|
/>
|
|
<EnterOutlined className={`${prefixCls}-edit-content-confirm`} />
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Editable;
|