Skip to content

Commit

Permalink
Adding support for defined IDs in TextControl component (#52028)
Browse files Browse the repository at this point in the history
* Adding support for defined IDs in `TextControl`

This patch adds support for custom IDs in the `TextControl` component.

Currently any passed ID prop is ignored, and a auto-generated value is
used instead.

---
Resolves #24749

* Updating tests

Changing the test setup for labels to be more `testing-library` idiomatic.

* Updating CHANGELOG

Adding line item under Enhancements to include `TextControl` changes.

* Fixing use of `useInstanceId`

Dropping the old `useUniqueId` pattern in favour of the more succinct and complete `useInstanceId` usage.
  • Loading branch information
andrewhayward authored Jul 12, 2023
1 parent d59187f commit 357afa8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

## Unreleased

### Enhancements

- `TextControl`: Add `id` prop to allow for custom IDs in `TextControl`s ([#52028](https://github.com/WordPress/gutenberg/pull/52028)).

### Bug Fix

- `Popover`: Pin `react-dropdown-menu` version to avoid breaking changes in dependency updates. ([52356](https://github.com/WordPress/gutenberg/pull/52356)).
- `Popover`: Pin `react-dropdown-menu` version to avoid breaking changes in dependency updates. ([52356](https://github.com/WordPress/gutenberg/pull/52356)).

## 25.3.0 (2023-07-05)

Expand Down Expand Up @@ -32,6 +36,7 @@
- `UnitControl`: Revamp support for changing unit by typing ([#39303](https://github.com/WordPress/gutenberg/pull/39303)).
- `Modal`: Update corner radius to be between buttons and the site view frame, in a 2-4-8 system. ([#51254](https://github.com/WordPress/gutenberg/pull/51254)).
- `ItemGroup`: Update button focus state styles to be inline with other button focus states in the editor. ([#51576](https://github.com/WordPress/gutenberg/pull/51576)).
- `ItemGroup`: Update button focus state styles to target `:focus-visible` rather than `:focus`. ([#51787](https://github.com/WordPress/gutenberg/pull/51787)).

### Bug Fix

Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/text-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ function UnforwardedTextControl(
hideLabelFromVision,
value,
help,
id: idProp,
className,
onChange,
type = 'text',
...additionalProps
} = props;
const instanceId = useInstanceId( TextControl );
const id = `inspector-text-control-${ instanceId }`;
const id = useInstanceId( TextControl, 'inspector-text-control', idProp );
const onChangeValue = ( event: ChangeEvent< HTMLInputElement > ) =>
onChange( event.target.value );

Expand Down
61 changes: 61 additions & 0 deletions packages/components/src/text-control/test/text-control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import TextControl from '..';

const noop = () => {};

describe( 'TextControl', () => {
describe( 'When no ID prop is provided', () => {
it( 'should generate an ID', () => {
render( <TextControl onChange={ noop } value="" /> );

expect( screen.getByRole( 'textbox' ) ).toHaveAttribute(
'id',
expect.stringMatching( /^inspector-text-control-/ )
);
} );

it( 'should be labelled correctly', () => {
const labelValue = 'Test Label';
render(
<TextControl label={ labelValue } onChange={ noop } value="" />
);

expect(
screen.getByRole( 'textbox', { name: labelValue } )
).toBeVisible();
} );
} );

describe( 'When an ID prop is provided', () => {
const id = 'test-id';

it( 'should use the passed ID prop if provided', () => {
render( <TextControl id={ id } onChange={ noop } value="" /> );

expect( screen.getByRole( 'textbox' ) ).toHaveAttribute( 'id', id );
} );

it( 'should be labelled correctly', () => {
const labelValue = 'Test Label';
render(
<TextControl
label={ labelValue }
id={ id }
onChange={ noop }
value=""
/>
);

expect(
screen.getByRole( 'textbox', { name: labelValue } )
).toBeVisible();
} );
} );
} );

0 comments on commit 357afa8

Please sign in to comment.