From 5d960cc31670dcd9c8f17341f9111349ec66aa21 Mon Sep 17 00:00:00 2001 From: alex <44968616+nderkim@users.noreply.github.com> Date: Thu, 27 Oct 2022 14:22:10 +1100 Subject: [PATCH 01/12] Add classNames API --- docs/examples/Experimental.tsx | 2 + packages/react-select/src/Select.tsx | 18 +++- .../react-select/src/components/Control.tsx | 30 ++---- .../react-select/src/components/Group.tsx | 17 ++-- .../react-select/src/components/Input.tsx | 8 +- packages/react-select/src/components/Menu.tsx | 90 ++++++++--------- .../src/components/MultiValue.tsx | 96 +++++++------------ .../react-select/src/components/Option.tsx | 30 ++---- .../src/components/Placeholder.tsx | 13 +-- .../src/components/SingleValue.tsx | 15 ++- .../src/components/containers.tsx | 45 ++++----- .../src/components/indicators.tsx | 50 ++++------ packages/react-select/src/styles.ts | 56 +++++------ packages/react-select/src/types.ts | 9 +- packages/react-select/src/utils.ts | 33 ++++++- storybook/stories/WithTailwind.stories.tsx | 46 +++++++++ 16 files changed, 279 insertions(+), 279 deletions(-) create mode 100644 storybook/stories/WithTailwind.stories.tsx diff --git a/docs/examples/Experimental.tsx b/docs/examples/Experimental.tsx index 552132bcd2..236d6d0d9f 100644 --- a/docs/examples/Experimental.tsx +++ b/docs/examples/Experimental.tsx @@ -123,6 +123,7 @@ const Group = (props: GroupProps) => { const { Heading, getStyles, + getClassName, children, label, headingProps, @@ -136,6 +137,7 @@ const Group = (props: GroupProps) => { selectProps={selectProps} theme={theme} getStyles={getStyles} + getClassName={getClassName} cx={cx} {...headingProps} > diff --git a/packages/react-select/src/Select.tsx b/packages/react-select/src/Select.tsx index 11076fc810..bae7541a37 100644 --- a/packages/react-select/src/Select.tsx +++ b/packages/react-select/src/Select.tsx @@ -40,7 +40,12 @@ import { import { defaultComponents, SelectComponentsConfig } from './components/index'; -import { defaultStyles, StylesConfig, StylesProps } from './styles'; +import { + ClassNamesConfig, + defaultStyles, + StylesConfig, + StylesProps, +} from './styles'; import { defaultTheme, ThemeConfig } from './theme'; import { @@ -99,6 +104,10 @@ export interface Props< * This is useful when styling via CSS classes instead of the Styles API approach. */ classNamePrefix?: string | null; + /** + * Provide a className based on state for each inner component + */ + classNames: ClassNamesConfig; /** Close the select menu when the user selects an option */ closeMenuOnSelect: boolean; /** @@ -271,6 +280,7 @@ export const defaultProps = { backspaceRemovesValue: true, blurInputOnSelect: isTouchCapable(), captureMenuScroll: !isTouchCapable(), + classNames: {}, closeMenuOnSelect: true, closeMenuOnScroll: false, components: {}, @@ -1046,6 +1056,7 @@ export default class Select< clearValue, cx, getStyles, + getClassName, getValue, selectOption, setValue, @@ -1058,6 +1069,7 @@ export default class Select< clearValue, cx, getStyles, + getClassName, getValue, hasValue, isMulti, @@ -1085,6 +1097,10 @@ export default class Select< const custom = this.props.styles[key]; return custom ? custom(base, props as any) : base; }; + getClassName = >( + key: Key, + props: StylesProps[Key] + ) => this.props.classNames[key]?.(props as any); getElementId = ( element: | 'group' diff --git a/packages/react-select/src/components/Control.tsx b/packages/react-select/src/components/Control.tsx index 60534d0714..c00576d600 100644 --- a/packages/react-select/src/components/Control.tsx +++ b/packages/react-select/src/components/Control.tsx @@ -7,6 +7,7 @@ import { CSSObjectWithLabel, GroupBase, } from '../types'; +import { getStyleProps } from '../utils'; export interface ControlProps< Option = unknown, @@ -68,30 +69,17 @@ const Control = < >( props: ControlProps ) => { - const { - children, - cx, - getStyles, - className, - isDisabled, - isFocused, - innerRef, - innerProps, - menuIsOpen, - } = props; + const { children, isDisabled, isFocused, innerRef, innerProps, menuIsOpen } = + props; return (
{children} diff --git a/packages/react-select/src/components/Group.tsx b/packages/react-select/src/components/Group.tsx index cc3dee2d30..4d0c75413e 100644 --- a/packages/react-select/src/components/Group.tsx +++ b/packages/react-select/src/components/Group.tsx @@ -1,9 +1,10 @@ /** @jsx jsx */ import { ComponentType, ReactNode } from 'react'; import { jsx } from '@emotion/react'; -import { cleanCommonProps } from '../utils'; +import { cleanCommonProps, getStyleProps } from '../utils'; import { + CommonProps, CommonPropsAndClassName, CSSObjectWithLabel, CX, @@ -62,9 +63,9 @@ const Group = < ) => { const { children, - className, cx, getStyles, + getClassName, Heading, headingProps, innerProps, @@ -73,16 +74,13 @@ const Group = < selectProps, } = props; return ( -
+
{label} @@ -101,6 +99,7 @@ interface GroupHeadingPropsDefinedProps< selectProps: Props; theme: Theme; getStyles: GetStyles; + getClassName: CommonProps['getClassName']; cx: CX; } @@ -137,12 +136,10 @@ export const GroupHeading = < >( props: GroupHeadingProps ) => { - const { getStyles, cx, className } = props; const { data, ...innerProps } = cleanCommonProps(props); return (
); diff --git a/packages/react-select/src/components/Input.tsx b/packages/react-select/src/components/Input.tsx index 9b4e9e2ff1..cf5b02fe56 100644 --- a/packages/react-select/src/components/Input.tsx +++ b/packages/react-select/src/components/Input.tsx @@ -7,7 +7,7 @@ import { CSSObjectWithLabel, GroupBase, } from '../types'; -import { cleanCommonProps } from '../utils'; +import { cleanCommonProps, getStyleProps } from '../utils'; export interface InputSpecificProps< Option = unknown, @@ -93,14 +93,12 @@ const Input = < >( props: InputProps ) => { - const { className, cx, getStyles, value } = props; + const { cx, value } = props; const { innerRef, isDisabled, isHidden, inputClassName, ...innerProps } = cleanCommonProps(props); - return (
>( props: MenuProps ) => { - const { children, className, cx, getStyles, innerRef, innerProps } = props; - + const { children, innerRef, innerProps } = props; return (
@@ -426,18 +425,13 @@ export const MenuList = < >( props: MenuListProps ) => { - const { children, className, cx, getStyles, innerProps, innerRef, isMulti } = - props; + const { children, innerProps, innerRef, isMulti } = props; return (
@@ -485,17 +479,13 @@ export const NoOptionsMessage = < >( props: NoticeProps ) => { - const { children, className, cx, getStyles, innerProps } = props; + const { children, innerProps } = props; return (
{children} @@ -513,17 +503,13 @@ export const LoadingMessage = < >( props: NoticeProps ) => { - const { children, className, cx, getStyles, innerProps } = props; + const { children, innerProps } = props; return (
{children} @@ -578,17 +564,18 @@ export const MenuPortal = < Option, IsMulti extends boolean, Group extends GroupBase