Migrates V2 Select to new ui/components (#5400)
* v2 select to components folder + docs * Fix type errors * fix type checks * Fix imports Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
1 parent
e57f734e79
commit
1f4e6c1987
15 files changed
+150
-30
No files matched your search
@@ -16,3 +16,4 @@ export {
|
||||
InputFieldWithSelect,
|
||||
} from "./inputs/Input";
|
||||
export { Label } from "./inputs/Label";
|
||||
export { Select, SelectField, SelectWithValidation, getReactSelectProps } from "./select";
|
||||
@@ -0,0 +1,210 @@
|
||||
import { useId } from "@radix-ui/react-id";
|
||||
import * as React from "react";
|
||||
import ReactSelect, {
|
||||
components as reactSelectComponents,
|
||||
GroupBase,
|
||||
Props,
|
||||
SingleValue,
|
||||
MultiValue,
|
||||
SelectComponentsConfig,
|
||||
} from "react-select";
|
||||
|
||||
import classNames from "@calcom/lib/classNames";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
import { Label } from "../inputs/Label";
|
||||
import {
|
||||
ControlComponent,
|
||||
InputComponent,
|
||||
MenuComponent,
|
||||
MenuListComponent,
|
||||
OptionComponent,
|
||||
SingleValueComponent,
|
||||
ValueContainerComponent,
|
||||
MultiValueComponent,
|
||||
} from "./components";
|
||||
|
||||
export type SelectProps<
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
> = Props<Option, IsMulti, Group>;
|
||||
|
||||
export const getReactSelectProps = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
components,
|
||||
}: {
|
||||
className?: string;
|
||||
components: SelectComponentsConfig<Option, IsMulti, Group>;
|
||||
}) => ({
|
||||
className: classNames("block h-[36px] w-full min-w-0 flex-1 rounded-md", className),
|
||||
classNamePrefix: "cal-react-select",
|
||||
components: {
|
||||
...reactSelectComponents,
|
||||
IndicatorSeparator: () => null,
|
||||
Input: InputComponent,
|
||||
Option: OptionComponent,
|
||||
Control: ControlComponent,
|
||||
SingleValue: SingleValueComponent,
|
||||
Menu: MenuComponent,
|
||||
MenuList: MenuListComponent,
|
||||
ValueContainer: ValueContainerComponent,
|
||||
MultiValue: MultiValueComponent,
|
||||
...components,
|
||||
},
|
||||
});
|
||||
|
||||
export const Select = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
components,
|
||||
styles,
|
||||
...props
|
||||
}: SelectProps<Option, IsMulti, Group>) => {
|
||||
const reactSelectProps = React.useMemo(() => {
|
||||
return getReactSelectProps<Option, IsMulti, Group>({ className, components: components || {} });
|
||||
}, [className, components]);
|
||||
|
||||
return (
|
||||
<ReactSelect
|
||||
{...reactSelectProps}
|
||||
{...props}
|
||||
styles={{
|
||||
option: (defaultStyles, state) => ({
|
||||
...defaultStyles,
|
||||
backgroundColor: state.isSelected
|
||||
? state.isFocused
|
||||
? "var(--brand-color)"
|
||||
: "var(--brand-color)"
|
||||
: state.isFocused
|
||||
? "var(--brand-color-dark-mode)"
|
||||
: "var(--brand-text-color)",
|
||||
}),
|
||||
...styles,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
type IconLeadingProps = {
|
||||
icon: React.ReactNode;
|
||||
children?: React.ReactNode;
|
||||
} & React.ComponentProps<typeof reactSelectComponents.Control>;
|
||||
|
||||
export const IconLeading = ({ icon, children, ...props }: IconLeadingProps) => {
|
||||
return (
|
||||
<reactSelectComponents.Control {...props}>
|
||||
{icon}
|
||||
{children}
|
||||
</reactSelectComponents.Control>
|
||||
);
|
||||
};
|
||||
|
||||
export const SelectField = function SelectField<
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>(
|
||||
props: {
|
||||
name?: string;
|
||||
containerClassName?: string;
|
||||
label?: string;
|
||||
labelProps?: React.ComponentProps<typeof Label>;
|
||||
className?: string;
|
||||
error?: string;
|
||||
} & SelectProps<Option, IsMulti, Group>
|
||||
) {
|
||||
const { t } = useLocale();
|
||||
const { label = t(props.name || ""), containerClassName, labelProps, className, ...passThrough } = props;
|
||||
const id = useId();
|
||||
return (
|
||||
<div className={classNames(containerClassName)}>
|
||||
<div className={classNames(className)}>
|
||||
{!!label && (
|
||||
<Label htmlFor={id} {...labelProps} className={classNames(props.error && "text-red-900")}>
|
||||
{label}
|
||||
</Label>
|
||||
)}
|
||||
</div>
|
||||
<Select {...passThrough} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* TODO: It should replace Select after through testing
|
||||
*/
|
||||
export function SelectWithValidation<
|
||||
Option extends { label: string; value: string },
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
required = false,
|
||||
onChange,
|
||||
value,
|
||||
...remainingProps
|
||||
}: SelectProps<Option, IsMulti, Group> & { required?: boolean }) {
|
||||
const [hiddenInputValue, _setHiddenInputValue] = React.useState(() => {
|
||||
if (value instanceof Array || !value) {
|
||||
return;
|
||||
}
|
||||
return value.value || "";
|
||||
});
|
||||
|
||||
const setHiddenInputValue = React.useCallback((value: MultiValue<Option> | SingleValue<Option>) => {
|
||||
let hiddenInputValue = "";
|
||||
if (value instanceof Array) {
|
||||
hiddenInputValue = value.map((val) => val.value).join(",");
|
||||
} else {
|
||||
hiddenInputValue = value?.value || "";
|
||||
}
|
||||
_setHiddenInputValue(hiddenInputValue);
|
||||
}, []);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!value) {
|
||||
return;
|
||||
}
|
||||
setHiddenInputValue(value);
|
||||
}, [value, setHiddenInputValue]);
|
||||
|
||||
return (
|
||||
<div className={classNames("relative", remainingProps.className)}>
|
||||
<Select
|
||||
value={value}
|
||||
{...remainingProps}
|
||||
onChange={(value, ...remainingArgs) => {
|
||||
setHiddenInputValue(value);
|
||||
if (onChange) {
|
||||
onChange(value, ...remainingArgs);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
{required && (
|
||||
<input
|
||||
tabIndex={-1}
|
||||
autoComplete="off"
|
||||
style={{
|
||||
opacity: 0,
|
||||
width: "100%",
|
||||
height: 1,
|
||||
position: "absolute",
|
||||
}}
|
||||
value={hiddenInputValue}
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
onChange={() => {}}
|
||||
// TODO:Not able to get focus to work
|
||||
// onFocus={() => selectRef.current?.focus()}
|
||||
required={required}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
import {
|
||||
components as reactSelectComponents,
|
||||
ControlProps,
|
||||
GroupBase,
|
||||
InputProps,
|
||||
MenuListProps,
|
||||
MenuProps,
|
||||
MultiValueProps,
|
||||
OptionProps,
|
||||
SingleValueProps,
|
||||
ValueContainerProps,
|
||||
} from "react-select";
|
||||
|
||||
import { classNames } from "@calcom/lib";
|
||||
|
||||
import { Icon } from "../../../Icon";
|
||||
|
||||
export const InputComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
inputClassName,
|
||||
...props
|
||||
}: InputProps<Option, IsMulti, Group>) => {
|
||||
return (
|
||||
<reactSelectComponents.Input
|
||||
// disables our default form focus hightlight on the react-select input element
|
||||
inputClassName={classNames(
|
||||
"focus:ring-0 focus:ring-offset-0 dark:!text-darkgray-900 !text-black",
|
||||
inputClassName
|
||||
)}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export const OptionComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: OptionProps<Option, IsMulti, Group>) => {
|
||||
return (
|
||||
<reactSelectComponents.Option
|
||||
{...props}
|
||||
className={classNames(
|
||||
className,
|
||||
"dark:bg-darkgray-100 !flex !cursor-pointer justify-between !py-3",
|
||||
props.isFocused && "dark:!bg-darkgray-200 !bg-gray-100",
|
||||
props.isSelected && "dark:!bg-darkgray-300 !bg-neutral-900"
|
||||
)}>
|
||||
<span>{props.label}</span> {props.isSelected && <Icon.FiCheck className="h-4 w-4" />}
|
||||
</reactSelectComponents.Option>
|
||||
);
|
||||
};
|
||||
|
||||
export const ControlComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: ControlProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.Control
|
||||
{...props}
|
||||
className={classNames(
|
||||
className,
|
||||
"dark:bg-darkgray-100 dark:border-darkgray-300 border-gray-300 bg-white text-sm leading-4 placeholder:text-sm placeholder:font-normal focus-within:border-0 focus-within:ring-2 focus-within:ring-neutral-800 hover:border-neutral-400 dark:focus-within:ring-white"
|
||||
)}
|
||||
/>
|
||||
);
|
||||
|
||||
export const SingleValueComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: SingleValueProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.SingleValue
|
||||
{...props}
|
||||
className={classNames(
|
||||
className,
|
||||
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400"
|
||||
)}
|
||||
/>
|
||||
);
|
||||
|
||||
export const ValueContainerComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: ValueContainerProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.ValueContainer
|
||||
{...props}
|
||||
className={classNames(
|
||||
"dark:text-darkgray-900 dark:placeholder:text-darkgray-500 text-black placeholder:text-gray-400",
|
||||
className
|
||||
)}
|
||||
/>
|
||||
);
|
||||
|
||||
export const MultiValueComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: MultiValueProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.MultiValue
|
||||
{...props}
|
||||
className={classNames(
|
||||
"dark:bg-darkgray-200 dark:text-darkgray-900 !rounded-md bg-gray-100 text-gray-700",
|
||||
className
|
||||
)}
|
||||
/>
|
||||
);
|
||||
|
||||
export const MenuComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: MenuProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.Menu
|
||||
{...props}
|
||||
className={classNames(
|
||||
"dark:bg-darkgray-100 dark:border-darkgray-300 border-1 !rounded-md border-gray-900 bg-white text-sm leading-4 dark:text-white",
|
||||
className
|
||||
)}
|
||||
/>
|
||||
);
|
||||
|
||||
export const MenuListComponent = <
|
||||
Option,
|
||||
IsMulti extends boolean = false,
|
||||
Group extends GroupBase<Option> = GroupBase<Option>
|
||||
>({
|
||||
className,
|
||||
...props
|
||||
}: MenuListProps<Option, IsMulti, Group>) => (
|
||||
<reactSelectComponents.MenuList
|
||||
{...props}
|
||||
className={classNames(
|
||||
"scrollbar-thin scrollbar-thumb-rounded-md dark:scrollbar-thumb-darkgray-300 scrollbar-thumb-gray-300 scrollbar-track-transparent scrollbar-track-w-[80px] rounded-md",
|
||||
className
|
||||
)}
|
||||
/>
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
export { SelectWithValidation, SelectField, getReactSelectProps, Select } from "./Select";
|
||||
@@ -0,0 +1,115 @@
|
||||
import { Canvas, Meta, Story, ArgsTable } from '@storybook/addon-docs';
|
||||
import { Examples, Example, Note, Title,CustomArgsTable, VariantRow,VariantsTable} from '@calcom/storybook/components'
|
||||
import { Icon } from "@calcom/ui/Icon";
|
||||
|
||||
import {SelectField} from "./Select"
|
||||
|
||||
<Meta title="UI/Form/Select Field" component={SelectField} />
|
||||
|
||||
<Title title="Select" suffix="Brief" subtitle="Version 2.0 — Last Update: 22 Aug 2022"/>
|
||||
|
||||
## Definition
|
||||
Dropdown fields allow users to input existing options that is preset by the deisgner/ developer. It can be just one choice per field, or they might be multiple choices depends on the circumstances.
|
||||
|
||||
## Structure
|
||||
<CustomArgsTable of={SelectField} />
|
||||
|
||||
|
||||
|
||||
export const options = [
|
||||
{ value: 0, label: "Option One" },
|
||||
{ value: 1, label: "Option Two" },
|
||||
];
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
<Examples title=" Single Selected / Unselected" footnote={
|
||||
<ul>
|
||||
<li>The difference between the types are when they are filled. </li>
|
||||
</ul>
|
||||
}>
|
||||
<Example title="Single Select [Unselected]">
|
||||
<SelectField
|
||||
label={"Single Select"}
|
||||
options={options}
|
||||
/>
|
||||
</Example>
|
||||
<Example title="Single Select [Selected]">
|
||||
<SelectField
|
||||
label={"Single Select"}
|
||||
options={options}
|
||||
defaultValue={options[0]}
|
||||
/>
|
||||
</Example>
|
||||
<Example title="Multi Select [Unselected]">
|
||||
<SelectField
|
||||
label={"Multi Select"}
|
||||
options={options}
|
||||
isMulti={true}
|
||||
/>
|
||||
</Example>
|
||||
<Example title="Multi Select [Selected]">
|
||||
<SelectField
|
||||
label={"Multi Select"}
|
||||
options={options}
|
||||
isMulti={true}
|
||||
defaultValue={options[0]}
|
||||
/>
|
||||
</Example>
|
||||
</Examples>
|
||||
|
||||
<Examples title="Variants">
|
||||
<Example title="Default">
|
||||
<SelectField
|
||||
label={"Default Select"}
|
||||
options={options}/>
|
||||
</Example>
|
||||
<Example title="Icon Left">
|
||||
WIP
|
||||
{/* <SelectField options={options} components={{ Control }}/> */}
|
||||
</Example>
|
||||
</Examples>
|
||||
|
||||
|
||||
## Variant Caviats (WIP) - To be updated
|
||||
Using Icons is a bit of a strange one cause you can't simpily pass in an icon as a prop. You have to pass in a component. To the select field.
|
||||
|
||||
```js
|
||||
// Bad: Inline declaration will cause remounting issues
|
||||
const BadSelect = props => (
|
||||
<Select {...props} components={{
|
||||
Control: ({ children, ...rest }) => (
|
||||
<components.Control {...rest}>
|
||||
👎 {children}
|
||||
</components.Control>
|
||||
)}}
|
||||
/>
|
||||
)
|
||||
|
||||
// Good: Custom component declared outside of the Select scope
|
||||
const Control = <IconLeading icon={Icon.FiPlus}/>
|
||||
|
||||
const GoodSelect = props => <Select {...props} components={{ Control }} />
|
||||
|
||||
```
|
||||
|
||||
<Examples title="States ">
|
||||
<Example title="Default">
|
||||
<SelectField options={options} label={"Default Select"}/>
|
||||
</Example>
|
||||
{/* <Example title="Hover">
|
||||
<SelectField options={options} className="sb-pseudo--hover"/>
|
||||
</Example>
|
||||
<Example title="Focus">
|
||||
<SelectField options={options} className="sb-pseudo--focus"/>
|
||||
</Example> */}
|
||||
</Examples>
|
||||
|
||||
|
||||
## Select Story
|
||||
<Canvas>
|
||||
<Story name="Default">
|
||||
<SelectField options={options} label={"Default Select"}/>
|
||||
</Story>
|
||||
</Canvas>
|
||||
@@ -23,6 +23,9 @@ export {
|
||||
TextAreaField,
|
||||
TextField,
|
||||
InputFieldWithSelect,
|
||||
Select,
|
||||
SelectField,
|
||||
SelectWithValidation,
|
||||
} from "./form";
|
||||
export { TopBanner } from "./top-banner";
|
||||
export type { TopBannerProps } from "./top-banner";
|
||||
Reference in new issue
Block a user