Skip to content

Commit

Permalink
[@mantine/core] Add option to configure clear button tab index in Sel…
Browse files Browse the repository at this point in the history
…ect, MultiSelect and DatePicker components (#1243)

* [@mantine/core] Select: Added tab index to clear button

Signed-off-by: Andreas Weisberg <andreas.weisberg@gmail.com>

* [@mantine/core] MultiSelect: Added tab index to clear button

Signed-off-by: Andreas Weisberg <andreas.weisberg@gmail.com>

* [@mantine/dates] DatePicker: Added tab index to clear button

Signed-off-by: Andreas Weisberg <andreas.weisberg@gmail.com>

* Made clear button tab index optional
  • Loading branch information
ahweis committed Apr 13, 2022
1 parent b0d879e commit 8516a5c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/mantine-core/src/components/MultiSelect/MultiSelect.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,25 @@ storiesOf('MultiSelect', module)
<MantineProvider defaultProps={{ MultiSelect: { radius: 0, label: 'Default label' } }}>
<MultiSelect data={data} placeholder="Select items" />
</MantineProvider>
))
.add('Clearable button not in tab index', () => (
<div style={{ padding: 40, maxWidth: 400 }}>
<MultiSelect
label="Multi select"
data={data}
defaultValue={['react', 'ng']}
placeholder="Select items"
nothingFound="Nothing found"
searchable
clearable
clearButtonTabIndex={-1}
/>
<MultiSelect
label="Multi select"
data={data}
defaultValue={['react', 'ng']}
placeholder="Select items"
nothingFound="Nothing found"
/>
</div>
));
6 changes: 6 additions & 0 deletions src/mantine-core/src/components/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export interface MultiSelectProps

/** Select highlighted item on blur */
selectOnBlur?: boolean;

/** Set the clear button tab index to disabled or default after input field */
clearButtonTabIndex?: -1 | 0;
}

export function defaultFilter(value: string, selected: boolean, item: SelectItem) {
Expand Down Expand Up @@ -121,6 +124,7 @@ const defaultProps: Partial<MultiSelectProps> = {
switchDirectionOnFlip: false,
zIndex: getDefaultZIndex('popover'),
selectOnBlur: false,
clearButtonTabIndex: 0,
};

export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>(
Expand Down Expand Up @@ -185,6 +189,7 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>(
errorProps,
labelProps,
descriptionProps,
clearButtonTabIndex,
...others
} = useMantineDefaultProps('MultiSelect', defaultProps, props);

Expand Down Expand Up @@ -589,6 +594,7 @@ export const MultiSelect = forwardRef<HTMLInputElement, MultiSelectProps>(
onClear: handleClear,
error,
disabled,
clearButtonTabIndex,
})}
>
<div className={classes.values}>
Expand Down
20 changes: 20 additions & 0 deletions src/mantine-core/src/components/Select/Select.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,24 @@ storiesOf('Select', module)
searchable
/>
</div>
))
.add('Clearable button not in tab index', () => (
<div style={{ padding: 40, maxWidth: 400 }}>
<Select
label="Search in first select"
placeholder="Choose value"
data={stringData}
searchable
clearable
clearButtonTabIndex={-1}
/>
<Select
label="Tab directly to next select"
placeholder="Choose value"
data={stringData}
searchable
clearable
clearButtonTabIndex={-1}
/>
</div>
));
6 changes: 6 additions & 0 deletions src/mantine-core/src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ export interface SelectProps

/** Should data be filtered when search value exactly matches selected item */
filterDataOnExactSearchMatch?: boolean;

/** Set the clear button tab index to disabled or default after input field */
clearButtonTabIndex?: -1 | 0;
}

export function defaultFilter(value: string, item: SelectItem) {
Expand Down Expand Up @@ -161,6 +164,7 @@ const defaultProps: Partial<SelectProps> = {
switchDirectionOnFlip: false,
filterDataOnExactSearchMatch: false,
zIndex: getDefaultZIndex('popover'),
clearButtonTabIndex: 0,
};

export const Select = forwardRef<HTMLInputElement, SelectProps>((props: SelectProps, ref) => {
Expand Down Expand Up @@ -220,6 +224,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props: SelectPr
labelProps,
placeholder,
filterDataOnExactSearchMatch,
clearButtonTabIndex,
...others
} = useMantineDefaultProps('Select', defaultProps, props);

Expand Down Expand Up @@ -608,6 +613,7 @@ export const Select = forwardRef<HTMLInputElement, SelectProps>((props: SelectPr
clearButtonLabel,
onClear: handleClear,
error,
clearButtonTabIndex,
})}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface SelectRightSectionProps {
error?: any;
// eslint-disable-next-line react/no-unused-prop-types
disabled?: boolean;
clearButtonTabIndex?: number;
}

export function SelectRightSection({
Expand All @@ -19,13 +20,15 @@ export function SelectRightSection({
onClear,
size,
error,
clearButtonTabIndex,
}: SelectRightSectionProps) {
return shouldClear ? (
<CloseButton
variant="transparent"
aria-label={clearButtonLabel}
onClick={onClear}
size={size}
tabIndex={clearButtonTabIndex}
/>
) : (
<ChevronIcon error={error} size={size} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ storiesOf('DatePicker', module)
allowFreeInput
/>
</SubmitForm>
))
.add('Clear button tab index disabled', () => (
<div style={{ padding: 40, maxWidth: 400 }}>
<DatePicker placeholder="Submit with enter" label="Event date" clearButtonTabIndex={-1} />
</div>
));
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export interface DatePickerBaseSharedProps

/** Modal z-index */
modalZIndex?: number;

/** Set the clear button tab index to disabled or default after input field */
clearButtonTabIndex?: -1 | 0;
}

export interface DatePickerBaseProps extends DatePickerBaseSharedProps {
Expand Down Expand Up @@ -181,6 +184,7 @@ export const DatePickerBase = forwardRef<HTMLInputElement, DatePickerBaseProps>(
errorProps,
labelProps,
descriptionProps,
clearButtonTabIndex = 0,
...others
}: DatePickerBaseProps,
ref
Expand Down Expand Up @@ -234,6 +238,7 @@ export const DatePickerBase = forwardRef<HTMLInputElement, DatePickerBaseProps>(
aria-label={clearButtonLabel}
onClick={onClear}
size={size}
tabIndex={clearButtonTabIndex}
/>
) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ storiesOf('DateRangePicker', module)
<DateRangePicker initialLevel="year" />
</div>
))
.add('Controlled', () => <Controlled />);
.add('Controlled', () => <Controlled />)
.add('Clear button tab index disabled', () => (
<div style={{ padding: 40 }}>
<DateRangePicker clearButtonTabIndex={-1} />
</div>
));

0 comments on commit 8516a5c

Please sign in to comment.