2017-11-17 14:38:54 +08:00
|
|
|
|
import * as React from 'react';
|
2020-11-18 11:27:58 +08:00
|
|
|
|
import RcTextArea, { TextAreaProps as RcTextAreaProps } from 'rc-textarea';
|
2020-12-29 22:42:04 +08:00
|
|
|
|
import ResizableTextArea from 'rc-textarea/lib/ResizableTextArea';
|
2021-01-13 21:00:30 +08:00
|
|
|
|
import omit from 'rc-util/lib/omit';
|
2020-07-16 00:25:47 +08:00
|
|
|
|
import classNames from 'classnames';
|
2020-11-18 11:27:58 +08:00
|
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2019-11-01 18:19:29 +08:00
|
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
2020-11-18 11:27:58 +08:00
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2020-12-29 22:42:04 +08:00
|
|
|
|
import { fixControlledValue, resolveOnChange, triggerFocus, InputFocusOptions } from './Input';
|
2020-10-21 14:31:16 +08:00
|
|
|
|
import SizeContext, { SizeType } from '../config-provider/SizeContext';
|
2017-05-22 14:44:58 +08:00
|
|
|
|
|
2020-12-03 20:33:34 +08:00
|
|
|
|
interface ShowCountProps {
|
|
|
|
|
formatter: (args: { count: number; maxLength?: number }) => string;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
function fixEmojiLength(value: string, maxLength: number) {
|
|
|
|
|
return [...(value || '')].slice(0, maxLength).join('');
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 14:24:56 +08:00
|
|
|
|
export interface TextAreaProps extends RcTextAreaProps {
|
2019-11-01 18:19:29 +08:00
|
|
|
|
allowClear?: boolean;
|
2020-07-16 00:25:47 +08:00
|
|
|
|
bordered?: boolean;
|
2020-12-03 20:33:34 +08:00
|
|
|
|
showCount?: boolean | ShowCountProps;
|
2020-10-12 20:39:51 +08:00
|
|
|
|
size?: SizeType;
|
2017-05-22 14:44:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:42:04 +08:00
|
|
|
|
export interface TextAreaRef {
|
|
|
|
|
focus: (options?: InputFocusOptions) => void;
|
|
|
|
|
blur: () => void;
|
|
|
|
|
resizableTextArea?: ResizableTextArea;
|
2017-11-01 11:47:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
bordered = true,
|
|
|
|
|
showCount = false,
|
|
|
|
|
maxLength,
|
|
|
|
|
className,
|
|
|
|
|
style,
|
|
|
|
|
size: customizeSize,
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionStart,
|
|
|
|
|
onCompositionEnd,
|
|
|
|
|
onChange,
|
2020-11-18 11:27:58 +08:00
|
|
|
|
...props
|
|
|
|
|
},
|
|
|
|
|
ref,
|
|
|
|
|
) => {
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
|
|
2021-01-13 21:00:30 +08:00
|
|
|
|
const innerRef = React.useRef<RcTextArea>(null);
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const clearableInputRef = React.useRef<ClearableLabeledInput>(null);
|
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
const [compositing, setCompositing] = React.useState(false);
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const [value, setValue] = useMergedState(props.defaultValue, {
|
|
|
|
|
value: props.value,
|
|
|
|
|
});
|
2019-11-01 18:19:29 +08:00
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const handleSetValue = (val: string, callback?: () => void) => {
|
|
|
|
|
if (props.value === undefined) {
|
|
|
|
|
setValue(val);
|
|
|
|
|
callback?.();
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-11-01 18:19:29 +08:00
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
// =========================== Value Update ===========================
|
|
|
|
|
// Max length value
|
|
|
|
|
const hasMaxLength = Number(maxLength) > 0;
|
|
|
|
|
|
|
|
|
|
const onInternalCompositionStart: React.CompositionEventHandler<HTMLTextAreaElement> = e => {
|
|
|
|
|
setCompositing(true);
|
|
|
|
|
onCompositionStart?.(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onInternalCompositionEnd: React.CompositionEventHandler<HTMLTextAreaElement> = e => {
|
|
|
|
|
setCompositing(false);
|
|
|
|
|
|
|
|
|
|
let triggerValue = e.currentTarget.value;
|
|
|
|
|
if (hasMaxLength) {
|
|
|
|
|
triggerValue = fixEmojiLength(triggerValue, maxLength!);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Patch composition onChange when value changed
|
|
|
|
|
if (triggerValue !== value) {
|
|
|
|
|
handleSetValue(triggerValue);
|
2021-04-13 15:42:43 +08:00
|
|
|
|
resolveOnChange(e.currentTarget, e, onChange, triggerValue);
|
2021-03-23 15:03:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onCompositionEnd?.(e);
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
2021-03-23 15:03:47 +08:00
|
|
|
|
let triggerValue = e.target.value;
|
|
|
|
|
if (!compositing && hasMaxLength) {
|
|
|
|
|
triggerValue = fixEmojiLength(triggerValue, maxLength!);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleSetValue(triggerValue);
|
2021-04-13 15:42:43 +08:00
|
|
|
|
resolveOnChange(e.currentTarget, e, onChange, triggerValue);
|
2020-11-18 11:27:58 +08:00
|
|
|
|
};
|
2017-05-22 14:44:58 +08:00
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
// ============================== Reset ===============================
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const handleReset = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
|
|
|
|
|
handleSetValue('', () => {
|
|
|
|
|
innerRef.current?.focus();
|
|
|
|
|
});
|
2021-04-13 15:42:43 +08:00
|
|
|
|
resolveOnChange(innerRef.current?.resizableTextArea?.textArea!, e, onChange);
|
2020-11-18 11:27:58 +08:00
|
|
|
|
};
|
2017-05-22 14:44:58 +08:00
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const prefixCls = getPrefixCls('input', customizePrefixCls);
|
2020-10-19 14:03:19 +08:00
|
|
|
|
|
2020-12-29 22:42:04 +08:00
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
|
|
|
|
resizableTextArea: innerRef.current?.resizableTextArea,
|
|
|
|
|
focus: (option?: InputFocusOptions) => {
|
|
|
|
|
triggerFocus(innerRef.current?.resizableTextArea?.textArea, option);
|
|
|
|
|
},
|
|
|
|
|
blur: () => innerRef.current?.blur(),
|
|
|
|
|
}));
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const textArea = (
|
2020-06-13 14:24:56 +08:00
|
|
|
|
<RcTextArea
|
2020-11-18 11:27:58 +08:00
|
|
|
|
{...omit(props, ['allowClear'])}
|
2020-10-19 14:03:19 +08:00
|
|
|
|
className={classNames({
|
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
|
[className!]: className && !showCount,
|
2020-10-21 14:31:16 +08:00
|
|
|
|
[`${prefixCls}-sm`]: size === 'small' || customizeSize === 'small',
|
|
|
|
|
[`${prefixCls}-lg`]: size === 'large' || customizeSize === 'large',
|
2020-10-19 14:03:19 +08:00
|
|
|
|
})}
|
2021-01-13 21:00:30 +08:00
|
|
|
|
style={showCount ? undefined : style}
|
2019-11-01 18:19:29 +08:00
|
|
|
|
prefixCls={prefixCls}
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionStart={onInternalCompositionStart}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
onChange={handleChange}
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionEnd={onInternalCompositionEnd}
|
2020-12-29 22:42:04 +08:00
|
|
|
|
ref={innerRef}
|
2021-10-13 11:55:55 +08:00
|
|
|
|
maxLength={maxLength}
|
2019-11-01 18:19:29 +08:00
|
|
|
|
/>
|
2019-10-11 18:12:28 +08:00
|
|
|
|
);
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
let val = fixControlledValue(value) as string;
|
2020-10-19 14:03:19 +08:00
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
if (!compositing && hasMaxLength && (props.value === null || props.value === undefined)) {
|
|
|
|
|
// fix #27612 将value转为数组进行截取,解决 '😂'.length === 2 等emoji表情导致的截取乱码的问题
|
|
|
|
|
val = fixEmojiLength(val, maxLength!);
|
|
|
|
|
}
|
2020-10-10 11:30:58 +08:00
|
|
|
|
|
2020-10-19 14:03:19 +08:00
|
|
|
|
// TextArea
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const textareaNode = (
|
2020-10-19 14:03:19 +08:00
|
|
|
|
<ClearableLabeledInput
|
2020-11-18 11:27:58 +08:00
|
|
|
|
{...props}
|
2020-10-19 14:03:19 +08:00
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
|
direction={direction}
|
|
|
|
|
inputType="text"
|
2020-11-18 11:27:58 +08:00
|
|
|
|
value={val}
|
|
|
|
|
element={textArea}
|
|
|
|
|
handleReset={handleReset}
|
|
|
|
|
ref={clearableInputRef}
|
2020-10-19 14:03:19 +08:00
|
|
|
|
bordered={bordered}
|
2021-07-13 21:38:19 +08:00
|
|
|
|
style={showCount ? undefined : style}
|
2020-10-19 14:03:19 +08:00
|
|
|
|
/>
|
2017-05-22 14:44:58 +08:00
|
|
|
|
);
|
2020-10-19 14:03:19 +08:00
|
|
|
|
|
|
|
|
|
// Only show text area wrapper when needed
|
|
|
|
|
if (showCount) {
|
2021-03-23 15:03:47 +08:00
|
|
|
|
const valueLength = [...val].length;
|
2020-12-03 20:33:34 +08:00
|
|
|
|
|
|
|
|
|
let dataCount = '';
|
|
|
|
|
if (typeof showCount === 'object') {
|
|
|
|
|
dataCount = showCount.formatter({ count: valueLength, maxLength });
|
|
|
|
|
} else {
|
|
|
|
|
dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
|
|
|
|
|
}
|
2020-10-19 14:03:19 +08:00
|
|
|
|
|
2020-10-21 14:42:10 +08:00
|
|
|
|
return (
|
2020-11-18 11:27:58 +08:00
|
|
|
|
<div
|
|
|
|
|
className={classNames(
|
|
|
|
|
`${prefixCls}-textarea`,
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-textarea-rtl`]: direction === 'rtl',
|
|
|
|
|
},
|
|
|
|
|
`${prefixCls}-textarea-show-count`,
|
|
|
|
|
className,
|
2020-10-19 14:03:19 +08:00
|
|
|
|
)}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
style={style}
|
|
|
|
|
data-count={dataCount}
|
|
|
|
|
>
|
|
|
|
|
{textareaNode}
|
|
|
|
|
</div>
|
2020-10-19 14:03:19 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
return textareaNode;
|
|
|
|
|
},
|
|
|
|
|
);
|
2018-11-27 13:46:42 +08:00
|
|
|
|
|
|
|
|
|
export default TextArea;
|