From 4664e36fa4f39822e38bf43e280fd41b448a1d22 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Thu, 7 Sep 2017 20:43:21 -0500 Subject: [PATCH] refactor: input string refs to callback (#7489) --- components/cascader/index.tsx | 18 +++++++++--------- components/input/Input.tsx | 16 +++++++++------- components/input/Search.tsx | 13 ++++++++++--- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/components/cascader/index.tsx b/components/cascader/index.tsx index 3b3c163cac..8baab4fa93 100644 --- a/components/cascader/index.tsx +++ b/components/cascader/index.tsx @@ -107,12 +107,8 @@ export default class Cascader extends React.Component { }; cachedOptions: CascaderOptionType[]; - refs: { - [key: string]: any; - input: { - refs: { input: HTMLElement }, - }; - }; + + input: Input; constructor(props) { super(props); @@ -253,6 +249,10 @@ export default class Cascader extends React.Component { return [{ label: notFoundContent, value: 'ANT_CASCADER_NOT_FOUND', disabled: true }]; } + saveInput = (node) => { + this.input = node; + } + render() { const { props, state } = this; const { @@ -321,8 +321,8 @@ export default class Cascader extends React.Component { } // The default value of `matchInputWidth` is `true` const resultListMatchInputWidth = (showSearch as ShowSearchType).matchInputWidth === false ? false : true; - if (resultListMatchInputWidth && state.inputValue && this.refs.input) { - dropdownMenuColumnStyle.width = this.refs.input.refs.input.offsetWidth; + if (resultListMatchInputWidth && state.inputValue && this.input) { + dropdownMenuColumnStyle.width = this.input.input.offsetWidth; } const input = children || ( @@ -335,7 +335,7 @@ export default class Cascader extends React.Component { 0 ? undefined : placeholder} className={`${prefixCls}-input ${sizeCls}`} value={state.inputValue} diff --git a/components/input/Input.tsx b/components/input/Input.tsx index b170172d1e..17a12d24bb 100644 --- a/components/input/Input.tsx +++ b/components/input/Input.tsx @@ -78,9 +78,7 @@ export default class Input extends Component { suffix: PropTypes.node, }; - refs: { - input: HTMLInputElement; - }; + input: HTMLInputElement; handleKeyDown = (e) => { const { onPressEnter, onKeyDown } = this.props; @@ -93,11 +91,11 @@ export default class Input extends Component { } focus() { - this.refs.input.focus(); + this.input.focus(); } blur() { - this.refs.input.blur(); + this.input.blur(); } getInputClassName() { @@ -109,6 +107,10 @@ export default class Input extends Component { }); } + saveInput = (node) => { + this.input = node; + } + renderLabeledInput(children) { const props = this.props; // Not wrap when there is not addons @@ -212,14 +214,14 @@ export default class Input extends Component { {...otherProps} className={classNames(this.getInputClassName(), className)} onKeyDown={this.handleKeyDown} - ref="input" + ref={this.saveInput} />, ); } render() { if (this.props.type === 'textarea') { - return