Skip to content

Commit

Permalink
feat(select-fields): add a prevent default on enter key press
Browse files Browse the repository at this point in the history
  • Loading branch information
maximedasilva committed Sep 11, 2024
1 parent 57df715 commit 81dbb30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/react/lib/SelectField/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const withOnlyArbitraryValues = () => (
placeholder="Type a name"
allowArbitraryItems={true}
multiple={true}
noOptionsEnabled={false}
/>
);

Expand All @@ -171,3 +172,25 @@ export const noEmpty = () => (
animateMenu={slideInDownMenu}
/>
);

export const onAForm = () => {
const ref = useRef<SelectFieldRef>(null);
const actions = action('submitted');

return (
<form
onSubmit={e => {
actions({ name: 'submitted', event: e });
e.preventDefault();
}}
>
<SelectField
ref={ref}
allowArbitraryItems={true}
multiple={true}
noOptionsEnabled={false}
/>
<button type="submit">Submit</button>
</form>
);
};
2 changes: 2 additions & 0 deletions packages/react/lib/SelectField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ const SelectField = forwardRef<SelectFieldRef, SelectFieldProps>(({

switch (e.key) {
case 'Enter':
e.preventDefault();

if (allowArbitraryItems) {
onSelectOption(state.search, { resetSearch: true });
}
Expand Down

0 comments on commit 81dbb30

Please # to comment.