Skip to content

Commit

Permalink
fix: combobox rendered text when value prop changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzucchelli committed Oct 6, 2023
1 parent 79d7b72 commit c80c271
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-buses-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@strapi/design-system': patch
---

Fixed a visual bug for Combobox component when value is updated externally
9 changes: 9 additions & 0 deletions packages/strapi-design-system/src/Combobox/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ export const ComboboxInput = React.forwardRef<ComboboxInputElement, ComboboxInpu
skipWhen: !internalIsOpen,
});

React.useEffect(() => {
/**
* When the value prop changes "externally" update the text input value too
*/
handleTextValueChange(value || '');

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);

const hintId = `${generatedId}-hint`;
const errorId = `${generatedId}-error`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe('Combobox', () => {
expect(getByRole('combobox')).toHaveValue('Strawberry 2');
});

it('should correctly change the rendered text value of the combobox when the value prop changes externally', async () => {
const onTextValueChange = jest.fn();
const { rerender } = renderRTL(<Component options={defaultOptions} onTextValueChange={onTextValueChange} />);

expect(onTextValueChange).toHaveBeenCalledTimes(1);
expect(onTextValueChange).toHaveBeenCalledWith('');

rerender(<Component options={defaultOptions} onTextValueChange={onTextValueChange} value="bagel" />);

expect(onTextValueChange).toHaveBeenCalledTimes(2);
expect(onTextValueChange).toHaveBeenCalledWith('bagel');
});

describe('callbacks', () => {
it('should fire onChange only when the value is changed not when the input does', async () => {
const onChange = jest.fn();
Expand Down

0 comments on commit c80c271

Please sign in to comment.