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';
|
2020-06-13 14:24:56 +08:00
|
|
|
|
import omit from 'omit.js';
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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-10 11:30:58 +08:00
|
|
|
|
maxLength?: number;
|
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,
|
|
|
|
|
...props
|
|
|
|
|
},
|
|
|
|
|
ref,
|
|
|
|
|
) => {
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
|
|
2020-12-29 22:42:04 +08:00
|
|
|
|
const innerRef = React.useRef<RcTextArea>();
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const clearableInputRef = React.useRef<ClearableLabeledInput>(null);
|
|
|
|
|
|
|
|
|
|
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 prevValue = React.useRef(props.value);
|
2019-08-06 15:29:13 +08:00
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (props.value !== undefined || prevValue.current !== props.value) {
|
|
|
|
|
setValue(props.value);
|
|
|
|
|
prevValue.current = props.value;
|
|
|
|
|
}
|
|
|
|
|
}, [props.value, prevValue.current]);
|
2017-05-22 14:44:58 +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
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
handleSetValue(e.target.value);
|
2020-12-29 22:42:04 +08:00
|
|
|
|
resolveOnChange(innerRef.current as any, e, props.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 handleReset = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
|
|
|
|
|
handleSetValue('', () => {
|
|
|
|
|
innerRef.current?.focus();
|
|
|
|
|
});
|
2020-12-29 22:42:04 +08:00
|
|
|
|
resolveOnChange(innerRef.current as any, e, props.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'])}
|
|
|
|
|
maxLength={maxLength}
|
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
|
|
|
|
})}
|
|
|
|
|
style={showCount ? null : style}
|
2019-11-01 18:19:29 +08:00
|
|
|
|
prefixCls={prefixCls}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
onChange={handleChange}
|
2020-12-29 22:42:04 +08:00
|
|
|
|
ref={innerRef}
|
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
|
|
|
|
|
|
|
|
|
// Max length value
|
2020-10-10 11:30:58 +08:00
|
|
|
|
const hasMaxLength = Number(maxLength) > 0;
|
2020-11-11 00:56:57 +08:00
|
|
|
|
// fix #27612 将value转为数组进行截取,解决 '😂'.length === 2 等emoji表情导致的截取乱码的问题
|
2020-11-18 11:27:58 +08:00
|
|
|
|
val = hasMaxLength ? [...val].slice(0, maxLength).join('') : val;
|
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}
|
|
|
|
|
/>
|
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) {
|
2020-11-18 11:27:58 +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;
|