fix defaultValue

This commit is contained in:
afc163 2018-12-26 22:47:55 +08:00 committed by 偏右
parent d61ad7445a
commit f10cb764e4

View File

@ -2,6 +2,7 @@ import * as React from 'react';
import * as PropTypes from 'prop-types'; import * as PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import omit from 'omit.js'; import omit from 'omit.js';
import { polyfill } from 'react-lifecycles-compat';
import Group from './Group'; import Group from './Group';
import Search from './Search'; import Search from './Search';
import TextArea from './TextArea'; import TextArea from './TextArea';
@ -31,7 +32,7 @@ export interface InputProps
allowClear?: Boolean; allowClear?: Boolean;
} }
export default class Input extends React.Component<InputProps, any> { class Input extends React.Component<InputProps, any> {
static Group: typeof Group; static Group: typeof Group;
static Search: typeof Search; static Search: typeof Search;
static TextArea: typeof TextArea; static TextArea: typeof TextArea;
@ -40,7 +41,6 @@ export default class Input extends React.Component<InputProps, any> {
static defaultProps = { static defaultProps = {
type: 'text', type: 'text',
disabled: false, disabled: false,
allowClear: false,
}; };
static propTypes = { static propTypes = {
@ -65,12 +65,26 @@ export default class Input extends React.Component<InputProps, any> {
allowClear: PropTypes.bool, allowClear: PropTypes.bool,
}; };
state = { static getDerivedStateFromProps(nextProps: InputProps, state: any) {
value: '', if ('value' in nextProps) {
}; return {
...state,
value: nextProps.value,
};
}
return null;
}
input: HTMLInputElement; input: HTMLInputElement;
constructor(props: InputProps) {
super(props);
const value = props.value || props.defaultValue;
this.state = {
value,
};
}
handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
const { onPressEnter, onKeyDown } = this.props; const { onPressEnter, onKeyDown } = this.props;
if (e.keyCode === 13 && onPressEnter) { if (e.keyCode === 13 && onPressEnter) {
@ -273,3 +287,7 @@ export default class Input extends React.Component<InputProps, any> {
return <ConfigConsumer>{this.renderComponent}</ConfigConsumer>; return <ConfigConsumer>{this.renderComponent}</ConfigConsumer>;
} }
} }
polyfill(Input);
export default Input;