diff --git a/src/Input/index.tsx b/src/Input/index.tsx index 503bdcc..2624042 100644 --- a/src/Input/index.tsx +++ b/src/Input/index.tsx @@ -88,18 +88,19 @@ export default class Input extends React.PureComponent { } // Handle default Value - if (this.props.defaultValue) { + if (this.props.defaultValue || this.props.value) { + const value = this.props.defaultValue ?? this.props.value ?? '' if (!this.props.choices) { - this.setState({displayedValue: this.props.defaultValue.toString()}) + this.setState({displayedValue: value.toString()}) } else { - const res = this.props.choices.find((it) => { - return typeof it === 'string' ? it === this.props.defaultValue : it.value === this.props.defaultValue - }) + const res = this.props.choices.find( + (it) => typeof it === 'string' ? it === value : it.value === value + ) if (!res) { if (this.props.strictChoices) { this.setState({value: '', displayedValue: ''}) } else { - this.setState({displayedValue: this.props.defaultValue.toString()}) + this.setState({displayedValue: value.toString()}) } return } @@ -123,7 +124,7 @@ export default class Input extends React.PureComponent { public async componentDidUpdate(prevProps: Props, prevStates: States) { if (prevProps.value !== this.props.value) { - this.onChange(this.props.value?.toString() ?? '') + this.setState({ displayedValue: this.props.value?.toString(), value : this.props.value?.toString(), valueUpdate: true }) } if (prevStates.value !== this.state.value) { if (this.props.onValue) { @@ -147,7 +148,7 @@ export default class Input extends React.PureComponent { * return the real value of the field (depending if you a choices and the display value) * @returns the value of the field */ - public value(): string | number | readonly string[] | undefined { + public value(): string | number | ReadonlyArray | undefined { return this.state?.value ?? this.state.displayedValue ?? this.props.value ?? undefined } @@ -223,46 +224,46 @@ export default class Input extends React.PureComponent { return ( <> - {this.props.label && ( - - )} -
{this.props.label} )} - ref={this.parentRef} - > +
- {input as any} + {input as any} - {/* Left Icon */} - {this.getIcon(iconLeft, 'left')} + {/* Left Icon */} + {this.getIcon(iconLeft, 'left')} - {/* Right Icon */} - {iconRight ? - this.getIcon(iconRight, 'right') : - (this.props.choices && !this.props.disabled && !this.props.disableAutoIcons) && ( - - ) - } + {/* Right Icon */} + {iconRight ? + this.getIcon(iconRight, 'right') : + this.props.choices && !this.props.disabled && !this.props.disableAutoIcons && ( + + ) + } - {/* Helper text */} - {(this.props.helper) && ( - {this.props.helper} + {/* Helper text */} + {this.props.helper && ( + {this.props.helper} )} - {/* List when this is an autocomplete */} - {this.props.choices && ( - + {/* List when this is an autocomplete */} + {this.props.choices && ( + )} -
+
) } @@ -294,7 +295,7 @@ export default class Input extends React.PureComponent { } const v = this.state.displayedValue?.toLowerCase() return this.props.choices - .map((item, index) => typeof item === 'string' ? ({item: {display: item, value: item}, index}) : {item, index}) + .map((item, index) => typeof item === 'string' ? {item: {display: item, value: item}, index} : {item, index}) .filter( (item) => this.props.displayAllOptions || !this.state.valueUpdate || !v || item.item.display.toLowerCase().includes(v) || item.item.display.toLowerCase().toLowerCase().includes(v) ) @@ -326,8 +327,8 @@ export default class Input extends React.PureComponent { * @returns the icon */ private getIcon(Icon: Icon | { - icon: Icon; - transformer: (value: string) => string; + icon: Icon + transformer: (value: string) => string } | undefined, position: 'left' | 'right') { if (!Icon) { return undefined