Skip to content

fix: update type definitions to use ConditionalValue for color palette props #362

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

Merged
merged 3 commits into from
Mar 25, 2025
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
8 changes: 4 additions & 4 deletions src/chakra-components/multi-value.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { ColorPalette, SystemStyleObject } from "@chakra-ui/react";
import type { SystemStyleObject } from "@chakra-ui/react";
import { Span, useChakraContext, useSlotRecipe } from "@chakra-ui/react";
import type {
GroupBase,
MultiValueGenericProps,
MultiValueProps,
MultiValueRemoveProps,
} from "react-select";
import type { TagVariant } from "../types";
import type { ColorPaletteProp, TagVariant } from "../types";
import { CloseIcon } from "./icons";

const hasColorPalette = (
option: unknown
): option is { colorPalette: ColorPalette } =>
): option is { colorPalette: ColorPaletteProp } =>
typeof option === "object" &&
option !== null &&
"colorPalette" in option &&
Expand Down Expand Up @@ -52,7 +52,7 @@ export const MultiValue = <
const { colorPalette: themeTagColorPalette, variant: defaultTagVariant } =
chakraContext.getSlotRecipe("tag").defaultVariants ?? {};

let optionColorPalette: ColorPalette | undefined = themeTagColorPalette;
let optionColorPalette: ColorPaletteProp = themeTagColorPalette;
if (hasColorPalette(data)) {
optionColorPalette = data.colorPalette;
} else if (tagColorPalette) {
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type {
ColorPalette,
SpinnerProps,
SystemStyleObject,
} from "@chakra-ui/react";
import type { SpinnerProps, SystemStyleObject } from "@chakra-ui/react";
import type { GroupBase, StylesConfig, ThemeConfig } from "react-select";
import type {
ChakraStylesConfig,
ColorPaletteProp,
SelectedOptionStyle,
SizeProp,
TagVariant,
Expand Down Expand Up @@ -86,8 +83,9 @@ declare module "react-select/base" {
* @see {@link https://github.com/csandman/chakra-react-select#colorscheme}
* @see {@link https://www.chakra-ui.com/docs/components/tag#colors}
* @see {@link https://www.chakra-ui.com/docs/styling/virtual-color}
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/9ecae5c/packages/react/src/styled-system/generated/system.gen.ts#L688}
*/
tagColorPalette?: ColorPalette;
tagColorPalette?: ColorPaletteProp;

/**
* The `variant` prop that will be forwarded to your `MultiValue` component
Expand Down Expand Up @@ -122,8 +120,9 @@ declare module "react-select/base" {
* @defaultValue `blue`
* @see {@link https://github.com/csandman/chakra-react-select#selectedoptioncolorpalette--default-blue}
* @see {@link https://www.chakra-ui.com/docs/theming/customization/colors}
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/9ecae5c/packages/react/src/styled-system/generated/system.gen.ts#L688}
*/
selectedOptionColorPalette?: ColorPalette;
selectedOptionColorPalette?: ColorPaletteProp;

/**
* The color value to style the border of the `Control` with when the
Expand Down Expand Up @@ -207,8 +206,9 @@ declare module "react-select" {
* The color palette of the filled in area of the spinner.
*
* @see {@link https://www.chakra-ui.com/docs/components/spinner#colors}
* @see {@link https://github.com/chakra-ui/chakra-ui/blob/9ecae5c/packages/react/src/styled-system/generated/system.gen.ts#L688}
*/
colorPalette?: ColorPalette;
colorPalette?: ColorPaletteProp;

/**
* The color of the filled in area of the spinner.
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type {
ColorPalette,
ConditionalValue,
SelectRootProps,
SystemStyleObject,
TagRootProps,
Expand Down Expand Up @@ -26,6 +28,14 @@ import type {
ValueContainerProps,
} from "react-select";

export type CssVars = `var(--${string})`;

export type AnyString = string & {};

export type ColorPaletteProp = ConditionalValue<
ColorPalette | CssVars | AnyString
>;

export interface SizeProps<PropType extends string | number = string | number> {
sm: PropType;
md: PropType;
Expand Down
9 changes: 6 additions & 3 deletions src/use-chakra-select-props.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { ColorPalette } from "@chakra-ui/react";
import { useFieldContext } from "@chakra-ui/react";
import type { GroupBase, Props } from "react-select";
import chakraComponents from "./chakra-components";
import type { SelectedOptionStyle, UseFieldReturn } from "./types";
import type {
ColorPaletteProp,
SelectedOptionStyle,
UseFieldReturn,
} from "./types";

const useChakraSelectProps = <
Option,
Expand Down Expand Up @@ -42,7 +45,7 @@ const useChakraSelectProps = <
}

// Ensure that the color used for the selected options is a string
let realSelectedOptionColorPalette: ColorPalette =
let realSelectedOptionColorPalette: ColorPaletteProp =
selectedOptionColorPalette || "blue";
if (typeof realSelectedOptionColorPalette !== "string") {
realSelectedOptionColorPalette = "blue";
Expand Down
Loading