Debian Codegraph @

ivette/src/dome/renderer/controls/buttons.tsx

raw
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 // --------------------------------------------------------------------------
24 // --- Buttons, Check Boxes and Radio Groups
25 // --------------------------------------------------------------------------
26
27 /**
28 @packageDocumentation
29 @module dome/controls/buttons
30 */
31
32 import React from 'react';
33 import { classes } from 'dome/misc/utils';
34 import { Icon } from './icons';
35 import './style.css';
36
37 interface EVENT {
38 stopPropagation: () => void;
39 }
40
41 const DISABLED = ({ disabled = false, enabled = true }): boolean => (
42 !!disabled || !enabled
43 );
44
45 const TRIGGER = (onClick?: () => void) => (evt?: EVENT) => {
46 evt?.stopPropagation();
47 if (onClick) onClick();
48 };
49
50 // --------------------------------------------------------------------------
51 // --- Button
52 // --------------------------------------------------------------------------
53
54 const VISIBLE: React.CSSProperties = { visibility: 'visible' };
55 const HIDDEN: React.CSSProperties = { visibility: 'hidden' };
56
57 interface LABELprops {
58 disabled: boolean;
59 label: string;
60 }
61
62 const LABEL = ({ disabled, label }: LABELprops): JSX.Element => (
63 <div className="dome-xButton-label">
64 <div
65 className="dome-xButton-label dome-control-enabled"
66 style={disabled ? HIDDEN : VISIBLE}
67 >{label}
68 </div>
69 <div
70 className="dome-xButton-label dome-control-disabled"
71 style={disabled ? VISIBLE : HIDDEN}
72 >{label}
73 </div>
74 </div>
75 );
76
77 export type ButtonKind =
78 'default' | 'primary' | 'warning' | 'positive' | 'negative';
79
80 export interface ButtonProps {
81 /** Text of the label. Prepend to other children elements. */
82 label?: string;
83 /** Icon identifier. Displayed on the left side of the label. */
84 icon?: string;
85 /** Tool-tip description. */
86 title?: string;
87 /** Additional class. */
88 className?: string;
89 /** Additional style. */
90 style?: React.CSSProperties;
91 /** Defaults to `false`. */
92 selected?: boolean;
93 /** Defaults to `true`. */
94 enabled?: boolean;
95 /** Defaults to `false`. */
96 disabled?: boolean;
97 /** Defaults to `true`. */
98 visible?: boolean;
99 /** Defaults to `true`. */
100 display?: boolean;
101 /**
102 May gain focus.
103 Focused button can be clicked with the `ENTER` key.
104 Defaults to `false`.
105 */
106 focusable?: boolean;
107 /** Styled bytton:
108 - `'default'`: normal button;
109 - `'primary'`: primary button, in blue;
110 - `'warning'`: warning button, in orange;
111 - `'positive'`: positive button, in green;
112 - `'negative'`: negative button, in red.
113 */
114 kind?: ButtonKind;
115 /** Blinking button. Defaults to `false`. */
116 blink?: boolean;
117 /**
118 Button callback.
119 An undefined callback automatically disables the button.
120 */
121 onClick?: () => void;
122 }
123
124 /** Standard button. */
125 export function Button(props: ButtonProps): JSX.Element {
126 const disabled = props.onClick ? DISABLED(props) : true;
127 const {
128 focusable = false, kind = 'default',
129 visible = true, display = true, blink = false,
130 selected, icon, label, className = '',
131 } = props;
132 const theClass = classes(
133 'dome-xButton dome-xBoxButton',
134 `dome-xButton-${selected ? 'selected' : kind}`,
135 blink && 'dome-xButton-blink',
136 !visible && 'dome-control-hidden',
137 !display && 'dome-control-erased',
138 className,
139 );
140 const nofocus = focusable ? undefined : true;
141 return (
142 <button
143 type="button"
144 className={theClass}
145 disabled={disabled}
146 onClick={TRIGGER(props.onClick)}
147 title={props.title}
148 style={props.style}
149 tabIndex={nofocus && -1}
150 onMouseDown={nofocus && ((evt) => evt.preventDefault())}
151 >
152 {icon && <Icon id={icon} />}
153 {label && <LABEL disabled={disabled} label={label} />}
154 </button>
155 );
156 }
157
158 // --------------------------------------------------------------------------
159 // --- Icon Button
160 // --------------------------------------------------------------------------
161
162 /** Circled Icon Button. The label property is ignored. */
163 export const CircButton = (props: ButtonProps): JSX.Element => {
164 const disabled = props.onClick ? DISABLED(props) : true;
165 const {
166 focusable = false, kind = 'default',
167 visible = true, display = true,
168 selected, icon, blink, className = '',
169 } = props;
170 const theClass = classes(
171 'dome-xButton dome-xCircButton',
172 `dome-xButton-${selected ? 'selected' : kind}`,
173 blink && 'dome-xButton-blink',
174 !visible && 'dome-control-hidden',
175 !display && 'dome-control-erased',
176 className,
177 );
178 const nofocus = focusable ? undefined : true;
179 return (
180 <button
181 type="button"
182 className={theClass}
183 disabled={disabled}
184 onClick={TRIGGER(props.onClick)}
185 title={props.title}
186 style={props.style}
187 tabIndex={nofocus && -1}
188 onMouseDown={nofocus && ((evt) => evt.preventDefault())}
189 >
190 {icon && <Icon offset={-1} id={icon} />}
191 </button>
192 );
193 };
194
195 // --------------------------------------------------------------------------
196 // --- Icon Button
197 // --------------------------------------------------------------------------
198
199 export type IconButtonKind =
200 undefined | 'selected' | 'default' | 'negative' | 'positive' | 'warning';
201
202 export interface IconButtonProps {
203 /** Icon identifier. Displayed on the left side of the label. */
204 icon: string;
205 /** Tool-tip description. */
206 title?: string;
207 /** Icon size, in pixels (default: `12`). */
208 size?: number;
209 /** Vertical offset, in pixels. */
210 offset?: number;
211 /** Additional class. */
212 className?: string;
213 /** Additional style. */
214 style?: React.CSSProperties;
215 /** Defaults to `false`. */
216 selected?: boolean;
217 /** Defaults to `true`. */
218 enabled?: boolean;
219 /** Defaults to `false`. */
220 disabled?: boolean;
221 /** Defaults to `true`. */
222 visible?: boolean;
223 /** Defaults to `true`. */
224 display?: boolean;
225 /** Styled bytton:
226 - `'default'`: normal button;
227 - `'selected'`: selection button, in blue;
228 - `'warning'`: warning button, in orange;
229 - `'positive'`: positive button, in green;
230 - `'negative'`: negative button, in red.
231 */
232 kind?: IconButtonKind;
233 /**
234 Button callback.
235 An undefined callback automatically disables the button.
236 */
237 onClick?: () => void;
238 }
239
240 /** Borderless Icon Button. Label property is ignored. */
241 export function IconButton(props: IconButtonProps): JSX.Element | null {
242 const disabled = props.onClick ? DISABLED(props) : true;
243 const {
244 icon, title, className,
245 visible = true, display = true, selected,
246 kind = 'default',
247 } = props;
248 if (!icon) return null;
249 const theClass = classes(
250 'dome-xIconButton',
251 `dome-xIconButton-${selected ? 'selected' : kind}`,
252 (disabled ? 'dome-control-disabled' : 'dome-control-enabled'),
253 !visible && 'dome-control-hidden',
254 !display && 'dome-control-erased',
255 className,
256 );
257 return (
258 <Icon
259 id={icon}
260 title={title}
261 size={props.size}
262 offset={props.offset}
263 style={props.style}
264 className={theClass}
265 onClick={TRIGGER(disabled ? undefined : props.onClick)}
266 />
267 );
268 }
269
270 // --------------------------------------------------------------------------
271 // --- CheckBox
272 // --------------------------------------------------------------------------
273
274 const CHECKBOX_ENABLED = 'dome-control-enabled dome-xCheckbox ';
275 const CHECKBOX_DISABLED = 'dome-control-disabled dome-xCheckbox ';
276
277 export interface CheckProps {
278 /** Button label. */
279 label: string;
280 /** Button tooltip. */
281 title?: string;
282 /** Additional class. */
283 className?: string;
284 /** Additional style. */
285 style?: React.CSSProperties;
286 /** Defaults to `true`. */
287 enabled?: boolean;
288 /** Defaults to `false`. */
289 disabled?: boolean;
290 /** Defaults to `false`. */
291 value?: boolean;
292 /** Callback to changes. */
293 onChange?: (newValue: boolean) => void;
294 }
295
296 /** Checkbox button. */
297 export const Checkbox = (props: CheckProps): JSX.Element => {
298 const { value, onChange } = props;
299 const disabled = onChange ? DISABLED(props) : true;
300 const callback = onChange && (() => onChange(!value));
301 const baseClass = disabled ? CHECKBOX_DISABLED : CHECKBOX_ENABLED;
302 const labelClass = props.className || '';
303 return (
304 <label
305 title={props.title}
306 style={props.style}
307 className={baseClass + labelClass}
308 >
309 <input
310 type="checkbox"
311 disabled={disabled}
312 checked={value}
313 onChange={callback}
314 />
315 {props.label}
316 </label>
317 );
318 };
319
320 /** Switch button. */
321 export const Switch = (props: CheckProps): JSX.Element => {
322 const { onChange, value } = props;
323 const disabled = onChange ? DISABLED(props) : true;
324 const iconId = props.value ? 'SWITCH.ON' : 'SWITCH.OFF';
325 const onClick = onChange && (() => onChange(!value));
326 const className = classes(
327 'dome-xSwitch',
328 (disabled ? 'dome-control-disabled' : 'dome-control-enabled'),
329 props.className,
330 );
331 return (
332 <label
333 title={props.title}
334 style={props.style}
335 className={className}
336 onClick={TRIGGER(onClick)}
337 >
338 <Icon size={16} id={iconId} />
339 {props.label}
340 </label>
341 );
342 };
343
344 // --------------------------------------------------------------------------
345 // --- Radio Button
346 // --------------------------------------------------------------------------
347
348 export interface RadioProps<A> {
349 /** Button label. */
350 label: string;
351 /** Button tooltip. */
352 title?: string;
353 /** Additional class. */
354 className?: string;
355 /** Additional style. */
356 style?: React.CSSProperties;
357 /** Defaults to `true`. */
358 enabled?: boolean;
359 /** Defaults to `false`. */
360 disabled?: boolean;
361 /** Value associated to the Radio. */
362 value: A;
363 /** Currently selected value. */
364 selection?: A;
365 /** Callback to changes. */
366 onSelection?: (newValue: A) => void;
367 }
368
369 /** Radio Button. See also [[RadioGroup]]. */
370 export function Radio<A>(props: RadioProps<A>): JSX.Element {
371 const { onSelection, value, selection } = props;
372 const disabled = onSelection ? DISABLED(props) : true;
373 const checked = value === selection;
374 const onChange = onSelection && (() => onSelection(value));
375 const baseClass = disabled ? CHECKBOX_DISABLED : CHECKBOX_ENABLED;
376 const labelClass = props.className || '';
377 return (
378 <label
379 title={props.title}
380 style={props.style}
381 className={baseClass + labelClass}
382 >
383 <input
384 type="radio"
385 disabled={disabled}
386 checked={checked}
387 onChange={onChange}
388 />
389 {props.label}
390 </label>
391 );
392 }
393
394 // --------------------------------------------------------------------------
395 // --- Radio Group
396 // --------------------------------------------------------------------------
397
398 export interface RadioGroupProps<A> {
399 /** Defaults to `true`. */
400 enabled?: boolean;
401 /** Defaults to `false`. */
402 disabled?: boolean;
403 /** Currently selected value. */
404 value?: A;
405 /** Callback to selected values. */
406 onChange?: (newValue: A) => void;
407 /** Default selected value. */
408 className?: string;
409 /** Additional style for the `< dov /> ` container of Raiods */
410 style?: React.CSSProperties;
411 /** [[Radio]] Buttons. */
412 children?: React.ReactNode;
413 }
414
415 /**
416 Selector of Radio Buttons. Childrens of the `RadioGroup` shall be [[Radio]]
417 buttons.
418
419 The selected value of the group is broadcasted to the radio buttons. Their
420 callbacks are activated _before_ the radio group one, if any.
421
422 If the radio group is disabled, all the radio buttons are disabled also and
423 their `disabled` properties are ignored. On the contrary, when the radio
424 group is enabled, the `disabled` property of each radio button is taken into
425 account.
426
427 The radio buttons inside a group are laidout in a vertical box with the
428 additional styling properties.
429 */
430 export function RadioGroup<A>(props: RadioGroupProps<A>): JSX.Element {
431 const {
432 className = '',
433 style,
434 value: selection,
435 onChange: onGroupSelect,
436 } = props;
437 const disabledGroup = onGroupSelect ? DISABLED(props) : true;
438 const makeRadio = (elt: unknown): JSX.Element => {
439 const typedElt = elt as React.ReactElement<RadioProps<A>>;
440 const radioProps = typedElt.props;
441 const disabled = disabledGroup || DISABLED(radioProps);
442 const { onSelection: onRadioSelect } = radioProps;
443 const onSelection = (v: A): void => {
444 if (onRadioSelect) onRadioSelect(v);
445 if (onGroupSelect) onGroupSelect(v);
446 };
447 return React.cloneElement(typedElt, {
448 disabled,
449 enabled: !disabled,
450 selection,
451 onSelection,
452 });
453 };
454 return (
455 <div className={`dome - xRadio - group ${className} `} style={style}>
456 {React.Children.map(props.children, makeRadio)}
457 </div>
458 );
459 }
460
461 // --------------------------------------------------------------------------
462 // --- Selector
463 // --------------------------------------------------------------------------
464
465 export interface SelectProps {
466 /** Field identifier (to make forms or labels) */
467 id?: string;
468 /** Button tooltip */
469 title?: string;
470 /** Button placeholder */
471 placeholder?: string;
472 /** Defaults to `true`. */
473 enabled?: boolean;
474 /** Defaults to `false`. */
475 disabled?: boolean;
476 /** Currently selected value. */
477 value?: string;
478 /** Callback to selected values. */
479 onChange?: (newValue?: string) => void;
480 /** Default selected value. */
481 className?: string;
482 /** Additional style for the `< dov /> ` container of Raiods */
483 style?: React.CSSProperties;
484 /** Shall be standard `<option/>` and `<optgroup/>` elements. */
485 children?: React.ReactNode;
486 }
487
488 /**
489 Menu Button.
490
491 The different options shall be specified with HTML
492 `< option/>` and `<optgroup/>` elements.
493 Options and group shall be specified as follows:
494
495 * <optgroup label='…'>…</optgroup>
496 * <option value='…' disabled=… >…</option>
497
498 */
499 export function Select(props: SelectProps): JSX.Element {
500 const { onChange, placeholder } = props;
501 const className = classes(
502 'dome-xSelect dome-xBoxButton dome-xButton-default dome-xButton-label',
503 props.className,
504 );
505 const disabled = onChange ? DISABLED(props) : true;
506 const callback = (evt: React.ChangeEvent<HTMLSelectElement>): void => {
507 if (onChange) onChange(evt.target.value);
508 };
509 return (
510 <select
511 id={props.id}
512 disabled={disabled}
513 className={className}
514 style={props.style}
515 title={props.title}
516 value={props.value}
517 onChange={callback}
518 >
519 {placeholder && <option value="">— {placeholder} —</option>}
520 {props.children}
521 </select >
522 );
523 }
524
525 // --------------------------------------------------------------------------
526 // --- Text Input
527 // --------------------------------------------------------------------------
528
529 export interface FieldProps {
530 /** Field identifier (to make forms or labels) */
531 id?: string;
532 /** Button tooltip */
533 title?: string;
534 /** Button placeholder */
535 placeholder?: string;
536 /** Defaults to `true`. */
537 enabled?: boolean;
538 /** Defaults to `false`. */
539 disabled?: boolean;
540 /** Default fo `false`. */
541 autoFocus?: boolean;
542 /** Currently selected value (updated on `ENTER` key) */
543 value?: string;
544 /** Callback on `ENTER` key. */
545 onChange?: (newValue: string) => void;
546 /** Callback on every modification. */
547 onEdited?: (tmpValue: string) => void;
548 /** Default selected value. */
549 className?: string;
550 /** Additional style for the `< dov /> ` container of Raiods */
551 style?: React.CSSProperties;
552 }
553
554 /**
555 Text Field.
556 */
557 export const Field = (props: FieldProps): JSX.Element => {
558 const [current, setCurrent] = React.useState<string>();
559 const { className = '', onChange, onEdited, value = '' } = props;
560 const disabled = onChange ? DISABLED(props) : true;
561 const theValue = current ?? value;
562 const ONCHANGE = (evt: React.ChangeEvent<HTMLInputElement>): void => {
563 const text = evt.target.value || '';
564 setCurrent(text);
565 if (onEdited) onEdited(text);
566 };
567 const ONKEYPRESS = (evt: React.KeyboardEvent): void => {
568 switch (evt.key) {
569 case 'Enter':
570 setCurrent(undefined);
571 if (onChange && current) onChange(current);
572 break;
573 case 'Escape':
574 setCurrent(undefined);
575 break;
576 default:
577 break;
578 }
579 };
580 return (
581 <input
582 id={props.id}
583 type="text"
584 autoFocus={!disabled && props.autoFocus}
585 value={theValue}
586 className={`dome - xField ${className} `}
587 style={props.style}
588 disabled={disabled}
589 placeholder={props.placeholder}
590 onKeyPress={ONKEYPRESS}
591 onChange={ONCHANGE}
592 />
593 );
594 };
595
596 // --------------------------------------------------------------------------