| 1 |
/* ************************************************************************ */ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/* ************************************************************************ */ |
| 22 |
|
| 23 |
/* eslint-disable @typescript-eslint/explicit-function-return-type */ |
| 24 |
|
| 25 |
/* --------------------------------------------------------------------------*/ |
| 26 |
/* --- Form Fields ---*/ |
| 27 |
/* --------------------------------------------------------------------------*/ |
| 28 |
|
| 29 |
/** |
| 30 |
Form Fields are made of states and fields, arranged into a form page. |
| 31 |
|
| 32 |
Field states are typically build with [[useState]] and [[useProperty]] |
| 33 |
hooks, also you can also build them manually. All the provided hooks |
| 34 |
can be used and composed with each other to build full feature states. |
| 35 |
|
| 36 |
Form fields shall be arranged into sections and form pages to obtain a |
| 37 |
wellformed layout. |
| 38 |
|
| 39 |
@packageDocumentation |
| 40 |
@module dome/layout/form |
| 41 |
*/ |
| 42 |
|
| 43 |
import { debounce } from 'lodash'; |
| 44 |
import React from 'react'; |
| 45 |
import * as Dome from 'dome'; |
| 46 |
import * as Utils from 'dome/misc/utils'; |
| 47 |
import { SVG } from 'dome/controls/icons'; |
| 48 |
import { Checkbox, Radio, Select as SelectMenu } from 'dome/controls/buttons'; |
| 49 |
|
| 50 |
export type FieldError = |
| 51 |
| undefined | boolean | string |
| 52 |
| { [key: string]: FieldError } | FieldError[]; |
| 53 |
export type Checker<A> = (value: A) => boolean | FieldError; |
| 54 |
export type Callback<A> = (value: A, error: FieldError) => void; |
| 55 |
export type FieldState<A> = [A, FieldError, Callback<A>]; |
| 56 |
|
| 57 |
/* --------------------------------------------------------------------------*/ |
| 58 |
/* --- State Errors Utilities ---*/ |
| 59 |
/* --------------------------------------------------------------------------*/ |
| 60 |
|
| 61 |
export function inRange( |
| 62 |
a: number, |
| 63 |
b: number, |
| 64 |
): Checker<number> { |
| 65 |
return (v: number) => (a <= v && v <= b); |
| 66 |
} |
| 67 |
|
| 68 |
export function validate<A>( |
| 69 |
value: A, |
| 70 |
checker: undefined | Checker<A>, |
| 71 |
): FieldError { |
| 72 |
if (checker) { |
| 73 |
try { |
| 74 |
const r = checker(value); |
| 75 |
if (r === undefined || r === true) return undefined; |
| 76 |
return r; |
| 77 |
} catch (err) { |
| 78 |
return `${err}`; |
| 79 |
} |
| 80 |
} |
| 81 |
return undefined; |
| 82 |
} |
| 83 |
|
| 84 |
export function isValid(err: FieldError): boolean { return !err; } |
| 85 |
|
| 86 |
type ObjectError = { [key: string]: FieldError }; |
| 87 |
|
| 88 |
function isObjectError(err: FieldError): err is ObjectError { |
| 89 |
return typeof err === 'object' && !Array.isArray(err); |
| 90 |
} |
| 91 |
|
| 92 |
function isArrayError(err: FieldError): err is FieldError[] { |
| 93 |
return Array.isArray(err); |
| 94 |
} |
| 95 |
|
| 96 |
function isValidObject(err: ObjectError): boolean { |
| 97 |
const ks = Object.keys(err); |
| 98 |
for (let k = 0; k < ks.length; k++) { |
| 99 |
if (!isValid(err[ks[k]])) return false; |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
function isValidArray(err: FieldError[]): boolean { |
| 105 |
for (let k = 0; k < err.length; k++) { |
| 106 |
if (!isValid(err[k])) return false; |
| 107 |
} |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/* --------------------------------------------------------------------------*/ |
| 112 |
/* --- State Hooks ---*/ |
| 113 |
/* --------------------------------------------------------------------------*/ |
| 114 |
|
| 115 |
/** Create a local field state, like `React.useState()` does. */ |
| 116 |
export function useState<A>( |
| 117 |
defaultValue: A, |
| 118 |
checker?: Checker<A>, |
| 119 |
onChange?: Callback<A>, |
| 120 |
): FieldState<A> { |
| 121 |
const [value, setValue] = React.useState<A>(defaultValue); |
| 122 |
const [error, setError] = React.useState<FieldError>(undefined); |
| 123 |
const setState = React.useCallback((newValue: A, newError: FieldError) => { |
| 124 |
const localError = validate(newValue, checker) || newError; |
| 125 |
setValue(newValue); |
| 126 |
setError(localError); |
| 127 |
if (onChange) onChange(newValue, localError); |
| 128 |
}, [checker, setValue, setError, onChange]); |
| 129 |
return [value, error, setState]; |
| 130 |
} |
| 131 |
|
| 132 |
/** Introduces a local state and propagates only non-errors. */ |
| 133 |
export function useValid<A>( |
| 134 |
state: [A, (newValue: A) => void], |
| 135 |
): FieldState<A> { |
| 136 |
const [value, setValue] = state; |
| 137 |
const [local, setLocal] = React.useState(value); |
| 138 |
const [error, setError] = React.useState<FieldError>(undefined); |
| 139 |
const update = React.useCallback( |
| 140 |
(newValue: A, newError: FieldError) => { |
| 141 |
setLocal(newValue); |
| 142 |
setError(newError); |
| 143 |
if (!newError) setValue(newValue); |
| 144 |
}, [setValue], |
| 145 |
); |
| 146 |
return [error ? local : value, error, update]; |
| 147 |
} |
| 148 |
|
| 149 |
/** Provides a new state with a default value. */ |
| 150 |
export function useDefault<A>( |
| 151 |
state: FieldState<A | undefined>, |
| 152 |
defaultValue: A, |
| 153 |
): FieldState<A> { |
| 154 |
const [value, error, setState] = state; |
| 155 |
return [value ?? defaultValue, error, setState]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
Coerces a state with defined value |
| 160 |
into some with possibly undefined one. |
| 161 |
*/ |
| 162 |
export function useDefined<A>( |
| 163 |
state: FieldState<A>, |
| 164 |
): FieldState<A | undefined> { |
| 165 |
const [value, error, setState] = state; |
| 166 |
const update = React.useCallback( |
| 167 |
(newValue: A | undefined, newError: FieldError) => { |
| 168 |
if (newValue !== undefined) { |
| 169 |
setState(newValue, newError); |
| 170 |
} |
| 171 |
}, [setState], |
| 172 |
); |
| 173 |
return [value, error, update]; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
Undefined value leads to an error. |
| 178 |
@param onError - error message in case of undefined or invalid updates. |
| 179 |
*/ |
| 180 |
export function useRequired<A>( |
| 181 |
state: FieldState<A>, |
| 182 |
onError?: string, |
| 183 |
): FieldState<A | undefined> { |
| 184 |
const [value, error, setState] = state; |
| 185 |
const cache = React.useRef(value); |
| 186 |
const update = React.useCallback( |
| 187 |
(newValue: A | undefined, newError: FieldError) => { |
| 188 |
if (newValue === undefined) { |
| 189 |
setState(cache.current, onError || 'Required field'); |
| 190 |
} else { |
| 191 |
setState(newValue, newError); |
| 192 |
} |
| 193 |
}, [cache, onError, setState], |
| 194 |
); |
| 195 |
return [value, error, update]; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
Enrich the state with a local checker. |
| 200 |
The local error, if any, has precedence over any error from updates. |
| 201 |
*/ |
| 202 |
export function useChecker<A>( |
| 203 |
state: FieldState<A>, |
| 204 |
checker?: Checker<A>, |
| 205 |
): FieldState<A> { |
| 206 |
const [value, error, setState] = state; |
| 207 |
const update = React.useCallback((newValue: A, newError: FieldError) => { |
| 208 |
const localError = validate(newValue, checker) || newError; |
| 209 |
setState(newValue, localError); |
| 210 |
}, [checker, setState]); |
| 211 |
return [value, error, update]; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
Transform a state `A` into a state `B` through converting functions. |
| 216 |
|
| 217 |
Input and output functions shall be the inverse with each others. |
| 218 |
|
| 219 |
In case an exception is raised during input conversion, state `B` |
| 220 |
retains its previous value (or default value) but forwards |
| 221 |
the translation error. |
| 222 |
|
| 223 |
In case an exception is raised during output conversion, a local state |
| 224 |
is maintained with the invalid `B` value until it is transformed into |
| 225 |
a valid one. |
| 226 |
|
| 227 |
@param input - converting function from `A` to `B` |
| 228 |
@param output - converting function from `B` to `A` |
| 229 |
*/ |
| 230 |
|
| 231 |
export function useFilter<A, B>( |
| 232 |
state: FieldState<A>, |
| 233 |
input: (value: A) => B, |
| 234 |
output: (value: B) => A, |
| 235 |
defaultValue: B, |
| 236 |
): FieldState<B> { |
| 237 |
|
| 238 |
const [value, error, setState] = state; |
| 239 |
const [localValue, setLocalValue] = React.useState(defaultValue); |
| 240 |
const [localError, setLocalError] = React.useState<FieldError>(undefined); |
| 241 |
const [dangling, setDangling] = React.useState(false); |
| 242 |
|
| 243 |
const update = React.useCallback( |
| 244 |
(newValue: B, newError: FieldError) => { |
| 245 |
try { |
| 246 |
const outValue = output(newValue); |
| 247 |
setLocalValue(newValue); |
| 248 |
setLocalError(newError); |
| 249 |
if (isValid(newError)) { |
| 250 |
setDangling(false); |
| 251 |
setState(outValue, undefined); |
| 252 |
} |
| 253 |
} catch (err) { |
| 254 |
setLocalValue(newValue); |
| 255 |
setLocalError(newError || err ? `${err}` : 'Invalid value'); |
| 256 |
setDangling(true); |
| 257 |
} |
| 258 |
}, [output, setState, setLocalValue, setLocalError], |
| 259 |
); |
| 260 |
|
| 261 |
if (dangling) { |
| 262 |
return [localValue, localError, update]; |
| 263 |
} |
| 264 |
try { |
| 265 |
return [input(value), error, update]; |
| 266 |
} catch (err) { |
| 267 |
return [localValue, err ? `${err}` : 'Invalid input', update]; |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
Introduces a latency between local changes and propagated ones. |
| 274 |
A transient local state is maintained during debounced updates, until |
| 275 |
the last update is finally flushed. |
| 276 |
*/ |
| 277 |
export function useLatency<A>( |
| 278 |
state: FieldState<A>, |
| 279 |
latency?: number, |
| 280 |
): FieldState<A> { |
| 281 |
const [value, error, setState] = state; |
| 282 |
const period = latency ?? 0; |
| 283 |
const [localValue, setLocalValue] = React.useState(value); |
| 284 |
const [localError, setLocalError] = React.useState(error); |
| 285 |
const [dangling, setDangling] = React.useState(false); |
| 286 |
const update = React.useMemo(() => { |
| 287 |
if (period > 0) { |
| 288 |
const propagate = debounce( |
| 289 |
(lateValue: A, lateError: FieldError) => { |
| 290 |
setState(lateValue, lateError); |
| 291 |
setDangling(false); |
| 292 |
}, period, |
| 293 |
); |
| 294 |
return (newValue: A, newError: FieldError) => { |
| 295 |
setLocalValue(newValue); |
| 296 |
setLocalError(newError); |
| 297 |
setDangling(true); |
| 298 |
propagate(newValue, newError); |
| 299 |
}; |
| 300 |
} |
| 301 |
setDangling(false); |
| 302 |
return setState; |
| 303 |
}, [period, setDangling, setState, setLocalValue, setLocalError]); |
| 304 |
return [ |
| 305 |
dangling ? localValue : value, |
| 306 |
dangling ? localError : error, |
| 307 |
update, |
| 308 |
]; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
Returns the state associated to a property of the input state. |
| 313 |
*/ |
| 314 |
export function useProperty<A, K extends keyof A>( |
| 315 |
state: FieldState<A>, |
| 316 |
property: K, |
| 317 |
checker?: Checker<A[K]>, |
| 318 |
): FieldState<A[K]> { |
| 319 |
const [value, error, setState] = state; |
| 320 |
const update = React.useCallback((newProp: A[K], newError: FieldError) => { |
| 321 |
const newValue = { ...value, [property]: newProp }; |
| 322 |
const objError = isObjectError(error) ? error : {}; |
| 323 |
const propError = validate(newProp, checker) || newError; |
| 324 |
const localError = { ...objError, [property]: propError }; |
| 325 |
setState(newValue, isValidObject(localError) ? undefined : localError); |
| 326 |
}, [value, error, setState, property, checker]); |
| 327 |
return [value[property], error, update]; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
Returns the state associated to an index element of the input state. |
| 332 |
*/ |
| 333 |
export function useIndex<A>( |
| 334 |
state: FieldState<A[]>, |
| 335 |
index: number, |
| 336 |
checker?: Checker<A>, |
| 337 |
): FieldState<A> { |
| 338 |
const [array, error, setState] = state; |
| 339 |
const update = React.useCallback((newValue: A, newError: FieldError) => { |
| 340 |
const newArray = array.slice(); |
| 341 |
newArray[index] = newValue; |
| 342 |
const localError = isArrayError(error) ? error.slice() : []; |
| 343 |
const valueError = validate(newValue, checker) || newError; |
| 344 |
localError[index] = valueError; |
| 345 |
setState(newArray, isValidArray(localError) ? undefined : localError); |
| 346 |
}, [array, error, setState, index, checker]); |
| 347 |
const itemError = isArrayError(error) ? error[index] : undefined; |
| 348 |
return [array[index], itemError, update]; |
| 349 |
} |
| 350 |
|
| 351 |
/* --------------------------------------------------------------------------*/ |
| 352 |
/* --- Form Filter Context ---*/ |
| 353 |
/* --------------------------------------------------------------------------*/ |
| 354 |
|
| 355 |
export interface FilterProps { |
| 356 |
/** default is false. */ |
| 357 |
hidden?: boolean; |
| 358 |
/** default is true. */ |
| 359 |
visible?: boolean; |
| 360 |
/** default is true. */ |
| 361 |
enabled?: boolean; |
| 362 |
/** default is false. */ |
| 363 |
disabled?: boolean; |
| 364 |
} |
| 365 |
|
| 366 |
export interface Children { children?: React.ReactNode } |
| 367 |
|
| 368 |
interface FormContext { |
| 369 |
disabled: boolean; |
| 370 |
hidden: boolean; |
| 371 |
} |
| 372 |
|
| 373 |
const CONTEXT = React.createContext<FormContext | undefined>(undefined); |
| 374 |
|
| 375 |
const HIDDEN = |
| 376 |
({ hidden = false, visible = true }: FilterProps) => hidden || !visible; |
| 377 |
|
| 378 |
const DISABLED = |
| 379 |
({ disabled = false, enabled = true }: FilterProps) => disabled || !enabled; |
| 380 |
|
| 381 |
function useContext(props?: FilterProps): FormContext { |
| 382 |
const Parent = React.useContext(CONTEXT); |
| 383 |
return { |
| 384 |
hidden: (props && HIDDEN(props)) || (Parent?.hidden ?? false), |
| 385 |
disabled: (props && DISABLED(props)) || (Parent?.disabled ?? false), |
| 386 |
}; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
Allow to locally disable or hide all its children fields. |
| 391 |
@category Form Containers |
| 392 |
*/ |
| 393 |
export function FormFilter(props: FilterProps & Children) { |
| 394 |
const context = useContext(props); |
| 395 |
if (context.hidden) return null; |
| 396 |
return ( |
| 397 |
<CONTEXT.Provider value={context}> |
| 398 |
{props.children} |
| 399 |
</CONTEXT.Provider> |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
/* --------------------------------------------------------------------------*/ |
| 404 |
/* --- Main Form Container ---*/ |
| 405 |
/* --------------------------------------------------------------------------*/ |
| 406 |
|
| 407 |
/** @category Form Containers */ |
| 408 |
export interface PageProps extends FilterProps, Children { |
| 409 |
/** Additional container class. */ |
| 410 |
className?: string; |
| 411 |
/** Additional container style. */ |
| 412 |
style?: React.CSSProperties; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
Main Form Container. |
| 417 |
@category Form Containers |
| 418 |
*/ |
| 419 |
export function Page(props: PageProps) { |
| 420 |
const { className, style, children, ...filter } = props; |
| 421 |
const { hidden, disabled } = useContext(filter); |
| 422 |
const css = Utils.classes(className ?? 'dome-xForm-grid'); |
| 423 |
if (hidden) return null; |
| 424 |
return ( |
| 425 |
<div className={css} style={style}> |
| 426 |
<CONTEXT.Provider value={{ hidden, disabled }}> |
| 427 |
{children} |
| 428 |
</CONTEXT.Provider> |
| 429 |
</div> |
| 430 |
); |
| 431 |
} |
| 432 |
|
| 433 |
// -------------------------------------------------------------------------- |
| 434 |
// --- Warning Badge |
| 435 |
// -------------------------------------------------------------------------- |
| 436 |
|
| 437 |
export interface WarningProps { |
| 438 |
/** Short warning message (displayed on hover). */ |
| 439 |
warning?: string; |
| 440 |
/** Error details (if a string is provided, in tooltip). */ |
| 441 |
error?: FieldError; |
| 442 |
/** Label offset. */ |
| 443 |
offset?: number; |
| 444 |
} |
| 445 |
|
| 446 |
/** Warning Badge. */ |
| 447 |
export function Warning(props: WarningProps) { |
| 448 |
const { warning, error, offset = 0 } = props; |
| 449 |
const DETAILS = typeof error === 'string' ? error : undefined; |
| 450 |
const WARNING = warning && ( |
| 451 |
<span className="dome-xForm-warning"> |
| 452 |
{warning} |
| 453 |
</span> |
| 454 |
); |
| 455 |
return ( |
| 456 |
<div |
| 457 |
className="dome-xIcon dome-xForm-error" |
| 458 |
style={{ top: offset - 2 }} |
| 459 |
title={DETAILS} |
| 460 |
> |
| 461 |
<SVG id="WARNING" size={11} /> |
| 462 |
{WARNING} |
| 463 |
</div> |
| 464 |
); |
| 465 |
} |
| 466 |
|
| 467 |
// -------------------------------------------------------------------------- |
| 468 |
// --- Block Container |
| 469 |
// -------------------------------------------------------------------------- |
| 470 |
|
| 471 |
/** |
| 472 |
Layout its contents inside a full-width container. |
| 473 |
@category Form Containers |
| 474 |
*/ |
| 475 |
export function FormBlock(props: FilterProps & Children) { |
| 476 |
const { children, ...filter } = props; |
| 477 |
return ( |
| 478 |
<FormFilter {...filter}> |
| 479 |
<div className="dome-xForm-block"> |
| 480 |
{children} |
| 481 |
</div> |
| 482 |
</FormFilter> |
| 483 |
); |
| 484 |
} |
| 485 |
|
| 486 |
// -------------------------------------------------------------------------- |
| 487 |
// --- Section Container |
| 488 |
// -------------------------------------------------------------------------- |
| 489 |
|
| 490 |
/** @category Form Fields */ |
| 491 |
export interface SectionProps extends FilterProps, Children { |
| 492 |
/** Section name. */ |
| 493 |
label: string; |
| 494 |
/** Tooltip text. */ |
| 495 |
title?: string; |
| 496 |
/** Warning Error (when unfolded). */ |
| 497 |
warning?: string; |
| 498 |
/** Associated Error. */ |
| 499 |
error?: FieldError; |
| 500 |
/** Fold/Unfold settings. */ |
| 501 |
settings?: string; |
| 502 |
/** Fold/Unfold state (defaults to false). */ |
| 503 |
unfold?: boolean; |
| 504 |
} |
| 505 |
|
| 506 |
/** @category Form Fields */ |
| 507 |
export function Section(props: SectionProps) { |
| 508 |
const { label, title, children, warning, error, ...filter } = props; |
| 509 |
const { disabled, hidden } = useContext(filter); |
| 510 |
const [unfold, flip] = Dome.useFlipSettings(props.settings, props.unfold); |
| 511 |
|
| 512 |
if (hidden) return null; |
| 513 |
|
| 514 |
const hasWarning = unfold && !disabled && error; |
| 515 |
|
| 516 |
const cssTitle = Utils.classes( |
| 517 |
'dome-text-title', |
| 518 |
disabled && 'dome-disabled', |
| 519 |
); |
| 520 |
|
| 521 |
return ( |
| 522 |
<CONTEXT.Provider value={{ hidden, disabled }}> |
| 523 |
<div className="dome-xForm-section"> |
| 524 |
<div className="dome-xForm-fold" onClick={flip}> |
| 525 |
<SVG id={unfold ? 'TRIANGLE.DOWN' : 'TRIANGLE.RIGHT'} size={11} /> |
| 526 |
</div> |
| 527 |
<label className={cssTitle} title={title}> |
| 528 |
{label} |
| 529 |
</label> |
| 530 |
{hasWarning && <Warning warning={warning} />} |
| 531 |
</div> |
| 532 |
{unfold && children} |
| 533 |
{unfold && <div className="dome-xForm-hsep" />} |
| 534 |
</CONTEXT.Provider> |
| 535 |
); |
| 536 |
} |
| 537 |
|
| 538 |
/* --------------------------------------------------------------------------*/ |
| 539 |
/* --- Generic Field --- */ |
| 540 |
/* --------------------------------------------------------------------------*/ |
| 541 |
|
| 542 |
/** @category Form Fields */ |
| 543 |
export interface GenericFieldProps extends FilterProps, Children { |
| 544 |
/** Field label. */ |
| 545 |
label: string; |
| 546 |
/** Field tooltip text. */ |
| 547 |
title?: string; |
| 548 |
/** Field offset. */ |
| 549 |
offset?: number; |
| 550 |
/** Html tag `<input />` element. */ |
| 551 |
htmlFor?: string; |
| 552 |
/** Warning message (in case of error). */ |
| 553 |
onError?: string; |
| 554 |
/** Error (if any). */ |
| 555 |
error?: FieldError; |
| 556 |
} |
| 557 |
|
| 558 |
let FIELDID = 0; |
| 559 |
|
| 560 |
/** Generates a unique, stable identifier. */ |
| 561 |
export function useHtmlFor() { |
| 562 |
return React.useMemo(() => `dome-field ${FIELDID++}`, []); |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
Generic Field. |
| 567 |
Layout its content in a top-left aligned box on the right of the label. |
| 568 |
@category Form Fields |
| 569 |
*/ |
| 570 |
export function Field(props: GenericFieldProps) { |
| 571 |
const { hidden, disabled } = useContext(props); |
| 572 |
|
| 573 |
if (hidden) return null; |
| 574 |
|
| 575 |
const { label, title, offset, htmlFor, children } = props; |
| 576 |
|
| 577 |
const cssLabel = Utils.classes( |
| 578 |
'dome-xForm-label dome-text-label', |
| 579 |
disabled && 'dome-disabled', |
| 580 |
); |
| 581 |
|
| 582 |
const cssField = Utils.classes( |
| 583 |
'dome-xForm-field dome-text-label', |
| 584 |
disabled && 'dome-disabled', |
| 585 |
); |
| 586 |
|
| 587 |
const { onError, error } = props; |
| 588 |
|
| 589 |
const WARNING = error ? ( |
| 590 |
<Warning offset={offset} warning={onError} error={error} /> |
| 591 |
) : null; |
| 592 |
|
| 593 |
return ( |
| 594 |
<> |
| 595 |
<label |
| 596 |
className={cssLabel} |
| 597 |
style={{ top: offset }} |
| 598 |
htmlFor={htmlFor} |
| 599 |
title={title} |
| 600 |
> |
| 601 |
{label} |
| 602 |
</label> |
| 603 |
<div className={cssField} title={title}> |
| 604 |
{children} |
| 605 |
{WARNING} |
| 606 |
</div> |
| 607 |
</> |
| 608 |
); |
| 609 |
|
| 610 |
} |
| 611 |
|
| 612 |
/* --------------------------------------------------------------------------*/ |
| 613 |
/* --- Input Fields ---*/ |
| 614 |
/* --------------------------------------------------------------------------*/ |
| 615 |
|
| 616 |
/** @category Form Fields */ |
| 617 |
export interface FieldProps<A> extends FilterProps { |
| 618 |
/** Field label. */ |
| 619 |
label: string; |
| 620 |
/** Field tooltip text. */ |
| 621 |
title?: string; |
| 622 |
/** Field state. */ |
| 623 |
state: FieldState<A>; |
| 624 |
/** Checker. */ |
| 625 |
checker?: Checker<A>; |
| 626 |
/** Alternative error message (in case of error). */ |
| 627 |
onError?: string; |
| 628 |
} |
| 629 |
|
| 630 |
type InputEvent = { target: { value: string } }; |
| 631 |
type InputState = [string, FieldError, (evt: InputEvent) => void]; |
| 632 |
|
| 633 |
function useChangeEvent(setState: Callback<string>) { |
| 634 |
return React.useCallback( |
| 635 |
(evt: InputEvent) => { setState(evt.target.value, undefined); }, |
| 636 |
[setState], |
| 637 |
); |
| 638 |
} |
| 639 |
|
| 640 |
/* --------------------------------------------------------------------------*/ |
| 641 |
/* --- Text Fields ---*/ |
| 642 |
/* --------------------------------------------------------------------------*/ |
| 643 |
|
| 644 |
/** @category Text Fields */ |
| 645 |
export interface TextFieldProps extends FieldProps<string | undefined> { |
| 646 |
placeholder?: string; |
| 647 |
className?: string; |
| 648 |
style?: React.CSSProperties; |
| 649 |
latency?: number; |
| 650 |
} |
| 651 |
|
| 652 |
function useTextInputField( |
| 653 |
props: TextFieldProps, |
| 654 |
defaultLatency: number, |
| 655 |
): InputState { |
| 656 |
const checked = useChecker(props.state, props.checker); |
| 657 |
const period = props.latency ?? defaultLatency; |
| 658 |
const [value, error, setState] = useLatency(checked, period); |
| 659 |
const onChange = useChangeEvent(setState); |
| 660 |
return [value || '', error, onChange]; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
Text Field. |
| 665 |
@category Text Fields |
| 666 |
*/ |
| 667 |
export function TextField(props: TextFieldProps) { |
| 668 |
const { disabled } = useContext(props); |
| 669 |
const id = useHtmlFor(); |
| 670 |
const css = Utils.classes('dome-xForm-text-field', props.className); |
| 671 |
const [value, error, onChange] = useTextInputField(props, 600); |
| 672 |
return ( |
| 673 |
<Field |
| 674 |
{...props} |
| 675 |
offset={4} |
| 676 |
htmlFor={id} |
| 677 |
error={error} |
| 678 |
> |
| 679 |
<input |
| 680 |
id={id} |
| 681 |
type="text" |
| 682 |
value={value} |
| 683 |
className={css} |
| 684 |
style={props.style} |
| 685 |
disabled={disabled} |
| 686 |
placeholder={props.placeholder} |
| 687 |
onChange={onChange} |
| 688 |
/> |
| 689 |
</Field> |
| 690 |
); |
| 691 |
} |
| 692 |
|
| 693 |
/** |
| 694 |
Monospaced Text Field. |
| 695 |
@category Text Fields |
| 696 |
*/ |
| 697 |
export function TextCodeField(props: TextFieldProps) { |
| 698 |
const { disabled } = useContext(props); |
| 699 |
const id = useHtmlFor(); |
| 700 |
const [value, error, onChange] = useTextInputField(props, 600); |
| 701 |
const css = Utils.classes( |
| 702 |
'dome-xForm-text-field', |
| 703 |
'dome-text-code', |
| 704 |
props.className, |
| 705 |
); |
| 706 |
return ( |
| 707 |
<Field |
| 708 |
{...props} |
| 709 |
offset={4} |
| 710 |
htmlFor={id} |
| 711 |
error={error} |
| 712 |
> |
| 713 |
<input |
| 714 |
id={id} |
| 715 |
type="text" |
| 716 |
value={value} |
| 717 |
className={css} |
| 718 |
style={props.style} |
| 719 |
disabled={disabled} |
| 720 |
placeholder={props.placeholder} |
| 721 |
onChange={onChange} |
| 722 |
/> |
| 723 |
</Field> |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
/* --------------------------------------------------------------------------*/ |
| 728 |
/* --- Text Area Fields ---*/ |
| 729 |
/* --------------------------------------------------------------------------*/ |
| 730 |
|
| 731 |
/** @category Text Fields */ |
| 732 |
export interface TextFieldAreaProps extends TextFieldProps { |
| 733 |
/** Number of columns (default 35, min 5). */ |
| 734 |
cols?: number; |
| 735 |
/** Number of rows (default 5, min 2). */ |
| 736 |
rows?: number; |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
Text Field Area. |
| 741 |
@category Text Fields |
| 742 |
*/ |
| 743 |
export function TextFieldArea(props: TextFieldAreaProps) { |
| 744 |
const { disabled } = useContext(props); |
| 745 |
const id = useHtmlFor(); |
| 746 |
const [value, error, onChange] = useTextInputField(props, 900); |
| 747 |
const cols = Math.max(5, props.cols ?? 35); |
| 748 |
const rows = Math.max(2, props.rows ?? 5); |
| 749 |
const css = Utils.classes( |
| 750 |
'dome-xForm-textarea-field', |
| 751 |
props.className, |
| 752 |
); |
| 753 |
return ( |
| 754 |
<Field |
| 755 |
{...props} |
| 756 |
offset={4} |
| 757 |
htmlFor={id} |
| 758 |
error={error} |
| 759 |
> |
| 760 |
<textarea |
| 761 |
id={id} |
| 762 |
wrap="hard" |
| 763 |
spellCheck |
| 764 |
value={value} |
| 765 |
cols={cols} |
| 766 |
rows={rows - 1} |
| 767 |
className={css} |
| 768 |
style={props.style} |
| 769 |
disabled={disabled} |
| 770 |
placeholder={props.placeholder} |
| 771 |
onChange={onChange} |
| 772 |
/> |
| 773 |
</Field> |
| 774 |
); |
| 775 |
} |
| 776 |
|
| 777 |
/** |
| 778 |
Monospaced Text Field Area. |
| 779 |
@category Text Fields |
| 780 |
*/ |
| 781 |
export function TextCodeFieldArea(props: TextFieldAreaProps) { |
| 782 |
const { disabled } = useContext(props); |
| 783 |
const id = useHtmlFor(); |
| 784 |
const [value, error, onChange] = useTextInputField(props, 900); |
| 785 |
const cols = Math.max(5, props.cols ?? 35); |
| 786 |
const rows = Math.max(2, props.rows ?? 5); |
| 787 |
const css = Utils.classes( |
| 788 |
'dome-xForm-textarea-field', |
| 789 |
'dome-text-code', |
| 790 |
props.className, |
| 791 |
); |
| 792 |
return ( |
| 793 |
<Field |
| 794 |
{...props} |
| 795 |
offset={4} |
| 796 |
htmlFor={id} |
| 797 |
error={error} |
| 798 |
> |
| 799 |
<textarea |
| 800 |
id={id} |
| 801 |
wrap="off" |
| 802 |
spellCheck={false} |
| 803 |
value={value} |
| 804 |
cols={cols} |
| 805 |
rows={rows} |
| 806 |
className={css} |
| 807 |
style={props.style} |
| 808 |
disabled={disabled} |
| 809 |
placeholder={props.placeholder} |
| 810 |
onChange={onChange} |
| 811 |
/> |
| 812 |
</Field> |
| 813 |
); |
| 814 |
} |
| 815 |
|
| 816 |
/* --------------------------------------------------------------------------*/ |
| 817 |
/* --- Number Field ---*/ |
| 818 |
/* --------------------------------------------------------------------------*/ |
| 819 |
|
| 820 |
/** @category Number Fields */ |
| 821 |
export interface NumberFieldProps extends FieldProps<number | undefined> { |
| 822 |
units?: string; |
| 823 |
placeholder?: string; |
| 824 |
className?: string; |
| 825 |
style?: React.CSSProperties; |
| 826 |
latency?: number; |
| 827 |
} |
| 828 |
|
| 829 |
function TEXT_OF_NUMBER(n: number | undefined): string { |
| 830 |
if (n === undefined) return ''; |
| 831 |
if (Number.isNaN(n)) throw new Error('Invalid number'); |
| 832 |
return Number(n).toLocaleString('en'); |
| 833 |
} |
| 834 |
|
| 835 |
function NUMBER_OF_TEXT(s: string): number | undefined { |
| 836 |
if (s === '') return undefined; |
| 837 |
const n = Number.parseFloat(s.replace(/[ ,]/g, '')); |
| 838 |
if (Number.isNaN(n)) throw new Error('Invalid number'); |
| 839 |
return n; |
| 840 |
} |
| 841 |
|
| 842 |
/** |
| 843 |
Text Field for Numbers. |
| 844 |
@category Number Fields |
| 845 |
*/ |
| 846 |
export function NumberField(props: NumberFieldProps) { |
| 847 |
const { units, latency = 600 } = props; |
| 848 |
const { disabled } = useContext(props); |
| 849 |
const id = useHtmlFor(); |
| 850 |
const css = Utils.classes('dome-xForm-number-field', props.className); |
| 851 |
const checked = useChecker(props.state, props.checker); |
| 852 |
const filtered = useFilter(checked, TEXT_OF_NUMBER, NUMBER_OF_TEXT, ''); |
| 853 |
const [value, error, setState] = useLatency(filtered, latency); |
| 854 |
const onChange = useChangeEvent(setState); |
| 855 |
const UNITS = units && ( |
| 856 |
<label className="dome-text-label dome-xForm-units">{units}</label> |
| 857 |
); |
| 858 |
return ( |
| 859 |
<Field |
| 860 |
{...props} |
| 861 |
htmlFor={id} |
| 862 |
error={error} |
| 863 |
> |
| 864 |
<input |
| 865 |
id={id} |
| 866 |
type="text" |
| 867 |
value={value} |
| 868 |
className={css} |
| 869 |
style={props.style} |
| 870 |
disabled={disabled} |
| 871 |
placeholder={props.placeholder} |
| 872 |
onChange={onChange} |
| 873 |
/> |
| 874 |
{UNITS} |
| 875 |
</Field> |
| 876 |
); |
| 877 |
} |
| 878 |
|
| 879 |
/* --------------------------------------------------------------------------*/ |
| 880 |
/* --- Spinner Field ---*/ |
| 881 |
/* --------------------------------------------------------------------------*/ |
| 882 |
|
| 883 |
/** @category Number Fields */ |
| 884 |
export interface SpinnerFieldProps extends NumberFieldProps { |
| 885 |
units?: string; |
| 886 |
/** Minimum value (included). */ |
| 887 |
min: number; |
| 888 |
/** Maximum value (included). */ |
| 889 |
max: number; |
| 890 |
/** Stepper increment (defaults 1). */ |
| 891 |
step?: number; |
| 892 |
} |
| 893 |
|
| 894 |
/** |
| 895 |
Spinner Field |
| 896 |
@category Number Fields |
| 897 |
*/ |
| 898 |
export function SpinnerField(props: SpinnerFieldProps) { |
| 899 |
const { units, min, max, step = 1, latency = 600, checker } = props; |
| 900 |
const { disabled } = useContext(props); |
| 901 |
const id = useHtmlFor(); |
| 902 |
const css = Utils.classes('dome-xForm-spinner-field', props.className); |
| 903 |
const fullChecker = React.useCallback((v: number | undefined) => { |
| 904 |
if (v !== undefined && min <= v && v <= max) { |
| 905 |
return checker ? checker(v) : true; |
| 906 |
} |
| 907 |
return `Range ${min}…${max}`; |
| 908 |
|
| 909 |
}, [min, max, checker]); |
| 910 |
const checked = useChecker(props.state, fullChecker); |
| 911 |
const filtered = useFilter(checked, TEXT_OF_NUMBER, NUMBER_OF_TEXT, ''); |
| 912 |
const [value, error, setState] = useLatency(filtered, latency); |
| 913 |
const onChange = useChangeEvent(setState); |
| 914 |
const UNITS = units && ( |
| 915 |
<label className="dome-text-label dome-xForm-units">{units}</label> |
| 916 |
); |
| 917 |
return ( |
| 918 |
<Field |
| 919 |
{...props} |
| 920 |
htmlFor={id} |
| 921 |
error={error} |
| 922 |
> |
| 923 |
<input |
| 924 |
id={id} |
| 925 |
type="number" |
| 926 |
value={value} |
| 927 |
min={min} |
| 928 |
max={max} |
| 929 |
step={step} |
| 930 |
className={css} |
| 931 |
style={props.style} |
| 932 |
disabled={disabled} |
| 933 |
placeholder={props.placeholder} |
| 934 |
onChange={onChange} |
| 935 |
/> |
| 936 |
{UNITS} |
| 937 |
</Field> |
| 938 |
); |
| 939 |
} |
| 940 |
|
| 941 |
/* --------------------------------------------------------------------------*/ |
| 942 |
/* --- Slider Field ---*/ |
| 943 |
/* --------------------------------------------------------------------------*/ |
| 944 |
|
| 945 |
/** @category Number Fields */ |
| 946 |
export interface SliderFieldProps extends FieldProps<number> { |
| 947 |
/** Minimal value (included). */ |
| 948 |
min: number; |
| 949 |
/** Maximal value (included). */ |
| 950 |
max: number; |
| 951 |
/** Default is 1. */ |
| 952 |
step?: number; |
| 953 |
/** Reset value on double-click (if defined). */ |
| 954 |
onReset?: number; |
| 955 |
/** |
| 956 |
Show a label displaying the value (default is true). |
| 957 |
In case a function is provided, it is used to reformat the value. |
| 958 |
*/ |
| 959 |
labelValue?: boolean | ((value: number) => string); |
| 960 |
className?: string; |
| 961 |
style?: React.CSSProperties; |
| 962 |
latency?: number; |
| 963 |
} |
| 964 |
|
| 965 |
const FORMAT_VALUE = (v: number) => Number(v).toString(); |
| 966 |
const FORMAT_RANGE = (v: number) => (v > 0 ? `+${v}` : `-${-v}`); |
| 967 |
const FORMATING = (props: SliderFieldProps) => { |
| 968 |
const { labelValue = true, min } = props; |
| 969 |
if (labelValue === false) return undefined; |
| 970 |
if (labelValue === true) return min < 0 ? FORMAT_RANGE : FORMAT_VALUE; |
| 971 |
return labelValue; |
| 972 |
}; |
| 973 |
|
| 974 |
const CSS_SLIDER = 'dome-text-label dome-xForm-units dome-xForm-slider-value'; |
| 975 |
const SHOW_SLIDER = `${CSS_SLIDER} dome-xForm-slider-show`; |
| 976 |
const HIDE_SLIDER = `${CSS_SLIDER} dome-xForm-slider-hide`; |
| 977 |
|
| 978 |
/** |
| 979 |
Slider Field |
| 980 |
@category Number Fields |
| 981 |
*/ |
| 982 |
export function SliderField(props: SliderFieldProps) { |
| 983 |
const { min, max, step = 1, latency = 600, onReset } = props; |
| 984 |
const { disabled } = useContext(props); |
| 985 |
const id = useHtmlFor(); |
| 986 |
const css = Utils.classes('dome-xForm-slider-field', props.className); |
| 987 |
const checked = useChecker(props.state, props.checker); |
| 988 |
const delayed = useLatency(checked, latency); |
| 989 |
const [label, setLabel] = React.useState<string | undefined>(undefined); |
| 990 |
const [value, error, setState] = delayed; |
| 991 |
const labeling = FORMATING(props); |
| 992 |
const onChange = React.useMemo( |
| 993 |
() => { |
| 994 |
const fadeOut = debounce(() => setLabel(undefined), latency); |
| 995 |
return (evt: InputEvent) => { |
| 996 |
const v = Number.parseInt(evt.target.value, 10); |
| 997 |
if (!Number.isNaN(v)) { |
| 998 |
setState(v, undefined); |
| 999 |
const vlabel = labeling && labeling(v); |
| 1000 |
setLabel(vlabel); |
| 1001 |
if (vlabel) fadeOut(); |
| 1002 |
} else { |
| 1003 |
setLabel(undefined); |
| 1004 |
} |
| 1005 |
}; |
| 1006 |
}, [labeling, latency, setState, setLabel], |
| 1007 |
); |
| 1008 |
const onDoubleClick = React.useCallback(() => { |
| 1009 |
if (onReset) { |
| 1010 |
setState(onReset, undefined); |
| 1011 |
setLabel(undefined); |
| 1012 |
} |
| 1013 |
}, [onReset, setState, setLabel]); |
| 1014 |
const VALUELABEL = labeling && ( |
| 1015 |
<label className={label ? SHOW_SLIDER : HIDE_SLIDER}> |
| 1016 |
{label} |
| 1017 |
</label> |
| 1018 |
); |
| 1019 |
return ( |
| 1020 |
<Field |
| 1021 |
{...props} |
| 1022 |
htmlFor={id} |
| 1023 |
error={error} |
| 1024 |
> |
| 1025 |
<input |
| 1026 |
id={id} |
| 1027 |
type="range" |
| 1028 |
value={value} |
| 1029 |
min={min} |
| 1030 |
max={max} |
| 1031 |
step={step} |
| 1032 |
className={css} |
| 1033 |
style={props.style} |
| 1034 |
disabled={disabled} |
| 1035 |
onDoubleClick={onDoubleClick} |
| 1036 |
onChange={onChange} |
| 1037 |
/> |
| 1038 |
{VALUELABEL} |
| 1039 |
</Field> |
| 1040 |
); |
| 1041 |
} |
| 1042 |
|
| 1043 |
/* --------------------------------------------------------------------------*/ |
| 1044 |
/* --- Date Field ---*/ |
| 1045 |
/* --------------------------------------------------------------------------*/ |
| 1046 |
|
| 1047 |
/** @category Time and Date Fields */ |
| 1048 |
export interface TimeOrDateFieldProps extends FieldProps<string | undefined> { |
| 1049 |
min?: string; |
| 1050 |
max?: string; |
| 1051 |
className?: string; |
| 1052 |
style?: React.CSSProperties; |
| 1053 |
latency?: number; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/** |
| 1057 |
Field with a Date Input element. |
| 1058 |
|
| 1059 |
The date is presented in english locale, with format `mm/dd/yyyy`, |
| 1060 |
but the state value is a string compatible with |
| 1061 |
javascript `Date('yyyy-dd-mm')` format. |
| 1062 |
|
| 1063 |
@category Time and Date Fields |
| 1064 |
*/ |
| 1065 |
export function DateField(props: TimeOrDateFieldProps) { |
| 1066 |
const { disabled } = useContext(props); |
| 1067 |
const id = useHtmlFor(); |
| 1068 |
const css = Utils.classes('dome-xForm-date-field', props.className); |
| 1069 |
const [value, error, onChange] = useTextInputField(props, 600); |
| 1070 |
return ( |
| 1071 |
<Field |
| 1072 |
{...props} |
| 1073 |
htmlFor={id} |
| 1074 |
error={error} |
| 1075 |
> |
| 1076 |
<input |
| 1077 |
id={id} |
| 1078 |
type="date" |
| 1079 |
value={value} |
| 1080 |
min={props.min} |
| 1081 |
max={props.max} |
| 1082 |
className={css} |
| 1083 |
style={props.style} |
| 1084 |
disabled={disabled} |
| 1085 |
onChange={onChange} |
| 1086 |
/> |
| 1087 |
</Field> |
| 1088 |
); |
| 1089 |
} |
| 1090 |
|
| 1091 |
/** |
| 1092 |
Field with a Time Input element. |
| 1093 |
|
| 1094 |
The time is presented in english locale, but its internal |
| 1095 |
value is a string 'hh:mm' on 24h per day basis. This internal |
| 1096 |
format can be used to form a valid javascript |
| 1097 |
`Date('yyyy-mm-ddThh:mm')` object. |
| 1098 |
|
| 1099 |
@category Time and Date Fields |
| 1100 |
*/ |
| 1101 |
export function TimeField(props: TimeOrDateFieldProps) { |
| 1102 |
const { disabled } = useContext(props); |
| 1103 |
const id = useHtmlFor(); |
| 1104 |
const css = Utils.classes('dome-xForm-date-field', props.className); |
| 1105 |
const [value, error, onChange] = useTextInputField(props, 600); |
| 1106 |
return ( |
| 1107 |
<Field |
| 1108 |
{...props} |
| 1109 |
htmlFor={id} |
| 1110 |
error={error} |
| 1111 |
> |
| 1112 |
<input |
| 1113 |
id={id} |
| 1114 |
type="time" |
| 1115 |
value={value} |
| 1116 |
min={props.min} |
| 1117 |
max={props.max} |
| 1118 |
className={css} |
| 1119 |
style={props.style} |
| 1120 |
disabled={disabled} |
| 1121 |
onChange={onChange} |
| 1122 |
/> |
| 1123 |
</Field> |
| 1124 |
); |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* --------------------------------------------------------------------------*/ |
| 1128 |
/* --- Color Field ---*/ |
| 1129 |
/* --------------------------------------------------------------------------*/ |
| 1130 |
|
| 1131 |
/** @category Form Fields */ |
| 1132 |
export type ColorFieldProps = FieldProps<string | undefined>; |
| 1133 |
|
| 1134 |
/** @category Form Fields */ |
| 1135 |
export function ColorField(props: ColorFieldProps) { |
| 1136 |
const { disabled } = useContext(props); |
| 1137 |
const id = useHtmlFor(); |
| 1138 |
const [value, error, onChange] = useTextInputField(props, 600); |
| 1139 |
return ( |
| 1140 |
<Field |
| 1141 |
{...props} |
| 1142 |
htmlFor={id} |
| 1143 |
error={error} |
| 1144 |
> |
| 1145 |
<input |
| 1146 |
id={id} |
| 1147 |
type="color" |
| 1148 |
value={value || '#ffffff'} |
| 1149 |
className="dome-xForm-color-field" |
| 1150 |
disabled={disabled} |
| 1151 |
onChange={onChange} |
| 1152 |
/> |
| 1153 |
</Field> |
| 1154 |
); |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* --------------------------------------------------------------------------*/ |
| 1158 |
/* --- Check Box Field ---*/ |
| 1159 |
/* --------------------------------------------------------------------------*/ |
| 1160 |
|
| 1161 |
/** @category Form Fields */ |
| 1162 |
export interface CheckboxFieldProps extends FieldProps<boolean> { |
| 1163 |
/** Default is false */ |
| 1164 |
inverted?: boolean; |
| 1165 |
} |
| 1166 |
|
| 1167 |
/** @category Form Fields */ |
| 1168 |
export function CheckboxField(props: CheckboxFieldProps) { |
| 1169 |
const { hidden, disabled } = useContext(props); |
| 1170 |
|
| 1171 |
if (hidden) return null; |
| 1172 |
|
| 1173 |
const [value, , setState] = props.state; |
| 1174 |
const { label, title, inverted } = props; |
| 1175 |
const css = Utils.classes( |
| 1176 |
'dome-xForm-field dome-text-label', |
| 1177 |
disabled && 'dome-disabled', |
| 1178 |
); |
| 1179 |
const onChange = () => setState(!value, undefined); |
| 1180 |
return ( |
| 1181 |
<Checkbox |
| 1182 |
className={css} |
| 1183 |
label={label} |
| 1184 |
title={title} |
| 1185 |
disabled={disabled} |
| 1186 |
value={inverted ? !value : value} |
| 1187 |
onChange={onChange} |
| 1188 |
/> |
| 1189 |
); |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* --------------------------------------------------------------------------*/ |
| 1193 |
/* --- Radio Box Field ---*/ |
| 1194 |
/* --------------------------------------------------------------------------*/ |
| 1195 |
|
| 1196 |
/** @category Form Fields */ |
| 1197 |
export interface RadioFieldProps<A> extends FieldProps<A> { |
| 1198 |
value: A; |
| 1199 |
} |
| 1200 |
|
| 1201 |
/** @category Form Fields */ |
| 1202 |
export function RadioField<A>(props: RadioFieldProps<A>) { |
| 1203 |
const { hidden, disabled } = useContext(props); |
| 1204 |
|
| 1205 |
if (hidden) return null; |
| 1206 |
|
| 1207 |
const [selection, , setState] = props.state; |
| 1208 |
const onSelection = (value: A) => setState(value, undefined); |
| 1209 |
const { label, title, value } = props; |
| 1210 |
const css = Utils.classes( |
| 1211 |
'dome-xForm-field dome-text-label', |
| 1212 |
disabled && 'dome-disabled', |
| 1213 |
); |
| 1214 |
|
| 1215 |
return ( |
| 1216 |
<Radio |
| 1217 |
className={css} |
| 1218 |
label={label} |
| 1219 |
title={title} |
| 1220 |
value={value} |
| 1221 |
disabled={disabled} |
| 1222 |
selection={selection} |
| 1223 |
onSelection={onSelection} |
| 1224 |
/> |
| 1225 |
); |
| 1226 |
} |
| 1227 |
|
| 1228 |
/* --------------------------------------------------------------------------*/ |
| 1229 |
/* --- Select Menu Field ---*/ |
| 1230 |
/* --------------------------------------------------------------------------*/ |
| 1231 |
|
| 1232 |
/** @category Form Fields */ |
| 1233 |
export interface SelectFieldProps extends FieldProps<string | undefined> { |
| 1234 |
placeholder?: string; |
| 1235 |
children?: React.ReactNode; |
| 1236 |
} |
| 1237 |
|
| 1238 |
/** |
| 1239 |
Children must be standard `<option>` or `<optgroup>` elements. |
| 1240 |
|
| 1241 |
@category Form Fields |
| 1242 |
*/ |
| 1243 |
export function SelectField(props: SelectFieldProps) { |
| 1244 |
const { disabled } = useContext(props); |
| 1245 |
const id = useHtmlFor(); |
| 1246 |
const [value, error, setState] = useChecker(props.state, props.checker); |
| 1247 |
const onChange = (newValue?: string) => setState(newValue, undefined); |
| 1248 |
const { children, placeholder } = props; |
| 1249 |
return ( |
| 1250 |
<Field |
| 1251 |
{...props} |
| 1252 |
offset={5} |
| 1253 |
error={error} |
| 1254 |
htmlFor={id} |
| 1255 |
> |
| 1256 |
<SelectMenu |
| 1257 |
id={id} |
| 1258 |
value={value} |
| 1259 |
disabled={disabled} |
| 1260 |
placeholder={placeholder} |
| 1261 |
onChange={onChange} |
| 1262 |
> |
| 1263 |
{children} |
| 1264 |
</SelectMenu> |
| 1265 |
</Field> |
| 1266 |
); |
| 1267 |
} |
| 1268 |
|
| 1269 |
/** @category Form Fields */ |
| 1270 |
export interface MenuFieldOption<A> { |
| 1271 |
value: A; |
| 1272 |
label: string; |
| 1273 |
} |
| 1274 |
|
| 1275 |
/** @category Form Fields */ |
| 1276 |
export interface MenuFieldProps<A> extends FieldProps<A> { |
| 1277 |
/** Field label. */ |
| 1278 |
label: string; |
| 1279 |
/** Field tooltip text. */ |
| 1280 |
title?: string; |
| 1281 |
/** Field state. */ |
| 1282 |
state: FieldState<A>; |
| 1283 |
placeholder?: string; |
| 1284 |
defaultValue: A; |
| 1285 |
options: MenuFieldOption<A>[]; |
| 1286 |
} |
| 1287 |
|
| 1288 |
type ENTRY<A> = { option: JSX.Element, field: string, value: A }; |
| 1289 |
|
| 1290 |
/** |
| 1291 |
Creates a `<SelectField/>` form field with a predefine set |
| 1292 |
of (typed) options. |
| 1293 |
|
| 1294 |
@category Form Fields |
| 1295 |
*/ |
| 1296 |
export function MenuField<A>(props: MenuFieldProps<A>): JSX.Element { |
| 1297 |
const entries: ENTRY<A>[] = React.useMemo(() => |
| 1298 |
props.options.map((e, k) => { |
| 1299 |
const field = `item#${k}`; |
| 1300 |
const option = <option value={field} key={field} label={e.label} />; |
| 1301 |
return { field, option, value: e.value }; |
| 1302 |
}), [props.options]); |
| 1303 |
const input = React.useCallback( |
| 1304 |
(v) => entries.find((e) => e.value === v)?.field |
| 1305 |
, [entries] |
| 1306 |
); |
| 1307 |
const output = React.useCallback( |
| 1308 |
(f) => entries.find((e) => e.field === f)?.value ?? props.defaultValue |
| 1309 |
, [entries, props.defaultValue] |
| 1310 |
); |
| 1311 |
const defaultField = React.useMemo( |
| 1312 |
() => input(props.defaultValue), |
| 1313 |
[input, props.defaultValue] |
| 1314 |
); |
| 1315 |
const state = useFilter<A, string | undefined>( |
| 1316 |
props.state, |
| 1317 |
input, output, |
| 1318 |
defaultField, |
| 1319 |
); |
| 1320 |
return ( |
| 1321 |
<SelectField |
| 1322 |
state={state} |
| 1323 |
label={props.label} |
| 1324 |
title={props.title} |
| 1325 |
placeholder={props.placeholder} > |
| 1326 |
{entries.map((e) => e.option)} |
| 1327 |
</SelectField> |
| 1328 |
); |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
// -------------------------------------------------------------------------- |