(
h: { readonly input: InputElement<Msg> },
options: {
/** The caller's `h.input`, to stack a part on it. `h.input` when left out. */
readonly element?: InputElement<Msg, R> | undefined
readonly props: InputProps
/** The message an edit sends, from the value the user left in the field. */
readonly onUpdate?:
| ((event: { readonly value: string }) => NoInfer<Msg> | undefined)
| undefined
}
)
| 142 | * @since 4.2.0 |
| 143 | */ |
| 144 | export const Input = <Msg, R = never>( |
| 145 | h: { readonly input: InputElement<Msg> }, |
| 146 | options: { |
| 147 | /** The caller's `h.input`, to stack a part on it. `h.input` when left out. */ |
| 148 | readonly element?: InputElement<Msg, R> | undefined |
| 149 | readonly props: InputProps |
| 150 | /** The message an edit sends, from the value the user left in the field. */ |
| 151 | readonly onUpdate?: |
| 152 | | ((event: { readonly value: string }) => NoInfer<Msg> | undefined) |
| 153 | | undefined |
| 154 | } |
| 155 | ): InputElement<Msg, R> => { |
| 156 | const element: InputElement<Msg, R> = options.element ?? h.input |
| 157 | const { onUpdate, props } = options |
| 158 | const disabled = props.disabled === true |
| 159 | // The writes wait for the finished input, so they come after whatever |
| 160 | // the caller chains on it, and the check sees an input handler chained |
| 161 | // after the call. |
| 162 | return element.forward((node) => { |
| 163 | // By `onUpdate` alone, not by the state: a disabled input sets no |
| 164 | // handler now, but the clash would only show once it was enabled. |
| 165 | if (onUpdate !== undefined && node.handlers.input !== undefined) { |
| 166 | throw new Error( |
| 167 | "Input: the element already handles input, which onUpdate would replace. Send the message from onUpdate" |
| 168 | ) |
| 169 | } |
| 170 | const input = node |
| 171 | .value(props.value) |
| 172 | .attrs({ |
| 173 | // Never set: the native `disabled` reports it. |
| 174 | "aria-disabled": undefined, |
| 175 | "data-disabled": flag(disabled) |
| 176 | }) |
| 177 | .disabled(disabled) |
| 178 | return !disabled && onUpdate !== undefined |
| 179 | ? input.on("input", ({ value }) => onUpdate({ value })) |
| 180 | : input |
| 181 | }) |
| 182 | } |
no test coverage detected