2022-02-14 17:09:35 +08:00
|
|
|
|
import classNames from 'classnames';
|
2022-05-07 14:31:54 +08:00
|
|
|
|
import type { TextAreaProps as RcTextAreaProps } from 'rc-textarea';
|
|
|
|
|
import RcTextArea from 'rc-textarea';
|
2022-10-04 23:31:32 +08:00
|
|
|
|
import type { ResizableTextAreaRef } from 'rc-textarea/lib/ResizableTextArea';
|
2020-11-18 11:27:58 +08:00
|
|
|
|
import useMergedState from 'rc-util/lib/hooks/useMergedState';
|
2022-02-14 17:09:35 +08:00
|
|
|
|
import omit from 'rc-util/lib/omit';
|
|
|
|
|
import * as React from 'react';
|
2020-11-18 11:27:58 +08:00
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-06-15 14:51:30 +08:00
|
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
2022-05-07 14:31:54 +08:00
|
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
2022-03-24 21:54:20 +08:00
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
2022-05-07 14:31:54 +08:00
|
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
2022-06-15 14:51:30 +08:00
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-02-14 17:09:35 +08:00
|
|
|
|
import ClearableLabeledInput from './ClearableLabeledInput';
|
2022-05-07 14:31:54 +08:00
|
|
|
|
import type { InputFocusOptions } from './Input';
|
|
|
|
|
import { fixControlledValue, resolveOnChange, triggerFocus } from './Input';
|
2017-05-22 14:44:58 +08:00
|
|
|
|
|
2020-12-03 20:33:34 +08:00
|
|
|
|
interface ShowCountProps {
|
2022-08-01 17:39:57 +08:00
|
|
|
|
formatter: (args: { value: string; count: number; maxLength?: number }) => string;
|
2020-12-03 20:33:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
function fixEmojiLength(value: string, maxLength: number) {
|
|
|
|
|
return [...(value || '')].slice(0, maxLength).join('');
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 11:27:52 +08:00
|
|
|
|
function setTriggerValue(
|
|
|
|
|
isCursorInEnd: boolean,
|
|
|
|
|
preValue: string,
|
|
|
|
|
triggerValue: string,
|
|
|
|
|
maxLength: number,
|
|
|
|
|
) {
|
|
|
|
|
let newTriggerValue = triggerValue;
|
|
|
|
|
if (isCursorInEnd) {
|
|
|
|
|
// 光标在尾部,直接截断
|
|
|
|
|
newTriggerValue = fixEmojiLength(triggerValue, maxLength!);
|
|
|
|
|
} else if (
|
|
|
|
|
[...(preValue || '')].length < triggerValue.length &&
|
|
|
|
|
[...(triggerValue || '')].length > maxLength!
|
|
|
|
|
) {
|
|
|
|
|
// 光标在中间,如果最后的值超过最大值,则采用原先的值
|
|
|
|
|
newTriggerValue = preValue;
|
|
|
|
|
}
|
|
|
|
|
return newTriggerValue;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
disabled?: boolean;
|
2022-02-14 17:09:35 +08:00
|
|
|
|
status?: InputStatus;
|
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;
|
2022-10-04 23:31:32 +08:00
|
|
|
|
resizableTextArea?: ResizableTextAreaRef;
|
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,
|
2022-04-29 20:48:10 +08:00
|
|
|
|
disabled: customDisabled,
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionStart,
|
|
|
|
|
onCompositionEnd,
|
|
|
|
|
onChange,
|
2023-03-21 10:42:53 +08:00
|
|
|
|
onFocus,
|
|
|
|
|
onBlur,
|
2022-02-14 17:09:35 +08:00
|
|
|
|
status: customStatus,
|
2020-11-18 11:27:58 +08:00
|
|
|
|
...props
|
|
|
|
|
},
|
|
|
|
|
ref,
|
|
|
|
|
) => {
|
|
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
|
|
|
|
const size = React.useContext(SizeContext);
|
|
|
|
|
|
2022-04-29 20:48:10 +08:00
|
|
|
|
// ===================== Disabled =====================
|
|
|
|
|
const disabled = React.useContext(DisabledContext);
|
2022-09-20 16:48:59 +08:00
|
|
|
|
const mergedDisabled = customDisabled ?? disabled;
|
2022-04-29 20:48:10 +08:00
|
|
|
|
|
2022-03-25 17:48:12 +08:00
|
|
|
|
const {
|
|
|
|
|
status: contextStatus,
|
|
|
|
|
hasFeedback,
|
2022-03-25 19:31:42 +08:00
|
|
|
|
isFormItemInput,
|
2022-03-25 17:48:12 +08:00
|
|
|
|
feedbackIcon,
|
|
|
|
|
} = React.useContext(FormItemInputContext);
|
2022-02-16 11:48:24 +08:00
|
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
2022-02-14 17:09:35 +08:00
|
|
|
|
|
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);
|
2023-03-21 10:42:53 +08:00
|
|
|
|
const [focused, setFocused] = React.useState(false);
|
2022-02-14 11:27:52 +08:00
|
|
|
|
const oldCompositionValueRef = React.useRef<string>();
|
|
|
|
|
const oldSelectionStartRef = React.useRef<number>(0);
|
2021-03-23 15:03:47 +08:00
|
|
|
|
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const [value, setValue] = useMergedState(props.defaultValue, {
|
|
|
|
|
value: props.value,
|
|
|
|
|
});
|
2022-01-15 22:08:19 +08:00
|
|
|
|
const { hidden } = props;
|
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);
|
2022-02-14 11:27:52 +08:00
|
|
|
|
// 拼音输入前保存一份旧值
|
|
|
|
|
oldCompositionValueRef.current = value as string;
|
|
|
|
|
// 保存旧的光标位置
|
|
|
|
|
oldSelectionStartRef.current = e.currentTarget.selectionStart;
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionStart?.(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onInternalCompositionEnd: React.CompositionEventHandler<HTMLTextAreaElement> = e => {
|
|
|
|
|
setCompositing(false);
|
|
|
|
|
|
|
|
|
|
let triggerValue = e.currentTarget.value;
|
|
|
|
|
if (hasMaxLength) {
|
2022-02-14 11:27:52 +08:00
|
|
|
|
const isCursorInEnd =
|
|
|
|
|
oldSelectionStartRef.current >= maxLength! + 1 ||
|
|
|
|
|
oldSelectionStartRef.current === oldCompositionValueRef.current?.length;
|
|
|
|
|
triggerValue = setTriggerValue(
|
|
|
|
|
isCursorInEnd,
|
|
|
|
|
oldCompositionValueRef.current as string,
|
|
|
|
|
triggerValue,
|
|
|
|
|
maxLength!,
|
|
|
|
|
);
|
2021-03-23 15:03:47 +08:00
|
|
|
|
}
|
|
|
|
|
// 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) {
|
2022-02-14 11:27:52 +08:00
|
|
|
|
// 1. 复制粘贴超过maxlength的情况 2.未超过maxlength的情况
|
|
|
|
|
const isCursorInEnd =
|
|
|
|
|
e.target.selectionStart >= maxLength! + 1 ||
|
|
|
|
|
e.target.selectionStart === triggerValue.length ||
|
|
|
|
|
!e.target.selectionStart;
|
|
|
|
|
triggerValue = setTriggerValue(isCursorInEnd, value as string, triggerValue, maxLength!);
|
2021-03-23 15:03:47 +08:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2023-03-21 10:42:53 +08:00
|
|
|
|
const handleBlur = (e: React.FocusEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
setFocused(false);
|
|
|
|
|
onBlur?.(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleFocus = (e: React.FocusEvent<HTMLTextAreaElement>) => {
|
|
|
|
|
setFocused(true);
|
|
|
|
|
onFocus?.(e);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
setFocused((prev) => !mergedDisabled && prev);
|
|
|
|
|
}, [mergedDisabled]);
|
|
|
|
|
|
2021-03-23 15:03:47 +08:00
|
|
|
|
// ============================== Reset ===============================
|
2020-11-18 11:27:58 +08:00
|
|
|
|
const handleReset = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
|
2022-06-15 14:51:30 +08:00
|
|
|
|
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'])}
|
2022-04-29 20:48:10 +08:00
|
|
|
|
disabled={mergedDisabled}
|
2022-02-14 17:09:35 +08:00
|
|
|
|
className={classNames(
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
|
[className!]: className && !showCount,
|
|
|
|
|
[`${prefixCls}-sm`]: size === 'small' || customizeSize === 'small',
|
|
|
|
|
[`${prefixCls}-lg`]: size === 'large' || customizeSize === 'large',
|
|
|
|
|
},
|
|
|
|
|
getStatusClassNames(prefixCls, mergedStatus),
|
|
|
|
|
)}
|
2022-10-06 19:19:16 +08:00
|
|
|
|
style={showCount ? { resize: style?.resize } : 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}
|
2023-03-21 10:42:53 +08:00
|
|
|
|
onBlur={handleBlur}
|
|
|
|
|
onFocus={handleFocus}
|
2021-03-23 15:03:47 +08:00
|
|
|
|
onCompositionEnd={onInternalCompositionEnd}
|
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
|
|
|
|
|
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
|
2022-04-29 20:48:10 +08:00
|
|
|
|
disabled={mergedDisabled}
|
2023-03-21 10:42:53 +08:00
|
|
|
|
focused={focused}
|
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}
|
2022-02-14 17:09:35 +08:00
|
|
|
|
status={customStatus}
|
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
|
2022-02-14 17:09:35 +08:00
|
|
|
|
if (showCount || hasFeedback) {
|
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') {
|
2022-08-01 17:39:57 +08:00
|
|
|
|
dataCount = showCount.formatter({ value: val, count: valueLength, maxLength });
|
2020-12-03 20:33:34 +08:00
|
|
|
|
} 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
|
2022-01-15 22:08:19 +08:00
|
|
|
|
hidden={hidden}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
className={classNames(
|
|
|
|
|
`${prefixCls}-textarea`,
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-textarea-rtl`]: direction === 'rtl',
|
2022-02-14 17:09:35 +08:00
|
|
|
|
[`${prefixCls}-textarea-show-count`]: showCount,
|
2022-03-25 19:31:42 +08:00
|
|
|
|
[`${prefixCls}-textarea-in-form-item`]: isFormItemInput,
|
2020-11-18 11:27:58 +08:00
|
|
|
|
},
|
2022-02-14 17:09:35 +08:00
|
|
|
|
getStatusClassNames(`${prefixCls}-textarea`, mergedStatus, hasFeedback),
|
2020-11-18 11:27:58 +08:00
|
|
|
|
className,
|
2020-10-19 14:03:19 +08:00
|
|
|
|
)}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
style={style}
|
|
|
|
|
data-count={dataCount}
|
|
|
|
|
>
|
|
|
|
|
{textareaNode}
|
2022-03-25 17:48:12 +08:00
|
|
|
|
{hasFeedback && <span className={`${prefixCls}-textarea-suffix`}>{feedbackIcon}</span>}
|
2020-11-18 11:27:58 +08:00
|
|
|
|
</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;
|