2023-01-11 14:18:13 +08:00
|
|
|
import type { TextAreaProps as RcTextAreaProps } from 'rc-textarea/lib/interface';
|
|
|
|
import type { TextAreaRef as RcTextAreaRef } from 'rc-textarea';
|
|
|
|
import { forwardRef } from 'react';
|
2022-02-14 17:09:35 +08:00
|
|
|
import * as React from 'react';
|
2023-01-11 14:18:13 +08:00
|
|
|
import RcTextArea from 'rc-textarea';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import CloseCircleFilled from '@ant-design/icons/CloseCircleFilled';
|
|
|
|
import type { BaseInputProps } from 'rc-input/lib/interface';
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
|
|
|
import useStyle from './style';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { SizeType } from '../config-provider/SizeContext';
|
|
|
|
import SizeContext from '../config-provider/SizeContext';
|
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
2022-06-15 14:51:30 +08:00
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { InputFocusOptions } from './Input';
|
2023-01-11 14:18:13 +08:00
|
|
|
import { triggerFocus } from './Input';
|
|
|
|
import DisabledContext from '../config-provider/DisabledContext';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-02-14 11:27:52 +08:00
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
export interface TextAreaProps extends Omit<RcTextAreaProps, 'suffix'> {
|
2020-07-16 00:25:47 +08:00
|
|
|
bordered?: boolean;
|
2020-10-12 20:39:51 +08:00
|
|
|
size?: SizeType;
|
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;
|
2023-01-11 14:18:13 +08:00
|
|
|
resizableTextArea?: RcTextAreaRef['resizableTextArea'];
|
2017-11-01 11:47:22 +08:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
|
2020-11-18 11:27:58 +08:00
|
|
|
(
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
bordered = true,
|
|
|
|
size: customizeSize,
|
2022-04-29 20:48:10 +08:00
|
|
|
disabled: customDisabled,
|
2022-02-14 17:09:35 +08:00
|
|
|
status: customStatus,
|
2023-01-11 14:18:13 +08:00
|
|
|
allowClear,
|
|
|
|
...rest
|
2020-11-18 11:27:58 +08:00
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
2022-04-06 21:49:30 +08:00
|
|
|
const { getPrefixCls, direction } = React.useContext(ConfigContext);
|
2023-01-11 14:18:13 +08:00
|
|
|
|
|
|
|
// ===================== Size =====================
|
2020-11-18 11:27:58 +08:00
|
|
|
const size = React.useContext(SizeContext);
|
2023-01-11 14:18:13 +08:00
|
|
|
const mergedSize = customizeSize || size;
|
2020-11-18 11:27:58 +08:00
|
|
|
|
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
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
// ===================== Status =====================
|
2022-03-25 17:48:12 +08:00
|
|
|
const {
|
|
|
|
status: contextStatus,
|
|
|
|
hasFeedback,
|
|
|
|
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
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
// ===================== Ref =====================
|
|
|
|
const innerRef = React.useRef<RcTextAreaRef>(null);
|
2022-03-14 20:57:01 +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(),
|
|
|
|
}));
|
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
const prefixCls = getPrefixCls('input', customizePrefixCls);
|
2020-10-19 14:03:19 +08:00
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
// Allow clear
|
|
|
|
let mergedAllowClear: BaseInputProps['allowClear'];
|
|
|
|
if (typeof allowClear === 'object' && allowClear?.clearIcon) {
|
|
|
|
mergedAllowClear = allowClear;
|
|
|
|
} else if (allowClear) {
|
|
|
|
mergedAllowClear = { clearIcon: <CloseCircleFilled /> };
|
2021-03-23 15:03:47 +08:00
|
|
|
}
|
2020-10-10 11:30:58 +08:00
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
// ===================== Style =====================
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2020-10-19 14:03:19 +08:00
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
return wrapSSR(
|
|
|
|
<RcTextArea
|
|
|
|
{...rest}
|
|
|
|
disabled={mergedDisabled}
|
|
|
|
allowClear={mergedAllowClear}
|
|
|
|
classes={{
|
|
|
|
affixWrapper: classNames(
|
|
|
|
`${prefixCls}-textarea-affix-wrapper`,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-affix-wrapper-rtl`]: direction === 'rtl',
|
|
|
|
[`${prefixCls}-affix-wrapper-borderless`]: !bordered,
|
|
|
|
[`${prefixCls}-affix-wrapper-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-affix-wrapper-lg`]: mergedSize === 'large',
|
|
|
|
},
|
|
|
|
getStatusClassNames(`${prefixCls}-affix-wrapper`, mergedStatus),
|
|
|
|
hashId,
|
|
|
|
),
|
|
|
|
countWrapper: classNames(
|
2020-11-18 11:27:58 +08:00
|
|
|
`${prefixCls}-textarea`,
|
2023-01-11 14:18:13 +08:00
|
|
|
`${prefixCls}-textarea-show-count`,
|
|
|
|
hashId,
|
|
|
|
),
|
|
|
|
textarea: classNames(
|
2020-11-18 11:27:58 +08:00
|
|
|
{
|
2023-01-11 14:18:13 +08:00
|
|
|
[`${prefixCls}-borderless`]: !bordered,
|
|
|
|
[`${prefixCls}-sm`]: mergedSize === 'small',
|
|
|
|
[`${prefixCls}-lg`]: mergedSize === 'large',
|
2020-11-18 11:27:58 +08:00
|
|
|
},
|
2023-01-11 14:18:13 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus),
|
2022-03-14 20:57:01 +08:00
|
|
|
hashId,
|
2023-01-11 14:18:13 +08:00
|
|
|
),
|
|
|
|
}}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
suffix={
|
|
|
|
hasFeedback && <span className={`${prefixCls}-textarea-suffix`}>{feedbackIcon}</span>
|
|
|
|
}
|
|
|
|
ref={innerRef}
|
|
|
|
/>,
|
|
|
|
);
|
2020-11-18 11:27:58 +08:00
|
|
|
},
|
|
|
|
);
|
2018-11-27 13:46:42 +08:00
|
|
|
|
|
|
|
export default TextArea;
|