Skip to content

Commit

Permalink
[@mantine/core] TagsInput: Fix selectFirstOptionOnChange prop not w…
Browse files Browse the repository at this point in the history
…orking (#6337)

* [@mantine/core] Implement selectFirstOptionOnChange for TagsInput

* Fix prettier test

* More consistent approach

* Prettier again

* Revert onChange for TagsInput to remove from diff

* Remove selectFirstOptionOnChange from groups demo

---------

Co-authored-by: Alex Lapp <[email protected]>
  • Loading branch information
alexlapp and Alex Lapp authored Jun 27, 2024
1 parent f490181 commit 7a321ea
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TagsInput } from '@mantine/core';
import { MantineDemo } from '@mantinex/demo';

const code = `
import { TagsInput } from '@mantine/core';
function Demo() {
return (
<TagsInput
label="Enter tags"
placeholder="Enter tags"
selectFirstOptionOnChange
data={[
'Apple', 'Banana', 'Kiwi', 'Mango', 'Watermelon', 'Raspberry', 'Strawberry',
]}
/>
);
}
`;

function Demo() {
return (
<TagsInput
label="Enter tags"
placeholder="Enter tags"
selectFirstOptionOnChange
data={['Apple', 'Banana', 'Kiwi', 'Mango', 'Watermelon', 'Raspberry', 'Strawberry']}
/>
);
}

export const selectFirst: MantineDemo = {
type: 'code',
component: Demo,
code,
maxWidth: 340,
centered: true,
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export const Demo_search = {
render: renderDemo(demos.search),
};

export const Demo_selectFirst = {
name: '⭐ Demo: selectFirstOptionOnChange',
render: renderDemo(demos.selectFirst),
};

export const Demo_scrollArea = {
name: '⭐ Demo: scrollArea',
render: renderDemo(demos.scrollArea),
Expand Down
1 change: 1 addition & 0 deletions packages/@docs/demos/src/demos/core/TagsInput/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export { withinPopover } from './TagsInput.demo.withinPopover';
export { renderOption } from './TagsInput.demo.renderOption';
export { dropdownWidth } from './TagsInput.demo.dropdownWidth';
export { acceptValueOnBlur } from './TagsInput.demo.acceptValueOnBlur';
export { selectFirst } from './TagsInput.demo.selectFirst';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef } from 'react';
import { useEffect, useRef } from 'react';
import { useId, useMergedRef, useUncontrolled } from '@mantine/hooks';
import {
BoxProps,
Expand Down Expand Up @@ -343,6 +343,12 @@ export const TagsInput = factory<TagsInputFactory>((_props, ref) => {
</Pill>
));

useEffect(() => {
if (selectFirstOptionOnChange) {
combobox.selectFirstOption();
}
}, [selectFirstOptionOnChange, _value, _searchValue]);

const clearButton = clearable && _value.length > 0 && !disabled && !readOnly && (
<Combobox.ClearButton
size={size as string}
Expand Down

0 comments on commit 7a321ea

Please sign in to comment.