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

react-components: Clearing input after creating new option #168

Merged
merged 2 commits into from
Nov 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ describe('<Autocomplete />', () => {
<Autocomplete
id="test-id"
options={[]}
allowCreateOption
onCreate={handleCreate}
/>,
);
Expand Down
22 changes: 13 additions & 9 deletions packages/react-components/src/Autocomplete/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ export type AutocompleteOwnProps<
* If `true`, the `input` element is required.
*/
required?: boolean;
/**
* If `true`, the create button element will be shown.
*/
allowCreateOption?: boolean;
/**
* If `true`, the `input` will indicate an error.
*/
Expand All @@ -101,7 +97,10 @@ export type AutocompleteOwnProps<
/**
* Render the tags elements.
*/
renderTag?: (props: object, option: T) => React.ReactNode;
renderTag?: (
props: ReturnType<UseAutocompleteReturnType<T, Multiple>['getTagProps']>,
option: T,
) => React.ReactNode;
/**
* The label to display when the tags are truncated (`limitTags`).
*/
Expand Down Expand Up @@ -461,6 +460,14 @@ export const Autocomplete = <
const rootProps = getRootProps();
const popoverProps = getPopoverProps();

const handleCreate = (event: React.SyntheticEvent) => {
if (onCreate && searchValue.length) {
onCreate(event, searchValue);
}

popoverProps.onClose(event);
};

const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
// Wait until IME is settled.
if (event.which !== 229) {
Expand All @@ -481,9 +488,7 @@ export const Autocomplete = <
// Prevent cursor move
event.preventDefault();

if (onCreate && !groupedOptions.length) {
onCreate(event, searchValue);
}
handleCreate(event);

popoverProps.onKeyDown(event);
break;
Expand Down Expand Up @@ -756,6 +761,5 @@ Autocomplete.defaultProps = {
loading: false,
loadingText: 'Loading...',
required: false,
allowCreateOption: false,
size: 'medium',
};
Loading