Fixed displaying/resetting of selected value

This commit is contained in:
Philipp Czora
2018-11-19 16:10:36 +01:00
parent a949a15f5b
commit 3d8323aa8a
2 changed files with 23 additions and 12 deletions

View File

@@ -21,29 +21,33 @@ type Props = {
value?: AutocompleteObject
};
type State = {
value: AutocompleteObject
};
type State = {};
class Autocomplete extends React.Component<Props, State> {
handleInputChange = (newValue: SelectValue) => {
this.setState({ value: newValue.value });
this.props.valueSelected(newValue.value);
};
render() {
const { label, helpText, value } = this.props;
const stringValue = value ? value.id : "";
let selectValue = null;
if (value) {
selectValue = {
value,
label: value.displayName
};
}
return (
<div className="field">
<LabelWithHelpIcon label={label} helpText={helpText} />
<div className="control">
<AsyncSelect
cacheOptions
defaultOptions
loadOptions={this.props.loadSuggestions}
onChange={this.handleInputChange}
value={stringValue}
value={selectValue}
placeholder="Start typing..." // TODO: i18n
noOptionsMessage={() => <>No suggestion available</>} // TODO: i18n
/>
</div>
</div>

View File

@@ -15,15 +15,18 @@ type Props = {
};
type State = {
entryToAdd: AutocompleteObject
entryToAdd?: AutocompleteObject
};
class AutocompleteAddEntryToTableField extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { entryToAdd: undefined };
}
render() {
const { disabled, buttonLabel, fieldLabel, helpText } = this.props;
const { entryToAdd } = this.state;
return (
<div className="field">
<Autocomplete
@@ -50,11 +53,15 @@ class AutocompleteAddEntryToTableField extends React.Component<Props, State> {
appendEntry = () => {
const { entryToAdd } = this.state;
this.props.addEntry(entryToAdd.id);
this.setState({ ...this.state, entryToAdd: undefined });
if (!entryToAdd) {
return;
}
this.setState({ ...this.state, entryToAdd: undefined }, () =>
this.props.addEntry(entryToAdd.id)
);
};
handleAddEntryChange = (selection: any) => {
handleAddEntryChange = (selection: AutocompleteObject) => {
this.setState({
...this.state,
entryToAdd: selection