-
Notifications
You must be signed in to change notification settings - Fork 49
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
Fixes Select Component initial value is always the first option #191
Conversation
When using the SelectControl component, the initial value for the select element upon page load uses the value of the first child option element, rather than having no - or an empty - value. To deal with this, added the first disabled option "- Select -". Since the 'SelectControl as BaseComponent' from '@wordpress/components' doesn't support the 'disabled' attribute for the <option> element, switched to using a manual <select> element, instead of using the BaseComponent from the library. Also in the Components Demo wizard, updated more meaningful labels and fixed the wrong use of onChange to handle event as argument, rather than value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to propose a different short-term solution to this that assumes this line will eventually find its way into @wordpress/components
:
- Within the Newspack component's src directory, duplicate the Gutenberg
SelectControl
source. You'll need to change theBaseControl
import but other than that it should be clean. - Change the
SelectControl
import to grab your copy rather than the original. - Add support for the disabled attribute to the copied code.
- Make the changes to assets/components/src/select-control/index.js as you would have originally had the disabled support been available.
If and when the proposed change is accepted in @wordpress/components
we'll simply delete the copy and revert the SelectControl
import.
A couple of other comments:
- The text of the disabled first option should be settable via a prop (maybe
placeholderOptionLabel
?) - Including this option should be optional. I'd probably suggest that the option is included only if the text is explicitly set.
This reverts commit 2a0ad6c which uses a <select> HTML element.
Created a local copy of the SelectComponent from @wordpress/components which implements the change proposed in WordPress/gutenberg#15976. The Newspack's component now uses the local assets/components/src/select-control-gutenberg-modified. Once the PR is implemented, we should switch back to the WP component. Added the first disabled option in the Components Demo Wizard. Also updated the labels there. Updated the SelectComponent's CSS, improved the disabled SelectOption visuals.
Thanks for all the comments! Great thinking! 👍I agree, that's a smarter solution, since it will make switching to the updated To handle both your comments, I made the disabled 1st option just a regular option with the It would be great if you could please give it another test run. |
Renamed component folder 'select-control-gutenberg-modified' to 'select-control-wordpress-components-modified'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you might be using a compiled file rather than the src? Try using this one instead:
You'll need to make only a couple of changes to it:
- Import
BaseControl
from@wordpress/components
instead of locally:
import { BaseControl } from '@wordpress/components';
- Add an export at the end:
export default withInstanceId( SelectControl );
and of course, add the disabled attribute handling.
The only downside is we're losing the Gutenberg component's styling (https://github.com/WordPress/gutenberg/blob/master/packages/components/src/select-control/style.scss), which would be a little tricky to import since it uses the break-medium
mixin.
I would also suggest putting it in a subdirectory of assets/components/src/select-control
. so there isn't any confusion about it being a new Component meant for use.
Moved the custom version of the wordpress-components Select Control file inside assets/components/src/select-control. Updated the same file to the source (not build) version.
Thank you for noticing all this! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for doing this! One last request—could you also run the stylesheet through Prettier? That will also catch a couple of syntactical odds and ends. Once you've done this, 🚢!
@jeffersonrabb thank you so much for your patience! Reformatted the stylesheet as well, and it's up to the formatting standards now. Merging! |
All Submissions:
Changes proposed in this Pull Request:
As detected by issue #83 , "... when using the SelectControl component, the initial value for the select element upon page load uses the value of the first child option element, rather than having no - or an empty - value".
Here this is fixed by adding a first
disabled
option with the label "- Select -" and emptyvalue
.However, since the
SelectControl as BaseComponent
from'@wordpress/components
doesn't support thedisabled
attribute for the<option>
element (see in codebase), now switched to using a manual<select>
HTML element.This PR should eventually update the BaseComponent, but since a PR process could be relatively lengthy, and our publishers are experiencing the bug right now, currently switched the component to use a pure
<select>
element with a<label>
.Also as a minor change, in the Components Demo wizard, updated more meaningful labels and fixed the wrong use of onChange to handle event as argument, rather than value.
When now clicking on the Select component (specifically relevant for a Select with no previous selections), a first disabled option "- Select -" is present, enabling correct onClick of the first option:
Closes #83 .
How to test the changes in this Pull Request:
nvm use 10.10.0 && npm ci && npm run clean && npm run build:webpack -- --watch
./wp-admin/admin.php?page=newspack-components-demo
.Other information: