Skip to content

Commit

Permalink
Use default tag theme props if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
csandman committed Sep 19, 2024
1 parent c9f2c41 commit a89643a
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/use-chakra-select-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ const useChakraSelectProps = <
// eslint-disable-next-line deprecation/deprecation
theme,
size,
tagColorScheme = "gray",
isDisabled,
isInvalid,
isReadOnly,
required,
isRequired,
inputId,
tagVariant,
selectedOptionStyle = "color",
selectedOptionColorScheme = "blue",
variant,
tagColorScheme,
tagVariant,
focusBorderColor,
errorBorderColor,
chakraStyles = {},
Expand All @@ -33,7 +33,12 @@ const useChakraSelectProps = <
...props
}: Props<Option, IsMulti, Group>): Props<Option, IsMulti, Group> => {
const chakraTheme = useTheme();
const { variant: defaultVariant } = chakraTheme.components.Input.defaultProps;
const { variant: defaultVariant = "outline" } =
chakraTheme.components.Input.defaultProps;
const {
colorScheme: defaultTagColorScheme = "gray",
variant: defaultTagVariant = "subtle",
} = chakraTheme.components.Tag.defaultProps;

// Combine the props passed into the component with the props that can be set
// on a surrounding form control to get the values of `isDisabled` and
Expand Down Expand Up @@ -61,23 +66,23 @@ const useChakraSelectProps = <

// Ensure that the color used for the selected options is a string
let realSelectedOptionColorScheme: string = selectedOptionColorScheme;
if (typeof realSelectedOptionColorScheme !== "string") {
if (!(selectedOptionColorScheme in chakraTheme.colors)) {
realSelectedOptionColorScheme = "blue";
}

const select: Props<Option, IsMulti, Group> = {
const selectProps: Props<Option, IsMulti, Group> = {
// Allow overriding of custom components
components: {
...chakraComponents,
...components,
},
// Custom select props
tagColorScheme,
size,
tagVariant,
selectedOptionStyle: realSelectedOptionStyle,
selectedOptionColorScheme: realSelectedOptionColorScheme,
variant: variant ?? defaultVariant,
tagColorScheme: tagColorScheme ?? defaultTagColorScheme,
tagVariant: tagVariant ?? defaultTagVariant,
chakraStyles,
focusBorderColor,
errorBorderColor,
Expand All @@ -96,7 +101,7 @@ const useChakraSelectProps = <
"aria-invalid": props["aria-invalid"] ?? inputProps["aria-invalid"],
};

return select;
return selectProps;
};

export default useChakraSelectProps;

0 comments on commit a89643a

Please sign in to comment.