Skip to content

Commit

Permalink
FIX Treat value not in SingleSelectField options as blank
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Dec 11, 2024
1 parent 0eb927c commit ed4a746
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

24 changes: 19 additions & 5 deletions client/src/components/SingleSelectField/SingleSelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class SingleSelectField extends Component {
this.handleChange = this.handleChange.bind(this);
}

componentDidMount() {
// If the value isn't in the available options, we should treat it as though it's empty.
// This is the same behaviour the entwine dropdown fields use.
if (!this.getOptions().map(option => option.value).includes(this.props.value)) {
if (typeof this.props.onChange === 'function') {
this.props.onChange(null, { id: this.props.id, value: '' });
}
}
}

/**
* Builds the select field in readonly mode with current props
*
Expand All @@ -34,9 +44,7 @@ class SingleSelectField extends Component {
*/
getSelectField() {
// .slice() to copy the array, because we could modify it with an empty item
const options = (this.props.source)
? this.props.source.slice()
: [];
const options = this.getOptions();

if (this.props.data.hasEmptyDefault && !options.find((item) => !item.value)) {
options.unshift({
Expand All @@ -48,7 +56,7 @@ class SingleSelectField extends Component {

return (
<Input type="select" {...this.getInputProps()}>
{ options.map((item, index) => {
{options.map((item, index) => {
const key = `${this.props.name}-${item.value || `empty${index}`}`;
const description = item.description || null;

Expand All @@ -57,7 +65,7 @@ class SingleSelectField extends Component {
{item.title}
</option>
);
}) }
})}
</Input>
);
}
Expand Down Expand Up @@ -85,6 +93,12 @@ class SingleSelectField extends Component {
return props;
}

getOptions() {
return (this.props.source)
? this.props.source.slice()
: [];
}

/**
* Handles changes to the select field's value.
*
Expand Down

0 comments on commit ed4a746

Please sign in to comment.