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

Add new getGroupOptions prop #5686

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tender-turkeys-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': minor
---

Add `getGroupOptions` prop
15 changes: 11 additions & 4 deletions packages/react-select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
getOptionLabel as getOptionLabelBuiltin,
getOptionValue as getOptionValueBuiltin,
isOptionDisabled as isOptionDisabledBuiltin,
getGroupOptions as getGroupOptionsBuiltin,
} from './builtins';

import { defaultComponents, SelectComponentsConfig } from './components/index';
Expand All @@ -53,6 +54,7 @@ import {
FocusDirection,
GetOptionLabel,
GetOptionValue,
GetGroupOptions,
GroupBase,
InputActionMeta,
MenuPlacement,
Expand Down Expand Up @@ -163,6 +165,8 @@ export interface Props<
getOptionLabel: GetOptionLabel<Option>;
/** Resolves option data to a string to compare options and specify value attributes */
getOptionValue: GetOptionValue<Option>;
/** Resolves option data to identify group and access group options */
getGroupOptions: GetGroupOptions<Option, Group>,
/** Hide the selected option from the menu */
hideSelectedOptions?: boolean;
/** The id to set on the SelectContainer component. */
Expand Down Expand Up @@ -292,6 +296,7 @@ export const defaultProps = {
formatGroupLabel: formatGroupLabelBuiltin,
getOptionLabel: getOptionLabelBuiltin,
getOptionValue: getOptionValueBuiltin,
getGroupOptions: getGroupOptionsBuiltin,
isDisabled: false,
isLoading: false,
isMulti: false,
Expand Down Expand Up @@ -394,24 +399,26 @@ function buildCategorizedOptions<
): CategorizedGroupOrOption<Option, Group>[] {
return props.options
.map((groupOrOption, groupOrOptionIndex) => {
if ('options' in groupOrOption) {
const categorizedOptions = groupOrOption.options
const groupOptions = props.getGroupOptions(groupOrOption);

if (groupOptions !== null) {
const categorizedOptions = groupOptions
.map((option, optionIndex) =>
toCategorizedOption(props, option, selectValue, optionIndex)
)
.filter((categorizedOption) => isFocusable(props, categorizedOption));
return categorizedOptions.length > 0
? {
type: 'group' as const,
data: groupOrOption,
data: groupOrOption as Group,
options: categorizedOptions,
index: groupOrOptionIndex,
}
: undefined;
}
const categorizedOption = toCategorizedOption(
props,
groupOrOption,
groupOrOption as Option,
selectValue,
groupOrOptionIndex
);
Expand Down
3 changes: 3 additions & 0 deletions packages/react-select/src/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export const getOptionValue = <Option>(option: Option): string =>

export const isOptionDisabled = <Option>(option: Option): boolean =>
!!(option as { isDisabled?: unknown }).isDisabled;

export const getGroupOptions = <Option, Group extends GroupBase<Option>>(groupOrOption: Option|Group): readonly Option[]|null =>
'options' in groupOrOption ? groupOrOption.options as readonly Option[] : null;
1 change: 1 addition & 0 deletions packages/react-select/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,6 @@ export type FocusDirection =

export type GetOptionLabel<Option> = (option: Option) => string;
export type GetOptionValue<Option> = (option: Option) => string;
export type GetGroupOptions<Option, Group extends GroupBase<Option>> = (groupOrOption: Option | Group) => readonly Option[]|null;

export type CSSObjectWithLabel = CSSObject & { label?: string };