|
1 | 1 | "use client"; |
2 | 2 |
|
3 | | -import React, { memo, forwardRef, ReactNode, useId, type CSSProperties } from "react"; |
| 3 | +import React, { |
| 4 | + memo, |
| 5 | + forwardRef, |
| 6 | + type ReactNode, |
| 7 | + useId, |
| 8 | + type CSSProperties, |
| 9 | + type ForwardedRef, |
| 10 | + type DetailedHTMLProps, |
| 11 | + type SelectHTMLAttributes, |
| 12 | + type ChangeEvent |
| 13 | +} from "react"; |
4 | 14 | import { symToStr } from "tsafe/symToStr"; |
5 | 15 | import { assert } from "tsafe/assert"; |
6 | 16 | import type { Equals } from "tsafe"; |
7 | 17 | import { fr } from "./fr"; |
8 | 18 | import { cx } from "./tools/cx"; |
| 19 | +import type { FrClassName } from "./fr/generatedFromCss/classNames"; |
9 | 20 |
|
10 | | -export type SelectProps = { |
| 21 | +export type SelectProps<Options extends SelectProps.Option[]> = { |
| 22 | + options: Options; |
11 | 23 | className?: string; |
12 | 24 | label: ReactNode; |
13 | 25 | hint?: ReactNode; |
14 | | - nativeSelectProps: React.DetailedHTMLProps< |
15 | | - React.SelectHTMLAttributes<HTMLSelectElement>, |
16 | | - HTMLSelectElement |
17 | | - >; |
18 | | - children: ReactNode; |
| 26 | + nativeSelectProps?: Omit< |
| 27 | + DetailedHTMLProps<SelectHTMLAttributes<HTMLSelectElement>, HTMLSelectElement>, |
| 28 | + "value" | "defaultValue" | "onChange" |
| 29 | + > & { |
| 30 | + // Overriding the type of value and defaultValue to only accept the value type of the options |
| 31 | + value?: Options[number]["value"]; |
| 32 | + defaultValue?: Options[number]["value"]; |
| 33 | + onChange?: ( |
| 34 | + e: Omit<ChangeEvent<HTMLSelectElement>, "target" | "currentTarget"> & { |
| 35 | + target: Omit<ChangeEvent<HTMLSelectElement>, "value"> & { |
| 36 | + value: Options[number]["value"]; |
| 37 | + }; |
| 38 | + currentTarget: Omit<ChangeEvent<HTMLSelectElement>, "value"> & { |
| 39 | + value: Options[number]["value"]; |
| 40 | + }; |
| 41 | + } |
| 42 | + ) => void; |
| 43 | + }; |
19 | 44 | /** Default: false */ |
20 | 45 | disabled?: boolean; |
21 | 46 | /** Default: "default" */ |
22 | | - state?: "success" | "error" | "default"; |
| 47 | + state?: SelectProps.State | "default"; |
23 | 48 | /** The message won't be displayed if state is "default" */ |
24 | 49 | stateRelatedMessage?: ReactNode; |
25 | 50 | style?: CSSProperties; |
| 51 | + placeholder?: string; |
26 | 52 | }; |
27 | 53 |
|
| 54 | +export namespace SelectProps { |
| 55 | + export type Option<T extends string = string> = { |
| 56 | + value: T; |
| 57 | + label: string; |
| 58 | + disabled?: boolean; |
| 59 | + hidden?: boolean; |
| 60 | + selected?: boolean; |
| 61 | + }; |
| 62 | + |
| 63 | + type ExtractState<FrClassName> = FrClassName extends `fr-select-group--${infer State}` |
| 64 | + ? Exclude<State, "disabled"> |
| 65 | + : never; |
| 66 | + |
| 67 | + export type State = ExtractState<FrClassName>; |
| 68 | +} |
| 69 | + |
28 | 70 | /** |
29 | 71 | * @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-select> |
30 | 72 | * */ |
31 | | -export const Select = memo( |
32 | | - forwardRef<HTMLDivElement, SelectProps>((props, ref) => { |
33 | | - const { |
34 | | - className, |
35 | | - label, |
36 | | - hint, |
37 | | - nativeSelectProps, |
38 | | - disabled = false, |
39 | | - children, |
40 | | - state = "default", |
41 | | - stateRelatedMessage, |
42 | | - style, |
43 | | - ...rest |
44 | | - } = props; |
| 73 | +function NonMemoizedNonForwardedSelect<T extends SelectProps.Option[]>( |
| 74 | + props: SelectProps<T>, |
| 75 | + ref: React.LegacyRef<HTMLDivElement> |
| 76 | +) { |
| 77 | + const { |
| 78 | + className, |
| 79 | + label, |
| 80 | + hint, |
| 81 | + nativeSelectProps, |
| 82 | + disabled = false, |
| 83 | + options, |
| 84 | + state = "default", |
| 85 | + stateRelatedMessage, |
| 86 | + placeholder, |
| 87 | + style, |
| 88 | + ...rest |
| 89 | + } = props; |
45 | 90 |
|
46 | | - assert<Equals<keyof typeof rest, never>>(); |
| 91 | + assert<Equals<keyof typeof rest, never>>(); |
47 | 92 |
|
48 | | - const selectId = `select-${useId()}`; |
49 | | - const stateDescriptionId = `select-${useId()}-desc`; |
| 93 | + const { selectId, stateDescriptionId } = (function useClosure() { |
| 94 | + const selectIdExplicitlyProvided = nativeSelectProps?.id; |
| 95 | + const elementId = useId(); |
| 96 | + const selectId = selectIdExplicitlyProvided ?? `select-${elementId}`; |
| 97 | + const stateDescriptionId = |
| 98 | + selectIdExplicitlyProvided !== undefined |
| 99 | + ? `${selectIdExplicitlyProvided}-desc` |
| 100 | + : `select-${elementId}-desc`; |
50 | 101 |
|
51 | | - return ( |
52 | | - <div |
53 | | - className={cx( |
54 | | - fr.cx( |
55 | | - "fr-select-group", |
56 | | - disabled && "fr-select-group--disabled", |
57 | | - (() => { |
58 | | - switch (state) { |
59 | | - case "error": |
60 | | - return "fr-select-group--error"; |
61 | | - case "success": |
62 | | - return "fr-select-group--valid"; |
63 | | - case "default": |
64 | | - return undefined; |
65 | | - } |
66 | | - assert<Equals<typeof state, never>>(false); |
67 | | - })() |
68 | | - ), |
69 | | - className |
70 | | - )} |
71 | | - ref={ref} |
72 | | - style={style} |
73 | | - {...rest} |
| 102 | + return { selectId, stateDescriptionId }; |
| 103 | + })(); |
| 104 | + |
| 105 | + return ( |
| 106 | + <div |
| 107 | + className={cx( |
| 108 | + fr.cx( |
| 109 | + "fr-select-group", |
| 110 | + disabled && "fr-select-group--disabled", |
| 111 | + state !== "default" && `fr-select-group--${state}` |
| 112 | + ), |
| 113 | + className |
| 114 | + )} |
| 115 | + ref={ref} |
| 116 | + style={style} |
| 117 | + {...rest} |
| 118 | + > |
| 119 | + <label className={fr.cx("fr-label")} htmlFor={selectId}> |
| 120 | + {label} |
| 121 | + {hint !== undefined && <span className={fr.cx("fr-hint-text")}>{hint}</span>} |
| 122 | + </label> |
| 123 | + <select |
| 124 | + {...(nativeSelectProps as any)} |
| 125 | + className={cx(fr.cx("fr-select"), nativeSelectProps?.className)} |
| 126 | + id={selectId} |
| 127 | + aria-describedby={stateDescriptionId} |
| 128 | + disabled={disabled} |
74 | 129 | > |
75 | | - <label className={fr.cx("fr-label")} htmlFor={selectId}> |
76 | | - {label} |
77 | | - {hint !== undefined && <span className={fr.cx("fr-hint-text")}>{hint}</span>} |
78 | | - </label> |
79 | | - <select |
80 | | - {...nativeSelectProps} |
81 | | - className={cx(fr.cx("fr-select"), nativeSelectProps.className)} |
82 | | - id={selectId} |
83 | | - aria-describedby={stateDescriptionId} |
84 | | - disabled={disabled} |
85 | | - > |
86 | | - {children} |
87 | | - </select> |
88 | | - {state !== "default" && ( |
89 | | - <p |
90 | | - id={stateDescriptionId} |
91 | | - className={fr.cx( |
92 | | - (() => { |
93 | | - switch (state) { |
94 | | - case "error": |
95 | | - return "fr-error-text"; |
96 | | - case "success": |
97 | | - return "fr-valid-text"; |
98 | | - } |
99 | | - assert<Equals<typeof state, never>>(false); |
100 | | - })() |
101 | | - )} |
102 | | - > |
103 | | - {stateRelatedMessage} |
104 | | - </p> |
105 | | - )} |
106 | | - </div> |
107 | | - ); |
108 | | - }) |
109 | | -); |
| 130 | + {[ |
| 131 | + ...(placeholder === undefined |
| 132 | + ? [] |
| 133 | + : [ |
| 134 | + { |
| 135 | + label: placeholder, |
| 136 | + selected: true, |
| 137 | + value: "", |
| 138 | + disabled: true |
| 139 | + } |
| 140 | + ]), |
| 141 | + ...options |
| 142 | + ].map((option, index) => ( |
| 143 | + <option {...option} key={`${option.value}-${index}`}> |
| 144 | + {option.label} |
| 145 | + </option> |
| 146 | + ))} |
| 147 | + </select> |
| 148 | + {state !== "default" && ( |
| 149 | + <p id={stateDescriptionId} className={fr.cx(`fr-${state}-text`)}> |
| 150 | + {stateRelatedMessage} |
| 151 | + </p> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export const Select = memo(forwardRef(NonMemoizedNonForwardedSelect)) as < |
| 158 | + T extends SelectProps.Option[] |
| 159 | +>( |
| 160 | + props: SelectProps<T> & { ref?: ForwardedRef<HTMLDivElement> } |
| 161 | +) => ReturnType<typeof NonMemoizedNonForwardedSelect>; |
110 | 162 |
|
111 | | -Select.displayName = symToStr({ Select }); |
| 163 | +(Select as any).displayName = symToStr({ Select }); |
112 | 164 |
|
113 | 165 | export default Select; |
0 commit comments