ant-design/components/input/TextArea.tsx

127 lines
3.4 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import ClearableLabeledInput from './ClearableLabeledInput';
import ResizableTextArea, { AutoSizeType } from './ResizableTextArea';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import { fixControlledValue, resolveOnChange } from './Input';
2017-05-22 14:44:58 +08:00
export type HTMLTextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>;
export interface TextAreaProps extends HTMLTextareaProps {
prefixCls?: string;
2019-10-11 18:12:28 +08:00
autoSize?: boolean | AutoSizeType;
onPressEnter?: React.KeyboardEventHandler<HTMLTextAreaElement>;
allowClear?: boolean;
onResize?: (size: { width: number; height: number }) => void;
2017-05-22 14:44:58 +08:00
}
export interface TextAreaState {
value: 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,
};
}
2017-09-17 15:48:44 +08:00
static getDerivedStateFromProps(nextProps: TextAreaProps) {
if ('value' in nextProps) {
return {
value: nextProps.value,
};
}
return null;
2017-05-22 14:44:58 +08:00
}
setValue(value: string, callback?: () => void) {
if (!('value' in this.props)) {
this.setState({ value }, callback);
}
}
focus = () => {
this.resizableTextArea.textArea.focus();
};
blur() {
this.resizableTextArea.textArea.blur();
}
saveTextArea = (resizableTextArea: ResizableTextArea) => {
this.resizableTextArea = 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, () => {
this.resizableTextArea.resizeTextarea();
});
resolveOnChange(this.resizableTextArea.textArea, e, this.props.onChange);
2018-12-07 20:02:01 +08:00
};
2017-05-22 14:44:58 +08:00
2017-11-21 19:51:31 +08:00
handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
2017-05-22 14:44:58 +08:00
const { onPressEnter, onKeyDown } = this.props;
if (e.keyCode === 13 && onPressEnter) {
onPressEnter(e);
}
if (onKeyDown) {
onKeyDown(e);
}
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.resizableTextArea.renderTextArea();
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) => {
return (
<ResizableTextArea
{...this.props}
prefixCls={prefixCls}
onKeyDown={this.handleKeyDown}
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) => {
const { value } = this.state;
const { prefixCls: customizePrefixCls } = this.props;
const prefixCls = getPrefixCls('input', customizePrefixCls);
2017-05-22 14:44:58 +08:00
return (
<ClearableLabeledInput
{...this.props}
prefixCls={prefixCls}
direction={direction}
inputType="text"
value={fixControlledValue(value)}
element={this.renderTextArea(prefixCls)}
handleReset={this.handleReset}
ref={this.saveClearableInput}
triggerFocus={this.focus}
/>
2017-05-22 14:44:58 +08:00
);
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;