mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-11-18 07:22:06 +00:00
Factorise Button and Input components
This commit is contained in:
@@ -15,7 +15,7 @@ import AsyncSelect from "react-select/async";
|
||||
import { Prettify } from "@/types";
|
||||
|
||||
import { Field, GenericFieldProps } from "./Field";
|
||||
import { BaseOption, classNames } from "./Select";
|
||||
import { BaseOption, createSelectClassNamesFn } from "./Select";
|
||||
|
||||
export type AsyncSelectFieldProps<
|
||||
TFieldValues extends FieldValues = FieldValues,
|
||||
@@ -34,6 +34,7 @@ export type AsyncSelectFieldProps<
|
||||
loadOptions: (searchString?: string) => Promise<BaseOption<T, D>[]>;
|
||||
onInformChange?: (data: BaseOption<T, D>[] | null) => void;
|
||||
separators?: boolean;
|
||||
size?: "small" | "medium";
|
||||
}
|
||||
>;
|
||||
|
||||
@@ -50,6 +51,7 @@ export const AsyncSelectField = <
|
||||
label,
|
||||
hideErrorMessage,
|
||||
required,
|
||||
size,
|
||||
|
||||
loadOption,
|
||||
loadOptions,
|
||||
@@ -132,6 +134,10 @@ export const AsyncSelectField = <
|
||||
return options;
|
||||
};
|
||||
|
||||
const classNames = createSelectClassNamesFn<BaseOption<T, D>, IsMulti>({
|
||||
size,
|
||||
});
|
||||
|
||||
return (
|
||||
<Field {...{ name, control, className, label, hideErrorMessage, required }}>
|
||||
<Controller
|
||||
@@ -184,10 +190,7 @@ export const AsyncSelectField = <
|
||||
},
|
||||
}),
|
||||
}}
|
||||
classNames={classNames<BaseOption<T, D>, IsMulti>(
|
||||
hasError,
|
||||
separators ?? false,
|
||||
)}
|
||||
classNames={classNames(hasError, separators ?? false)}
|
||||
{...selectProps}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -7,6 +7,8 @@ import { ReactDatePickerCustomHeaderProps } from "react-datepicker";
|
||||
import { IoChevronBack, IoChevronForward } from "react-icons/io5";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
import { Button } from "@/components/Button";
|
||||
|
||||
const chevronClasses = {
|
||||
chevronRoot: "h-4 w-4 dark:text-white",
|
||||
chevronClassDisabled: "!text-neutral-500",
|
||||
@@ -23,7 +25,9 @@ export const DatePickerCustomHeader = ({
|
||||
|
||||
return (
|
||||
<div className="mb-2 flex !w-full items-center justify-between border-b border-neutral-500 px-6 pb-4">
|
||||
<button
|
||||
<Button
|
||||
intent="tertiary"
|
||||
size="compacted"
|
||||
onClick={decreaseMonth}
|
||||
disabled={prevMonthButtonDisabled}
|
||||
className={chevronClasses.chevronRoot}
|
||||
@@ -34,9 +38,10 @@ export const DatePickerCustomHeader = ({
|
||||
prevMonthButtonDisabled && chevronClasses.chevronClassDisabled,
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
</Button>
|
||||
{format(date, "LLLL yyyy", { locale: locale === "fr" ? fr : enGB })}
|
||||
<button
|
||||
<Button
|
||||
intent="tertiary"
|
||||
onClick={increaseMonth}
|
||||
disabled={nextMonthButtonDisabled}
|
||||
className={chevronClasses.chevronRoot}
|
||||
@@ -47,7 +52,7 @@ export const DatePickerCustomHeader = ({
|
||||
nextMonthButtonDisabled && chevronClasses.chevronClassDisabled,
|
||||
)}
|
||||
/>
|
||||
</button>
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { VariantProps, cva } from "class-variance-authority";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export const inputVariants = cva(
|
||||
[
|
||||
"flex w-full content-center border",
|
||||
"transition-all duration-200",
|
||||
"border-gray-300 bg-gray-50 text-gray-900 shadow-sm focus:border-primary-500 focus:ring-primary-500",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-primary-500 dark:focus:ring-primary-500",
|
||||
],
|
||||
{
|
||||
variants: {
|
||||
state: {
|
||||
disabled: ["dark:bg-gray-50"],
|
||||
|
||||
error: ["!border-orange-700 focus:!border-orange-700"],
|
||||
},
|
||||
size: {
|
||||
small: ["font-medium", "px-3 py-2", "rounded-lg"],
|
||||
medium: ["font-medium", "p-3", "rounded-lg"],
|
||||
},
|
||||
},
|
||||
defaultVariants: {
|
||||
size: "medium",
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
export type InputVariants = VariantProps<typeof inputVariants>;
|
||||
export type InputProps = Omit<React.ComponentProps<"input">, "size"> &
|
||||
Omit<InputVariants, "state"> & {
|
||||
hasError?: boolean;
|
||||
startIcon?: React.ReactNode;
|
||||
endIcon?: React.ReactNode;
|
||||
};
|
||||
|
||||
export const Input = ({
|
||||
className,
|
||||
hasError,
|
||||
startIcon,
|
||||
endIcon,
|
||||
|
||||
size,
|
||||
|
||||
disabled,
|
||||
required,
|
||||
...rest
|
||||
}: InputProps) => {
|
||||
return (
|
||||
<div className="relative">
|
||||
{startIcon && (
|
||||
<div className="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-900 dark:text-white">
|
||||
{startIcon}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<input
|
||||
aria-required={required}
|
||||
aria-invalid={hasError}
|
||||
disabled={disabled}
|
||||
className={twMerge(
|
||||
inputVariants({
|
||||
size,
|
||||
state: hasError ? "error" : disabled ? "disabled" : undefined,
|
||||
}),
|
||||
startIcon && "pl-10",
|
||||
endIcon && "pr-10",
|
||||
className,
|
||||
)}
|
||||
{...rest}
|
||||
/>
|
||||
|
||||
{endIcon && (
|
||||
<div className="absolute inset-y-0 right-0 flex items-center pr-3 text-gray-900 dark:text-white">
|
||||
{endIcon}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -13,49 +13,57 @@ export type BaseOption<T = string, D = unknown> = {
|
||||
data?: D;
|
||||
};
|
||||
|
||||
export const classNames = <Option, IsMulti extends boolean = false>(
|
||||
hasError: boolean,
|
||||
separators: boolean,
|
||||
): ClassNamesConfig<Option, IsMulti, GroupBase<Option>> => ({
|
||||
container: () => "w-full",
|
||||
valueContainer: () => "text-gray-900 dark:text-white",
|
||||
indicatorsContainer: () => "flex items-center self-stretch shrink-0",
|
||||
clearIndicator: () => "flex items-center pr-2 text-gray-900 dark:text-white",
|
||||
dropdownIndicator: () =>
|
||||
"pointer-events-none flex items-center pr-2 text-gray-900 dark:text-white",
|
||||
indicatorSeparator: () => "w-px text-gray-900 dark:text-white",
|
||||
control: (state) =>
|
||||
twMerge(
|
||||
"group flex w-full items-center justify-between rounded-lg border p-2.5",
|
||||
"border-gray-300 bg-gray-50 text-gray-900 focus-within:border-primary-500 focus-within:ring-primary-500",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus-within:border-primary-500 dark:focus-within:ring-primary-500",
|
||||
export const createSelectClassNamesFn =
|
||||
<Option, IsMulti extends boolean = false>({
|
||||
size,
|
||||
}: {
|
||||
size?: "small" | "medium";
|
||||
}) =>
|
||||
(
|
||||
hasError: boolean,
|
||||
separators: boolean,
|
||||
): ClassNamesConfig<Option, IsMulti, GroupBase<Option>> => ({
|
||||
container: () => "w-full",
|
||||
valueContainer: () => "text-gray-900 dark:text-white",
|
||||
indicatorsContainer: () => "flex items-center self-stretch shrink-0",
|
||||
clearIndicator: () =>
|
||||
"flex items-center pr-2 text-gray-900 dark:text-white",
|
||||
dropdownIndicator: () =>
|
||||
"pointer-events-none flex items-center pr-2 text-gray-900 dark:text-white",
|
||||
indicatorSeparator: () => "w-px text-gray-900 dark:text-white",
|
||||
control: (state) =>
|
||||
twMerge(
|
||||
"group flex w-full items-center justify-between rounded-lg border p-3",
|
||||
"border-gray-300 bg-gray-50 text-gray-900 focus-within:border-primary-500 focus-within:ring-primary-500",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus-within:border-primary-500 dark:focus-within:ring-primary-500",
|
||||
|
||||
hasError && "ring-1 ring-error",
|
||||
state.isDisabled && "cursor-not-allowed",
|
||||
state.isFocused && "border-primary ring-1 ring-primary ring-opacity-50",
|
||||
),
|
||||
multiValue: () => "bg-fieldGray border rounded-lg flex space-x-1 pl-1 m-1",
|
||||
multiValueLabel: () => "",
|
||||
multiValueRemove: () => "items-center px-1 hover:text-primary",
|
||||
placeholder: () =>
|
||||
"block truncate pr-2 placeholder-gray dark:placeholder-gray-400",
|
||||
menu: () =>
|
||||
twMerge(
|
||||
"!z-30 mt-2 rounded-lg border border-gray-200 bg-white p-1",
|
||||
"border-gray-300 bg-gray-50 text-gray-900",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white",
|
||||
),
|
||||
groupHeading: () => "mx-3 mt-2 mb-1 text-textGray text-sm uppercase",
|
||||
option: ({ isFocused, isDisabled }) =>
|
||||
twMerge(
|
||||
"px-3 py-2 hover:cursor-pointer",
|
||||
separators && "border-b border-gray-200",
|
||||
isDisabled && "opacity-50",
|
||||
isFocused && "hover:bg-primary-500 hover:text-white",
|
||||
),
|
||||
noOptionsMessage: () =>
|
||||
"text-textGray p-2 bg-gray-50 border border-dashed border-gray-200 rounded-sm",
|
||||
});
|
||||
hasError && "ring-1 ring-error",
|
||||
state.isDisabled && "cursor-not-allowed",
|
||||
state.isFocused && "border-primary ring-1 ring-primary ring-opacity-50",
|
||||
size === "small" && "p-2",
|
||||
),
|
||||
multiValue: () => "bg-fieldGray border rounded-lg flex space-x-1 pl-1 m-1",
|
||||
multiValueLabel: () => "",
|
||||
multiValueRemove: () => "items-center px-1 hover:text-primary",
|
||||
placeholder: () =>
|
||||
"block truncate pr-2 placeholder-gray dark:placeholder-gray-400",
|
||||
menu: () =>
|
||||
twMerge(
|
||||
"!z-30 mt-2 rounded-lg border border-gray-200 bg-white p-1",
|
||||
"border-gray-300 bg-gray-50 text-gray-900",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white",
|
||||
),
|
||||
groupHeading: () => "mx-3 mt-2 mb-1 text-textGray text-sm uppercase",
|
||||
option: ({ isFocused, isDisabled }) =>
|
||||
twMerge(
|
||||
"px-3 py-2 hover:cursor-pointer",
|
||||
separators && "border-b border-gray-200",
|
||||
isDisabled && "opacity-50",
|
||||
isFocused && "hover:bg-primary-500 hover:text-white",
|
||||
),
|
||||
noOptionsMessage: () =>
|
||||
"text-textGray p-2 bg-gray-50 border border-dashed border-gray-200 rounded-sm",
|
||||
});
|
||||
|
||||
export type SelectProps<
|
||||
IsMulti extends boolean = false,
|
||||
@@ -66,6 +74,7 @@ export type SelectProps<
|
||||
required?: boolean;
|
||||
separators?: boolean;
|
||||
hasError?: boolean;
|
||||
size?: "small" | "medium";
|
||||
classNameOverrides?: ClassNamesConfig<
|
||||
BaseOption<T, D>,
|
||||
IsMulti,
|
||||
@@ -81,6 +90,7 @@ export const Select = <
|
||||
>({
|
||||
className,
|
||||
required,
|
||||
size,
|
||||
|
||||
placeholder,
|
||||
separators,
|
||||
@@ -90,6 +100,11 @@ export const Select = <
|
||||
...selectProps
|
||||
}: SelectProps<IsMulti, T, D>) => {
|
||||
const t = useTranslations("App");
|
||||
|
||||
const classNames = createSelectClassNamesFn<BaseOption<T, D>, IsMulti>({
|
||||
size,
|
||||
});
|
||||
|
||||
return (
|
||||
<ReactSelect
|
||||
options={options}
|
||||
@@ -105,10 +120,7 @@ export const Select = <
|
||||
}),
|
||||
}}
|
||||
classNames={{
|
||||
...classNames<BaseOption<T, D>, IsMulti>(
|
||||
hasError ?? false,
|
||||
separators ?? false,
|
||||
),
|
||||
...classNames(hasError ?? false, separators ?? false),
|
||||
...(classNameOverrides ?? {}),
|
||||
}}
|
||||
{...selectProps}
|
||||
|
||||
@@ -12,6 +12,7 @@ import { twMerge } from "tailwind-merge";
|
||||
import { Prettify } from "@/types";
|
||||
|
||||
import { Field, GenericFieldProps } from "./Field";
|
||||
import { inputVariants } from "./Input";
|
||||
|
||||
type TextAreaFieldProps<
|
||||
TFieldValues extends FieldValues = FieldValues,
|
||||
@@ -72,33 +73,25 @@ export const TextAreaField = <
|
||||
control={control}
|
||||
name={name}
|
||||
render={({ field }) => (
|
||||
<div className="relative">
|
||||
{startIcon && (
|
||||
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
{startIcon}
|
||||
</div>
|
||||
<textarea
|
||||
{...form.register(name, {
|
||||
required,
|
||||
})}
|
||||
aria-required={required}
|
||||
aria-invalid={hasError}
|
||||
disabled={disabled}
|
||||
className={twMerge(
|
||||
inputVariants({
|
||||
state: hasError ? "error" : disabled ? "disabled" : undefined,
|
||||
}),
|
||||
className,
|
||||
)}
|
||||
<textarea
|
||||
{...form.register(name, {
|
||||
required,
|
||||
})}
|
||||
aria-required={required}
|
||||
aria-invalid={hasError}
|
||||
disabled={disabled}
|
||||
className={twMerge(
|
||||
"flex w-full content-center rounded-lg border p-3",
|
||||
"border-gray-300 bg-gray-50 text-gray-900 shadow-sm focus:border-primary-500 focus:ring-primary-500",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-primary-500 dark:focus:ring-primary-500",
|
||||
hasError && "!border-orange-700 focus:!border-orange-700",
|
||||
disabled && "dark:bg-gray-50",
|
||||
)}
|
||||
{...rest}
|
||||
onChange={(e) => {
|
||||
field.onChange(e);
|
||||
}}
|
||||
value={field.value}
|
||||
/>
|
||||
</div>
|
||||
{...rest}
|
||||
onChange={(e) => {
|
||||
field.onChange(e);
|
||||
}}
|
||||
value={field.value}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -12,13 +12,14 @@ import { twMerge } from "tailwind-merge";
|
||||
import { Prettify } from "@/types";
|
||||
|
||||
import { Field, GenericFieldProps } from "./Field";
|
||||
import { Input, InputProps } from "./Input";
|
||||
|
||||
type TextFieldProps<
|
||||
TFieldValues extends FieldValues = FieldValues,
|
||||
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
||||
> = Prettify<
|
||||
GenericFieldProps<TFieldValues, TFieldName> &
|
||||
Omit<React.HTMLProps<HTMLInputElement>, "ref" | "name"> & {
|
||||
Omit<InputProps, "ref" | "name" | "size"> & {
|
||||
handleChanged?: (props: { name: string }) => void;
|
||||
startIcon?: React.ReactNode;
|
||||
}
|
||||
@@ -89,39 +90,26 @@ export const TextField = <
|
||||
control={control}
|
||||
name={name}
|
||||
render={({ field }) => (
|
||||
<div className="relative">
|
||||
{startIcon && (
|
||||
<div className="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
{startIcon}
|
||||
</div>
|
||||
)}
|
||||
<input
|
||||
type={type}
|
||||
{...form.register(name, {
|
||||
valueAsNumber: type === "number",
|
||||
required,
|
||||
})}
|
||||
aria-required={required}
|
||||
aria-invalid={hasError}
|
||||
disabled={disabled}
|
||||
className={twMerge(
|
||||
"flex w-full content-center rounded-lg border p-3",
|
||||
"border-gray-300 bg-gray-50 text-gray-900 shadow-sm focus:border-primary-500 focus:ring-primary-500",
|
||||
"dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-primary-500 dark:focus:ring-primary-500",
|
||||
hasError && "!border-orange-700 focus:!border-orange-700",
|
||||
disabled && "dark:bg-gray-50",
|
||||
startIcon && "pl-10",
|
||||
)}
|
||||
{...rest}
|
||||
onChange={(e) => {
|
||||
field.onChange(output(e));
|
||||
}}
|
||||
value={input(field.value)}
|
||||
onBlur={() => {
|
||||
if (handleChanged) handleChanged({ name });
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<Input
|
||||
type={type}
|
||||
{...form.register(name, {
|
||||
valueAsNumber: type === "number",
|
||||
required,
|
||||
})}
|
||||
hasError={hasError}
|
||||
required={required}
|
||||
disabled={disabled}
|
||||
startIcon={startIcon}
|
||||
className={twMerge(startIcon && "pl-10")}
|
||||
{...rest}
|
||||
onChange={(e) => {
|
||||
field.onChange(output(e));
|
||||
}}
|
||||
value={input(field.value)}
|
||||
onBlur={() => {
|
||||
if (handleChanged) handleChanged({ name });
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user