Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Treat value not in SingleSelectField options as blank #1872

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

34 changes: 29 additions & 5 deletions client/src/components/SingleSelectField/SingleSelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ 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.
let found = false;
/* eslint-disable-next-line no-restricted-syntax */
for (const option of this.getOptions()) {
// Intentionally use == instead of === to allow strings to match ints
// eslint-disable-next-line eqeqeq
if (option.value == this.props.value) {
found = true;
break;
}
}
if (!found) {
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 +54,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 +66,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 +75,7 @@ class SingleSelectField extends Component {
{item.title}
</option>
);
}) }
})}
</Input>
);
}
Expand Down Expand Up @@ -85,6 +103,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
Loading