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

UnitControl: Set correct unit when units has one option #33634

Merged
merged 1 commit into from
Jul 23, 2021

Conversation

walbo
Copy link
Member

@walbo walbo commented Jul 22, 2021

Description

When only one option is passed to the units prop in UnitControl component the onChange always returns px.

Ex on pattern that will return px:

const [ value, setValue ] = useState( '10%' );

<UnitControl
	value={ value }
	unit="%"
	units={ [ { value: '%', label: '%' } ] }
	onChange={ setValue }
/>

How has this been tested?

Testet with both multiple and single option in units prop.

Screenshots

Before

units

After

units-fixed

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

@walbo walbo changed the title UnitController: Set correct unit when units has one option UnitControl: Set correct unit when units has one option Jul 23, 2021
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, @walbo.

I noticed this issue yesterday and was about the create an issue when I saw your PR 😄

@Mamaduka Mamaduka merged commit ece5560 into WordPress:trunk Jul 23, 2021
@github-actions github-actions bot added this to the Gutenberg 11.2 milestone Jul 23, 2021
@aaronrobertshaw
Copy link
Contributor

Thanks for fixing this issue @walbo. I was also looking into this problem but you were too quick for me 🚀

Our solutions were pretty similar the only difference being that if we are changing the behaviour of the hasUnits utility we need to also update the native UnitControl component. It currently relies on the old implementation of hasUnits to prevent rendering a touchable button and picker for the units when the user doesn't in fact have any alternative options.

I've created a quick PR to address adding explicit checks for units.length into the native unit control and restoring it previous behaviour.

@walbo walbo deleted the fix/single-units-in-unit-controller branch July 23, 2021 09:29
@walbo
Copy link
Member Author

walbo commented Jul 23, 2021

Thanks for the follow-up @aaronrobertshaw 👍 Didn't spot that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants