From 36046592fa942c406cef709ea91d458add0a5252 Mon Sep 17 00:00:00 2001 From: Avior Date: Wed, 28 Sep 2022 17:11:21 +0200 Subject: [PATCH] fix: choice not respected on startup Signed-off-by: Avior --- src/Input/index.tsx | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Input/index.tsx b/src/Input/index.tsx index 2624042..acb2d59 100644 --- a/src/Input/index.tsx +++ b/src/Input/index.tsx @@ -6,7 +6,7 @@ import { Icon } from '../interfaces' import { buildClassName } from '../Util' import css from './Input.module.styl' import Menu from '../Menu' -import { objectOmit } from '@dzeio/object-util' +import { objectEqual, objectOmit } from '@dzeio/object-util' interface Props extends React.DetailedHTMLProps, HTMLInputElement> { id?: string @@ -123,8 +123,20 @@ export default class Input extends React.PureComponent { public async componentDidUpdate(prevProps: Props, prevStates: States) { - if (prevProps.value !== this.props.value) { - this.setState({ displayedValue: this.props.value?.toString(), value : this.props.value?.toString(), valueUpdate: true }) + if (prevProps.value !== this.props.value && this.props.value !== this.state.value) { + if (this.props.choices) { + const choice = this.props.choices.find((it) => typeof it === 'string' ? it : it.value === this.props.value?.toString()) + if (choice) { + this.setState({ + displayedValue: typeof choice === 'string' ? choice : choice.display, + value: typeof choice === 'string' ? choice : choice.value, + valueUpdate: true + }) + } + } else { + this.setState({ displayedValue: this.props.value?.toString(), value : this.props.value?.toString(), valueUpdate: true }) + + } } if (prevStates.value !== this.state.value) { if (this.props.onValue) { @@ -138,8 +150,10 @@ export default class Input extends React.PureComponent { if ( prevStates.value !== this.state.value || prevStates.displayedValue !== this.state.displayedValue || - prevStates.valueUpdate !== this.state.valueUpdate + prevStates.valueUpdate !== this.state.valueUpdate || + !objectEqual(prevProps.choices ?? [], this.props.choices ?? []) ) { + console.log('list updated') this.setState({list: this.buildList()}) } }