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 UnitControl resets unit when value is cleared #39531

Merged
merged 4 commits into from
Mar 21, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
### Bug Fix

- `NumberControl`: commit (and constrain) value on `blur` event ([#39186](https://github.com/WordPress/gutenberg/pull/39186)).
- Fix `UnitControl`'s reset of unit when the quantity value is cleared. ([#39531](https://github.com/WordPress/gutenberg/pull/39531/)).

## 19.6.0 (2022-03-11)

Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/unit-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ function UnforwardedUnitControl(
);

useEffect( () => {
setUnit( parsedUnit );
if ( parsedUnit !== undefined ) {
setUnit( parsedUnit );
}
}, [ parsedUnit ] );

// Stores parsed value for hand-off in state reducer.
Expand Down
19 changes: 19 additions & 0 deletions packages/components/src/unit-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,25 @@ describe( 'UnitControl', () => {
await waitFor( () => expect( selectA ).toHaveValue( 'vw' ) );
expect( selectB ).toHaveValue( 'vw' );
} );

it( 'should maintain the chosen non-default unit when value is cleared', async () => {
const units = [
{ value: 'pt', label: 'pt' },
{ value: 'vmax', label: 'vmax' },
];

const { user } = render(
<UnitControl units={ units } value="5" />
);

const select = getSelect();
await user.selectOptions( select, [ 'vmax' ] );

const input = getInput();
await user.clear( input );

expect( select ).toHaveValue( 'vmax' );
} );
} );

describe( 'Unit Parser', () => {
Expand Down