|
| 1 | +import React, { memo, forwardRef, ReactNode, useId } from "react"; |
| 2 | +import type { InputHTMLAttributes, TextareaHTMLAttributes } from "react"; |
| 3 | +import { symToStr } from "tsafe/symToStr"; |
| 4 | +import { assert } from "tsafe/assert"; |
| 5 | +import type { Equals } from "tsafe"; |
| 6 | +import { fr } from "./fr"; |
| 7 | +import { cx } from "./tools/cx"; |
| 8 | +import type { FrIconClassName, RiIconClassName } from "./fr/generatedFromCss/classNames"; |
| 9 | + |
| 10 | +export type InputProps = InputProps.Input | InputProps.TextArea; |
| 11 | + |
| 12 | +export namespace InputProps { |
| 13 | + export type Common = { |
| 14 | + className?: string; |
| 15 | + label: ReactNode; |
| 16 | + hintText?: ReactNode; |
| 17 | + /** default: false */ |
| 18 | + disabled?: boolean; |
| 19 | + message?: { |
| 20 | + type: "success" | "error"; |
| 21 | + text: ReactNode; |
| 22 | + }; |
| 23 | + iconId?: FrIconClassName | RiIconClassName; |
| 24 | + classes?: Partial< |
| 25 | + Record<"root" | "label" | "description" | "nativeInputOrTextArea" | "message", string> |
| 26 | + >; |
| 27 | + }; |
| 28 | + |
| 29 | + export type Input = Common & { |
| 30 | + /** Default: false */ |
| 31 | + isTextArea?: false; |
| 32 | + /** Props forwarded to the underlying <input /> element */ |
| 33 | + nativeInputProps?: InputHTMLAttributes<HTMLInputElement>; |
| 34 | + nativeTextAreaProps?: never; |
| 35 | + }; |
| 36 | + |
| 37 | + export type TextArea = Common & { |
| 38 | + /** Default: false */ |
| 39 | + isTextArea: true; |
| 40 | + nativeInputProps?: never; |
| 41 | + /** Props forwarded to the underlying <textarea /> element */ |
| 42 | + nativeTextAreaProps?: TextareaHTMLAttributes<HTMLTextAreaElement>; |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-highlight> |
| 48 | + * */ |
| 49 | +export const Input = memo( |
| 50 | + forwardRef<HTMLDivElement, InputProps>((props, ref) => { |
| 51 | + const { |
| 52 | + className, |
| 53 | + label, |
| 54 | + hintText, |
| 55 | + disabled = false, |
| 56 | + message, |
| 57 | + iconId: iconId_props, |
| 58 | + isTextArea = false, |
| 59 | + nativeInputProps = {}, |
| 60 | + nativeTextAreaProps = {}, |
| 61 | + classes = {}, |
| 62 | + ...rest |
| 63 | + } = props; |
| 64 | + |
| 65 | + const nativeInputOrTextAreaProps = isTextArea ? nativeTextAreaProps : nativeInputProps; |
| 66 | + |
| 67 | + const NativeInputOrTexArea = isTextArea ? "textarea" : "input"; |
| 68 | + |
| 69 | + assert<Equals<keyof typeof rest, never>>(); |
| 70 | + |
| 71 | + const inputId = (function useClosure() { |
| 72 | + const id = useId(); |
| 73 | + |
| 74 | + return nativeInputOrTextAreaProps.id ?? `input-${id}`; |
| 75 | + })(); |
| 76 | + |
| 77 | + const messageId = `${inputId}-desc-error`; |
| 78 | + |
| 79 | + return ( |
| 80 | + <div |
| 81 | + className={cx( |
| 82 | + fr.cx( |
| 83 | + "fr-input-group", |
| 84 | + disabled && "fr-input-group--disabled", |
| 85 | + message !== undefined && |
| 86 | + (() => { |
| 87 | + switch (message.type) { |
| 88 | + case "error": |
| 89 | + return "fr-input-group--error"; |
| 90 | + case "success": |
| 91 | + return "fr-input-group--valid"; |
| 92 | + } |
| 93 | + assert<Equals<typeof message.type, never>>(false); |
| 94 | + })() |
| 95 | + ), |
| 96 | + classes.root, |
| 97 | + className |
| 98 | + )} |
| 99 | + ref={ref} |
| 100 | + {...rest} |
| 101 | + > |
| 102 | + <label className={cx(fr.cx("fr-label"), classes.label)} htmlFor={inputId}> |
| 103 | + {label} |
| 104 | + {hintText !== undefined && <span className="fr-hint-text">{hintText}</span>} |
| 105 | + </label> |
| 106 | + {(() => { |
| 107 | + const nativeInputOrTextArea = ( |
| 108 | + <NativeInputOrTexArea |
| 109 | + {...(nativeInputOrTextAreaProps as {})} |
| 110 | + className={cx( |
| 111 | + fr.cx( |
| 112 | + "fr-input", |
| 113 | + message !== undefined && |
| 114 | + (() => { |
| 115 | + switch (message.type) { |
| 116 | + case "error": |
| 117 | + return "fr-input--error"; |
| 118 | + case "success": |
| 119 | + return "fr-input--valid"; |
| 120 | + } |
| 121 | + assert<Equals<typeof message.type, never>>(false); |
| 122 | + })() |
| 123 | + ), |
| 124 | + classes.nativeInputOrTextArea |
| 125 | + )} |
| 126 | + disabled={disabled || undefined} |
| 127 | + aria-describedby={messageId} |
| 128 | + type={isTextArea ? undefined : nativeInputProps.type ?? "text"} |
| 129 | + id={inputId} |
| 130 | + /> |
| 131 | + ); |
| 132 | + |
| 133 | + const iconId = |
| 134 | + iconId_props ?? |
| 135 | + (!isTextArea && nativeInputProps.type === "date" |
| 136 | + ? "ri-calendar-line" |
| 137 | + : undefined); |
| 138 | + |
| 139 | + return iconId === undefined ? ( |
| 140 | + nativeInputOrTextArea |
| 141 | + ) : ( |
| 142 | + <div className={fr.cx("fr-input-wrap", iconId)}> |
| 143 | + {nativeInputOrTextArea} |
| 144 | + </div> |
| 145 | + ); |
| 146 | + })()} |
| 147 | + {message !== undefined && ( |
| 148 | + <p id={messageId} className={cx(fr.cx("fr-error-text"), classes.message)}> |
| 149 | + {message.text} |
| 150 | + </p> |
| 151 | + )} |
| 152 | + </div> |
| 153 | + ); |
| 154 | + }) |
| 155 | +); |
| 156 | + |
| 157 | +Input.displayName = symToStr({ Input }); |
| 158 | + |
| 159 | +export default Input; |
0 commit comments