-
Notifications
You must be signed in to change notification settings - Fork 81
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
feat(Autocomplete): add controllable isOpen, inputValue props; extend text behaviour on item select #2567
feat(Autocomplete): add controllable isOpen, inputValue props; extend text behaviour on item select #2567
Conversation
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
8036755
to
d1b88c8
Compare
π¦ Changeset detectedLatest commit: 13e66bb The changes in this PR will be included in the next version bump. This PR includes changesets to release 32 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
}} | ||
onBlur={(e) => { | ||
onBlur?.(e as React.FocusEvent<HTMLInputElement>); | ||
inputProps.onBlur(e); |
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.
was missing and didn't fire onBlur properly
if (!closeAfterSelect) { | ||
toggleMenu(); | ||
} |
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.
this logic is moved to stateReducer
so it doesnt cause extra rerenders and doesnt fire onIsOpenChange
size-limit report π¦
|
if (clearAfterSelect) { | ||
handleInputValueChange(''); | ||
} |
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.
moved to onInputValueChange
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 to me - just a nitpick on the testing style. I didn't approve as this should be done by the owning team.
const input = screen.getByTestId('cf-autocomplete-input'); | ||
const container = screen.getByTestId('cf-autocomplete-container'); | ||
|
||
// Type one letter in the input to open the list | ||
fireEvent.input(input, { | ||
target: { | ||
value: 'a', | ||
}, | ||
}); | ||
|
||
// checks if the list is visible | ||
await waitFor(() => { | ||
expect(container).toBeVisible(); | ||
}); | ||
|
||
// go to the list first item | ||
fireEvent.keyDown(input, { | ||
key: 'ArrowDown', | ||
}); | ||
|
||
// press Enter to select the item | ||
fireEvent.keyDown(input, { | ||
key: 'Enter', | ||
}); |
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.
[nitpick]
Considering that we try to replace usage of fireEvent with userEvent for more realistic input testing, as well as some other recommended testing styles (e.g. avoid test IDs but use realistic queries), I would rewrite it like this:
const input = screen.getByTestId('cf-autocomplete-input'); | |
const container = screen.getByTestId('cf-autocomplete-container'); | |
// Type one letter in the input to open the list | |
fireEvent.input(input, { | |
target: { | |
value: 'a', | |
}, | |
}); | |
// checks if the list is visible | |
await waitFor(() => { | |
expect(container).toBeVisible(); | |
}); | |
// go to the list first item | |
fireEvent.keyDown(input, { | |
key: 'ArrowDown', | |
}); | |
// press Enter to select the item | |
fireEvent.keyDown(input, { | |
key: 'Enter', | |
}); | |
await userEvent.type(screen.getByRole('textbox'), 'a'); | |
expect(await screen.findByRole('menuitem', { name: /apple/ }).toBeInTheDocument(); | |
await userEvent.type(screen.getByRole('textbox'), '{arrowdown}{enter}'); |
All of these changes are also further explained in this blog post which I can highly recommend to understand why we shouldn't use test IDs, etc.
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 just copied one of the test cases and adjusted it for the new prop. but i refactored the test file now using user event and getByRole
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.
Thank you! I know that many of our repositories are still in this old state. Just want to avoid that reproduce it - maybe we can get rid of these things by slowly refactoring it bit by bit.
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.
Prop changes like this are a breaking change. Since the Autocomplete
is considered stable we will have to deprecate instead and remove in v5.
6f7745d
to
75624e3
Compare
thanks! done |
75624e3
to
cddd58d
Compare
cddd58d
to
f40922f
Compare
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 good, could you please add an example of using the new props in the documentation and storybook as per in the video? I was not able to reproduce it myself π€
curious what you think - there is an example for but i defo can add more stories to the storybook |
extended |
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.
Looking good π I noted down the deprecation for the next major release.
Purpose of PR
What's changed:
clearAfterSelect
(boolean
) replaced with πtextOnAfterSelect
('clear' | 'preserve' | 'replace'
) to be able to preserve the text input value when item selectedinputValue
prop added to be able to clear the text input programmatically (e.g. on menu close)isOpen
prop added (and correspondingonOpen
,onClose
handlers) to be able to control menu state and subscribe to its changeScreen.Recording.2023-08-25.at.16.05.03.mov