import * as React from 'react' import cls from 'classnames' import { Icon, TouchFeedback } from 'rcm-mobile' import './style/index.scss' import { RadioProps } from 'radio' /** * @author tongxiaokang * @version 0.0.1 */ export default class Input extends React.PureComponent { static defaultProps = { prefixCls: 'x-input', className: '', rows: 2, type: 'text', disabled: false, clearable: true, noValidate: false, maxLength: -1, minLength: -1, requiredMessage: `该字段为必填项`, patternMessage: '该字段格式不正确', minLengthMessage: `该字段最少长度为 {bound},缺少 {delta}`, maxLengthMessage: `该字段最大长度为 {bound},超出 {delta}` } constructor(props: InputProps & T) { super(props) this.state = { isWarn: false, // 是否通过校验 value: props.value || props.defaultValue || '', defaultValue: props.defaultValue } } componentDidMount(): void { this.value = this.state.value || '' } static getDerivedStateFromProps(nextProps: RadioProps, prevState: any) { if ('value' in nextProps && nextProps.value !== prevState.value) { return { value: nextProps.value } } else if ('defaultValue' in nextProps && nextProps.defaultValue !== prevState.defaultValue) { return { value: nextProps.defaultValue, defaultValue: nextProps.defaultValue } } return null } componentDidUpdate() { // 为 textarea 自动计算高度 if (this.props.autoSize && this.props.type === 'textarea') { const textareaDom = this.elem const paddingHeight = parseInt(this.getStyleValue(textareaDom, 'padding-top')) + parseInt(this.getStyleValue(textareaDom, 'padding-bottom')) textareaDom.style.height = '' // fix:行数减少时,高度不发生变化的问题 textareaDom.style.height = `${textareaDom.scrollHeight - paddingHeight}px` } } render() { const { value, isWarn } = this.state const { prefixCls, type, rows, className, disabled, hidden, clearable, onChange, requiredMessage, patternMessage, minLengthMessage, maxLengthMessage, autoSize, defaultValue, ...resetProps } = this.props const classNames = cls(prefixCls, { 'x-disabled': disabled, [`${prefixCls}-warn`]: isWarn }) const props = { ...resetProps, ref: this.refInput, value, className: classNames, disabled, onBlur: this.handleOnBlur, onChange: this.onInputChange } return (