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

[@mantine/core] Implement selectFirstOptionOnChange for TagsInput #6337

Merged
merged 8 commits into from
Jun 27, 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
@@ -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 @@ -334,6 +334,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
Loading