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

Use index for value in SelectWidget #1562

Closed
wants to merge 3 commits into from

Conversation

tdamsma
Copy link

@tdamsma tdamsma commented Dec 30, 2019

This PR changes the way options are selected in the SelectWidget by using the options array index instead of a serializing the selected value to the DOM (cast to string). This allows removal of a lot of hacky code to guess type of deserialized values, and also allows for selecting complete objects via a dropdown, addressing #1494.

I've added a playground example. I haven't looked at fixing the tests yet as would like feedback on the approach first.

Copy link
Member

@epicfaace epicfaace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a great improvement!

return i;
}
}
return "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return an empty string by default?

Could we just return undefined instead -- so that it would also be possible to select ""? (an additional improvement from the old code)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like a good idea, will look into it

}}>
{!multiple && schema.default === undefined && (
<option value="">{placeholder}</option>
)}
{enumOptions.map(({ value, label }, i) => {
const disabled = enumDisabled && enumDisabled.indexOf(value) != -1;
return (
<option key={i} value={value} disabled={disabled}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is likely a breaking change because it changes what's rendered in the DOM (the value)... but it's fine to go into version 2, and worth it to allow for better selection of any kinds of enums.

@tdamsma
Copy link
Author

tdamsma commented Jan 12, 2020

This would be a great improvement!

Good to hear, what can I do help this get landed? Not really sure how to proceed. Lots of tests are failing, but as they are written now they would become kind of pointless as most tests I saw focus on checking the values in the DOM render which are just a bunch of integers with this change.

@epicfaace
Copy link
Member

It seems that many of the tests are failing because o is undefined in .map(o => o.value): https://github.com/rjsf-team/react-jsonschema-form/blob/master/packages/core/src/components/widgets/SelectWidget.js#L43

@heath-freenome heath-freenome added the p1 Important to fix soon label Jan 15, 2023
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Updated `optionId()` to take an index rather than an option
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Also passed index to `optionId()` rather than option
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Updated `optionId()` to take an index rather than an option
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Also passed index to `optionId()` rather than option
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Updated `optionId()` to take an index rather than an option
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Also passed index to `optionId()` rather than option
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
heath-freenome added a commit to heath-freenome/react-jsonschema-form that referenced this pull request Jan 30, 2023
Fixes rjsf-team#1494, reimplementing rjsf-team#1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Updated `optionId()` to take an index rather than an option
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Also passed index to `optionId()` rather than option
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly
@heath-freenome
Copy link
Member

Implemented this fully for all widgets that use enumOptions (not just SelectWidget) in #3411

heath-freenome added a commit that referenced this pull request Jan 30, 2023
* Fix: switch enumOptions rendering widgets to use indexes
Fixes #1494, reimplementing #1562 to make all widgets that render `enumOptions` switch from option value to option index
- In `@rjsf/utils`, added/updated `enumOptionsXXXX` methods that enable using index instead of values
  - Added 100% unit tests for these new methods
  - Updated `optionId()` to take an index rather than an option
  - Also remove the now unnecessary `processSelectValue()` function and its unit test
- In all themes, updated the `CheckboxesWidget`, `RadioWidget` and `SelectWidget` to use the `enumOptions[]` index rather than value for the html elements
  - Utilized the new `enumOptionsXXXX` methods to facilitate this transformation
  - Also passed index to `optionId()` rather than option
  - Updated the tests in `core` and snapshots in the other themes to represent this change from value to index
- Updated the `playground` to add the new `Enumerated Object` example
- Updated the documentation for the new methods and the breaking change this causes
- Updated the `CHANGELOG.md` file accordingly

* Apply suggestions from code review

Co-authored-by: Nick Grosenbacher <[email protected]>

---------

Co-authored-by: Nick Grosenbacher <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p1 Important to fix soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants