ant-design/components/input/TextArea.tsx

174 lines
5.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import RcTextArea, { TextAreaProps as RcTextAreaProps, ResizableTextArea } from 'rc-textarea';
import omit from 'omit.js';
import classNames from 'classnames';
import ClearableLabeledInput from './ClearableLabeledInput';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { fixControlledValue, resolveOnChange } from './Input';
import SizeContext, { SizeType } from '../config-provider/SizeContext';
2017-05-22 14:44:58 +08:00
export interface TextAreaProps extends RcTextAreaProps {
allowClear?: boolean;
bordered?: boolean;
showCount?: boolean;
maxLength?: number;
size?: SizeType;
2017-05-22 14:44:58 +08:00
}
export interface TextAreaState {
value: any;
/** `value` from prev props */
prevValue: any;
}
class TextArea extends React.Component<TextAreaProps, TextAreaState> {
resizableTextArea: ResizableTextArea;
2019-08-05 18:38:10 +08:00
clearableInput: ClearableLabeledInput;
2017-09-17 15:48:44 +08:00
constructor(props: TextAreaProps) {
super(props);
const value = typeof props.value === 'undefined' ? props.defaultValue : props.value;
this.state = {
value,
// eslint-disable-next-line react/no-unused-state
prevValue: props.value,
};
}
2017-09-17 15:48:44 +08:00
static getDerivedStateFromProps(nextProps: TextAreaProps, { prevValue }: TextAreaState) {
const newState: Partial<TextAreaState> = { prevValue: nextProps.value };
if (nextProps.value !== undefined || prevValue !== nextProps.value) {
newState.value = nextProps.value;
}
return newState;
2017-05-22 14:44:58 +08:00
}
setValue(value: string, callback?: () => void) {
if (this.props.value === undefined) {
this.setState({ value }, callback);
}
}
focus = () => {
this.resizableTextArea.textArea.focus();
};
blur() {
this.resizableTextArea.textArea.blur();
}
setSelectionRange(start: number, end: number, direction?: 'forward' | 'backward' | 'none') {
this.resizableTextArea.textArea.setSelectionRange(start, end, direction);
}
saveTextArea = (textarea: RcTextArea) => {
this.resizableTextArea = textarea?.resizableTextArea;
2018-12-07 20:02:01 +08:00
};
2017-05-22 14:44:58 +08:00
saveClearableInput = (clearableInput: ClearableLabeledInput) => {
this.clearableInput = clearableInput;
};
handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
this.setValue(e.target.value);
resolveOnChange(this.resizableTextArea.textArea, e, this.props.onChange);
2018-12-07 20:02:01 +08:00
};
2017-05-22 14:44:58 +08:00
handleReset = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
this.setValue('', () => {
this.focus();
2019-08-05 18:38:10 +08:00
});
resolveOnChange(this.resizableTextArea.textArea, e, this.props.onChange);
2018-12-07 20:02:01 +08:00
};
2017-05-22 14:44:58 +08:00
renderTextArea = (prefixCls: string, bordered: boolean, size?: SizeType) => {
const { showCount, className, style, size: customizeSize } = this.props;
return (
<RcTextArea
{...omit(this.props, ['allowClear', 'bordered', 'showCount', 'size'])}
className={classNames({
[`${prefixCls}-borderless`]: !bordered,
[className!]: className && !showCount,
[`${prefixCls}-sm`]: size === 'small' || customizeSize === 'small',
[`${prefixCls}-lg`]: size === 'large' || customizeSize === 'large',
})}
style={showCount ? null : style}
prefixCls={prefixCls}
onChange={this.handleChange}
ref={this.saveTextArea}
/>
2019-10-11 18:12:28 +08:00
);
};
2019-10-11 18:12:28 +08:00
renderComponent = ({ getPrefixCls, direction }: ConfigConsumerProps) => {
let value = fixControlledValue(this.state?.value);
const {
prefixCls: customizePrefixCls,
bordered = true,
showCount = false,
maxLength,
className,
style,
} = this.props;
const prefixCls = getPrefixCls('input', customizePrefixCls);
// Max length value
const hasMaxLength = Number(maxLength) > 0;
value = hasMaxLength ? value.slice(0, maxLength) : value;
// TextArea
2020-10-21 14:42:10 +08:00
const textareaNode = (size?: SizeType) => (
<ClearableLabeledInput
{...this.props}
prefixCls={prefixCls}
direction={direction}
inputType="text"
value={value}
element={this.renderTextArea(prefixCls, bordered, size)}
handleReset={this.handleReset}
ref={this.saveClearableInput}
triggerFocus={this.focus}
bordered={bordered}
/>
2017-05-22 14:44:58 +08:00
);
// Only show text area wrapper when needed
if (showCount) {
const valueLength = [...value].length;
const dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
2020-10-21 14:42:10 +08:00
return (
<SizeContext.Consumer>
{(size?: SizeType) => (
<div
className={classNames(
`${prefixCls}-textarea`,
{
[`${prefixCls}-textarea-rtl`]: direction === 'rtl',
},
`${prefixCls}-textarea-show-count`,
className,
)}
style={style}
data-count={dataCount}
>
{textareaNode(size)}
</div>
)}
2020-10-21 14:42:10 +08:00
</SizeContext.Consumer>
);
}
return <SizeContext.Consumer>{textareaNode}</SizeContext.Consumer>;
2018-12-07 20:02:01 +08:00
};
render() {
return <ConfigConsumer>{this.renderComponent}</ConfigConsumer>;
}
2017-05-22 14:44:58 +08:00
}
export default TextArea;