@poadio/ui-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/LICENSE +22 -0
- package/README.md +161 -2
- package/dist/avatar.d.ts +26 -0
- package/dist/avatar.d.ts.map +1 -0
- package/dist/avatar.js +35 -0
- package/dist/avatar.js.map +1 -0
- package/dist/badge.d.ts +14 -0
- package/dist/badge.d.ts.map +1 -0
- package/dist/badge.js +7 -0
- package/dist/badge.js.map +1 -0
- package/dist/button-group.d.ts +24 -0
- package/dist/button-group.d.ts.map +1 -0
- package/dist/button-group.js +38 -0
- package/dist/button-group.js.map +1 -0
- package/dist/button.d.ts +20 -0
- package/dist/button.d.ts.map +1 -0
- package/dist/button.js +8 -0
- package/dist/button.js.map +1 -0
- package/dist/checkbox.d.ts +34 -0
- package/dist/checkbox.d.ts.map +1 -0
- package/dist/checkbox.js +41 -0
- package/dist/checkbox.js.map +1 -0
- package/dist/chip.d.ts +26 -0
- package/dist/chip.d.ts.map +1 -0
- package/dist/chip.js +25 -0
- package/dist/chip.js.map +1 -0
- package/dist/colorpicker.d.ts +37 -0
- package/dist/colorpicker.d.ts.map +1 -0
- package/dist/colorpicker.js +107 -0
- package/dist/colorpicker.js.map +1 -0
- package/dist/controllable.d.ts +2 -0
- package/dist/controllable.d.ts.map +1 -0
- package/dist/controllable.js +12 -0
- package/dist/controllable.js.map +1 -0
- package/dist/divider.d.ts +13 -0
- package/dist/divider.d.ts.map +1 -0
- package/dist/divider.js +8 -0
- package/dist/divider.js.map +1 -0
- package/dist/field.d.ts +43 -0
- package/dist/field.d.ts.map +1 -0
- package/dist/field.js +57 -0
- package/dist/field.js.map +1 -0
- package/dist/floating.d.ts +86 -0
- package/dist/floating.d.ts.map +1 -0
- package/dist/floating.js +235 -0
- package/dist/floating.js.map +1 -0
- package/dist/i18n.d.ts +32 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +34 -0
- package/dist/i18n.js.map +1 -0
- package/dist/icon.d.ts +31 -0
- package/dist/icon.d.ts.map +1 -0
- package/dist/icon.js +40 -0
- package/dist/icon.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +50 -0
- package/dist/index.js.map +1 -0
- package/dist/inplace.d.ts +36 -0
- package/dist/inplace.d.ts.map +1 -0
- package/dist/inplace.js +58 -0
- package/dist/inplace.js.map +1 -0
- package/dist/mask.d.ts +65 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +104 -0
- package/dist/mask.js.map +1 -0
- package/dist/message.d.ts +21 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +14 -0
- package/dist/message.js.map +1 -0
- package/dist/meter.d.ts +26 -0
- package/dist/meter.d.ts.map +1 -0
- package/dist/meter.js +26 -0
- package/dist/meter.js.map +1 -0
- package/dist/number.d.ts +37 -0
- package/dist/number.d.ts.map +1 -0
- package/dist/number.js +41 -0
- package/dist/number.js.map +1 -0
- package/dist/password.d.ts +38 -0
- package/dist/password.d.ts.map +1 -0
- package/dist/password.js +24 -0
- package/dist/password.js.map +1 -0
- package/dist/popover.d.ts +44 -0
- package/dist/popover.d.ts.map +1 -0
- package/dist/popover.js +76 -0
- package/dist/popover.js.map +1 -0
- package/dist/progress.d.ts +25 -0
- package/dist/progress.d.ts.map +1 -0
- package/dist/progress.js +15 -0
- package/dist/progress.js.map +1 -0
- package/dist/radio.d.ts +35 -0
- package/dist/radio.d.ts.map +1 -0
- package/dist/radio.js +74 -0
- package/dist/radio.js.map +1 -0
- package/dist/rating.d.ts +35 -0
- package/dist/rating.d.ts.map +1 -0
- package/dist/rating.js +52 -0
- package/dist/rating.js.map +1 -0
- package/dist/segmented.d.ts +39 -0
- package/dist/segmented.d.ts.map +1 -0
- package/dist/segmented.js +51 -0
- package/dist/segmented.js.map +1 -0
- package/dist/select.d.ts +96 -0
- package/dist/select.d.ts.map +1 -0
- package/dist/select.js +156 -0
- package/dist/select.js.map +1 -0
- package/dist/skeleton.d.ts +13 -0
- package/dist/skeleton.d.ts.map +1 -0
- package/dist/skeleton.js +7 -0
- package/dist/skeleton.js.map +1 -0
- package/dist/slider.d.ts +46 -0
- package/dist/slider.d.ts.map +1 -0
- package/dist/slider.js +105 -0
- package/dist/slider.js.map +1 -0
- package/dist/switch.d.ts +37 -0
- package/dist/switch.d.ts.map +1 -0
- package/dist/switch.js +36 -0
- package/dist/switch.js.map +1 -0
- package/dist/tag.d.ts +17 -0
- package/dist/tag.d.ts.map +1 -0
- package/dist/tag.js +8 -0
- package/dist/tag.js.map +1 -0
- package/dist/toggle-button.d.ts +31 -0
- package/dist/toggle-button.d.ts.map +1 -0
- package/dist/toggle-button.js +20 -0
- package/dist/toggle-button.js.map +1 -0
- package/dist/tooltip.d.ts +32 -0
- package/dist/tooltip.d.ts.map +1 -0
- package/dist/tooltip.js +79 -0
- package/dist/tooltip.js.map +1 -0
- package/package.json +71 -4
- package/src/avatar.tsx +65 -0
- package/src/badge.tsx +22 -0
- package/src/button-group.tsx +51 -0
- package/src/button.tsx +39 -0
- package/src/checkbox.tsx +77 -0
- package/src/chip.tsx +63 -0
- package/src/colorpicker.tsx +213 -0
- package/src/controllable.ts +11 -0
- package/src/divider.tsx +21 -0
- package/src/field.tsx +123 -0
- package/src/floating.tsx +308 -0
- package/src/i18n.tsx +54 -0
- package/src/icon.tsx +77 -0
- package/src/index.ts +49 -0
- package/src/inplace.tsx +101 -0
- package/src/mask.ts +142 -0
- package/src/message.tsx +42 -0
- package/src/meter.tsx +71 -0
- package/src/number.tsx +90 -0
- package/src/password.tsx +83 -0
- package/src/popover.tsx +131 -0
- package/src/progress.tsx +66 -0
- package/src/radio.tsx +123 -0
- package/src/rating.tsx +100 -0
- package/src/segmented.tsx +89 -0
- package/src/select.tsx +314 -0
- package/src/skeleton.tsx +20 -0
- package/src/slider.tsx +166 -0
- package/src/switch.tsx +79 -0
- package/src/tag.tsx +28 -0
- package/src/toggle-button.tsx +60 -0
- package/src/tooltip.tsx +118 -0
package/src/radio.tsx
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadRadioGroup value={mode} onValueChange={setMode} aria-label="Pooling">
|
|
4
|
+
* <PoadRadio value="transaction" label="Transaction" />
|
|
5
|
+
* <PoadRadio value="session" label="Session" hint="One connection per client" disabled />
|
|
6
|
+
* </PoadRadioGroup>
|
|
7
|
+
*
|
|
8
|
+
* La logique (choix au clic, flèches en boucle sur les radios actifs, « roving tabindex ») est la machine `radio` de
|
|
9
|
+
* @poadio/ui-core ; la valeur vit dans le groupe (contrôlée `value` ou non `defaultValue`). Même DOM, mêmes classes,
|
|
10
|
+
* même ARIA que `poad-radio-group` / `poad-radio` d'ui-angular. Style : `@poadio/ui-core/radio.css`.
|
|
11
|
+
*
|
|
12
|
+
* Le groupe connaît ses radios en parcourant ses enfants (fragments et éléments du DOM compris) : un `PoadRadio`
|
|
13
|
+
* enveloppé dans un composant de l'application n'est pas vu pour le clavier. Les valeurs se comparent avec `===`.
|
|
14
|
+
*/
|
|
15
|
+
import {
|
|
16
|
+
Children, createContext, isValidElement, useContext, useId, useRef, type ComponentPropsWithRef, type KeyboardEvent, type ReactNode,
|
|
17
|
+
} from 'react';
|
|
18
|
+
import {
|
|
19
|
+
classNames, radioControlProps, radioGroupClasses, radioKeydown, radioLabelProps, radioRootClasses, radioSelect,
|
|
20
|
+
type RadioGroupState, type RadioItem,
|
|
21
|
+
} from '@poadio/ui-core';
|
|
22
|
+
import { useControllable } from './controllable.js';
|
|
23
|
+
|
|
24
|
+
interface RadioGroupContextValue {
|
|
25
|
+
readonly state: RadioGroupState<unknown>;
|
|
26
|
+
readonly items: readonly RadioItem<unknown>[];
|
|
27
|
+
select(value: unknown): void;
|
|
28
|
+
}
|
|
29
|
+
const RadioGroupContext = createContext<RadioGroupContextValue | null>(null);
|
|
30
|
+
|
|
31
|
+
export interface PoadRadioGroupProps<T = unknown> extends Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onChange'> {
|
|
32
|
+
readonly value?: T | null;
|
|
33
|
+
readonly defaultValue?: T | null;
|
|
34
|
+
/** Appelé quand l'UTILISATEUR choisit (clic ou flèches). */
|
|
35
|
+
readonly onValueChange?: (value: T) => void;
|
|
36
|
+
readonly disabled?: boolean;
|
|
37
|
+
readonly orientation?: 'vertical' | 'horizontal';
|
|
38
|
+
readonly 'aria-label'?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Les `PoadRadio` d'un arbre d'enfants, dans l'ordre du document. */
|
|
42
|
+
function radiosOf(children: ReactNode): RadioItem<unknown>[] {
|
|
43
|
+
const out: RadioItem<unknown>[] = [];
|
|
44
|
+
Children.forEach(children, (child) => {
|
|
45
|
+
if (!isValidElement<{ value?: unknown; disabled?: boolean; children?: ReactNode }>(child)) return;
|
|
46
|
+
if (child.type === PoadRadio) out.push({ value: child.props.value, disabled: !!child.props.disabled });
|
|
47
|
+
else out.push(...radiosOf(child.props.children));
|
|
48
|
+
});
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function PoadRadioGroup<T = unknown>({
|
|
53
|
+
value, defaultValue = null, onValueChange, disabled, orientation = 'vertical', 'aria-label': ariaLabel, className, children, onKeyDown, ref, ...rest
|
|
54
|
+
}: PoadRadioGroupProps<T>) {
|
|
55
|
+
const [current, setCurrent] = useControllable<T | null>(value, defaultValue);
|
|
56
|
+
const root = useRef<HTMLDivElement | null>(null);
|
|
57
|
+
const state: RadioGroupState<unknown> = { value: current, disabled: !!disabled };
|
|
58
|
+
const items = radiosOf(children);
|
|
59
|
+
const select = (next: unknown) => {
|
|
60
|
+
if (state.disabled) return;
|
|
61
|
+
setCurrent(next as T);
|
|
62
|
+
onValueChange?.(next as T);
|
|
63
|
+
};
|
|
64
|
+
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
65
|
+
onKeyDown?.(e);
|
|
66
|
+
const next = radioKeydown(state, items, e.key);
|
|
67
|
+
if (!next) return;
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
select(next.value);
|
|
70
|
+
root.current?.querySelectorAll<HTMLButtonElement>('[role="radio"]')[next.index]?.focus();
|
|
71
|
+
};
|
|
72
|
+
return (
|
|
73
|
+
<RadioGroupContext value={{ state, items, select }}>
|
|
74
|
+
<div
|
|
75
|
+
{...rest}
|
|
76
|
+
ref={(el) => { root.current = el; if (typeof ref === 'function') ref(el); else if (ref) ref.current = el; }}
|
|
77
|
+
className={classNames('poad-radio-group', radioGroupClasses({ orientation, disabled }), className)}
|
|
78
|
+
role="radiogroup" aria-label={ariaLabel || undefined} onKeyDown={onKey}
|
|
79
|
+
>
|
|
80
|
+
{children}
|
|
81
|
+
</div>
|
|
82
|
+
</RadioGroupContext>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PoadRadioProps<T = unknown> extends Omit<ComponentPropsWithRef<'span'>, 'id'> {
|
|
87
|
+
readonly value: T;
|
|
88
|
+
readonly label?: string;
|
|
89
|
+
/** Aide sous le libellé. */
|
|
90
|
+
readonly hint?: string;
|
|
91
|
+
readonly disabled?: boolean;
|
|
92
|
+
/** Identifiant du bouton ; celui du libellé en dérive. Absent : généré. */
|
|
93
|
+
readonly id?: string;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Valeur d'un groupe absent : un radio solitaire n'est jamais coché. */
|
|
97
|
+
const NO_GROUP: RadioGroupState<unknown> = { value: Symbol('poad-radio-sans-groupe'), disabled: false };
|
|
98
|
+
|
|
99
|
+
export function PoadRadio<T = unknown>({ value, label = '', hint, disabled, id: idProp, className, children, ...rest }: PoadRadioProps<T>) {
|
|
100
|
+
const generatedId = useId();
|
|
101
|
+
const group = useContext(RadioGroupContext);
|
|
102
|
+
const item: RadioItem<unknown> = { value, disabled: !!disabled };
|
|
103
|
+
const state = group?.state ?? NO_GROUP;
|
|
104
|
+
const known = group ? group.items.findIndex((it) => it.value === value) : -1;
|
|
105
|
+
const items = known >= 0 ? group!.items : [...(group?.items ?? []), item];
|
|
106
|
+
const index = known >= 0 ? known : items.length - 1;
|
|
107
|
+
const element = { id: idProp ?? generatedId, label };
|
|
108
|
+
const control = radioControlProps(state, items, index, element);
|
|
109
|
+
const labelAttrs = radioLabelProps(element);
|
|
110
|
+
return (
|
|
111
|
+
<span {...rest} className={classNames('poad-radio', radioRootClasses(state, item), className)}>
|
|
112
|
+
<button
|
|
113
|
+
type="button" role={control.role} id={control.id} aria-checked={control['aria-checked']} tabIndex={control.tabindex} disabled={control.disabled}
|
|
114
|
+
aria-labelledby={control['aria-labelledby'] ?? undefined}
|
|
115
|
+
onClick={() => { if (group && radioSelect(group.state, item)) group.select(value); }}
|
|
116
|
+
>
|
|
117
|
+
<span className="poad-radio__dot" />
|
|
118
|
+
</button>
|
|
119
|
+
{labelAttrs && <label id={labelAttrs.id} htmlFor={labelAttrs.for} className="poad-radio__label">{label}{hint && <small>{hint}</small>}</label>}
|
|
120
|
+
{children}
|
|
121
|
+
</span>
|
|
122
|
+
);
|
|
123
|
+
}
|
package/src/rating.tsx
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadRating value={note} onValueChange={setNote} max={5} allowHalf showValue /> contrôlé
|
|
4
|
+
* <PoadRating defaultValue={3} readOnly /> non contrôlé
|
|
5
|
+
*
|
|
6
|
+
* La logique (← → ↑ ↓ ± 1 ou ½ avec miroir de droite à gauche, Home / End / Suppr, survol, demi-étoile, remise à
|
|
7
|
+
* zéro `clearable`, attributs `slider`) est la machine `rating` de @poadio/ui-core. Même DOM, mêmes classes, même
|
|
8
|
+
* ARIA que `poad-rating` d'ui-angular. Style : `@poadio/ui-core/rating.css` (+ `icon.css`). Nom par défaut : catalogue
|
|
9
|
+
* `field.rating`.
|
|
10
|
+
*
|
|
11
|
+
* `ref` désigne l'élément `role="slider"` ; `className` et les autres props vont sur la racine `<span class="poad-rating">`.
|
|
12
|
+
*/
|
|
13
|
+
import { useRef, useState, type ComponentPropsWithoutRef, type CSSProperties, type MouseEvent, type Ref } from 'react';
|
|
14
|
+
import {
|
|
15
|
+
RATING_KEYS, classNames, elementIsRtl, ratingColor, ratingControlProps, ratingFill, ratingFirstHalf, ratingIconSize, ratingInteractive, ratingRootClasses,
|
|
16
|
+
ratingShown, ratingStarClasses, ratingStars, ratingText, ratingTransition, type RatingEvent, type RatingSeverity, type RatingSize, type RatingState,
|
|
17
|
+
} from '@poadio/ui-core';
|
|
18
|
+
import { useControllable } from './controllable.js';
|
|
19
|
+
import { useUiI18n } from './i18n.js';
|
|
20
|
+
import { PoadIcon } from './icon.js';
|
|
21
|
+
|
|
22
|
+
export interface PoadRatingProps extends Omit<ComponentPropsWithoutRef<'span'>, 'onChange' | 'defaultValue'> {
|
|
23
|
+
readonly value?: number | null;
|
|
24
|
+
readonly defaultValue?: number | null;
|
|
25
|
+
/** Appelé quand l'UTILISATEUR change la note. */
|
|
26
|
+
readonly onValueChange?: (value: number) => void;
|
|
27
|
+
/** Nombre d'étoiles. Défaut : 5. */
|
|
28
|
+
readonly max?: number;
|
|
29
|
+
readonly allowHalf?: boolean;
|
|
30
|
+
readonly readOnly?: boolean;
|
|
31
|
+
readonly disabled?: boolean;
|
|
32
|
+
/** Cliquer la note courante la remet à 0. Défaut : oui. */
|
|
33
|
+
readonly clearable?: boolean;
|
|
34
|
+
/** Icône nommée. Défaut : `star`. */
|
|
35
|
+
readonly icon?: string;
|
|
36
|
+
readonly size?: RatingSize;
|
|
37
|
+
readonly severity?: RatingSeverity;
|
|
38
|
+
readonly showValue?: boolean;
|
|
39
|
+
readonly 'aria-label'?: string;
|
|
40
|
+
readonly ref?: Ref<HTMLDivElement>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function PoadRating({
|
|
44
|
+
value, defaultValue = null, onValueChange, max = 5, allowHalf, readOnly, disabled, clearable = true, icon = 'star', size = 'md', severity = 'warning',
|
|
45
|
+
showValue, 'aria-label': ariaLabel, ref, className, style, ...rest
|
|
46
|
+
}: PoadRatingProps) {
|
|
47
|
+
const { t } = useUiI18n();
|
|
48
|
+
const root = useRef<HTMLSpanElement>(null);
|
|
49
|
+
const [current, setCurrent] = useControllable(value, defaultValue);
|
|
50
|
+
const [hover, setHover] = useState<number | null>(null);
|
|
51
|
+
const options = { max, allowHalf, clearable };
|
|
52
|
+
const state: RatingState = { value: current ?? 0, hover, disabled: !!disabled, readonly: !!readOnly };
|
|
53
|
+
const control = ratingControlProps(state, options);
|
|
54
|
+
const px = ratingIconSize(size);
|
|
55
|
+
|
|
56
|
+
const send = (event: RatingEvent) => {
|
|
57
|
+
const next = ratingTransition(state, event, options);
|
|
58
|
+
if (next === state) return;
|
|
59
|
+
setHover(next.hover);
|
|
60
|
+
if (next.value !== state.value) { setCurrent(next.value); onValueChange?.(next.value); }
|
|
61
|
+
};
|
|
62
|
+
const firstHalf = (e: MouseEvent<HTMLElement>) => {
|
|
63
|
+
const r = e.currentTarget.getBoundingClientRect();
|
|
64
|
+
return ratingFirstHalf(e.clientX - r.left, r.width, elementIsRtl(root.current));
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<span
|
|
69
|
+
{...rest} ref={root} className={classNames('poad-rating', ratingRootClasses(state, { size }), className)}
|
|
70
|
+
style={{ ...style, '--_c': ratingColor(severity) } as CSSProperties}
|
|
71
|
+
>
|
|
72
|
+
<div
|
|
73
|
+
ref={ref} className="poad-rating__stars" role={control.role} tabIndex={control.tabindex}
|
|
74
|
+
aria-valuemin={control['aria-valuemin']} aria-valuemax={control['aria-valuemax']} aria-valuenow={control['aria-valuenow']} aria-valuetext={control['aria-valuetext']}
|
|
75
|
+
aria-label={ariaLabel || t('field.rating')} aria-disabled={control['aria-disabled'] ?? undefined} aria-readonly={control['aria-readonly'] ?? undefined}
|
|
76
|
+
onKeyDown={(e) => {
|
|
77
|
+
if (!ratingInteractive(state) || !RATING_KEYS.includes(e.key)) return;
|
|
78
|
+
e.preventDefault();
|
|
79
|
+
send({ type: 'keydown', key: e.key, rtl: elementIsRtl(root.current) });
|
|
80
|
+
}}
|
|
81
|
+
onMouseLeave={() => send({ type: 'leave' })}
|
|
82
|
+
>
|
|
83
|
+
{ratingStars(options).map((i) => {
|
|
84
|
+
const fill = ratingFill(state, i);
|
|
85
|
+
return (
|
|
86
|
+
<span
|
|
87
|
+
key={i} className={classNames('poad-rating__star', ratingStarClasses(fill))}
|
|
88
|
+
onMouseMove={(e) => send({ type: 'hover', index: i, firstHalf: firstHalf(e) })}
|
|
89
|
+
onClick={(e) => send({ type: 'select', index: i, firstHalf: firstHalf(e) })}
|
|
90
|
+
>
|
|
91
|
+
<PoadIcon name={icon} size={px} className="off" />
|
|
92
|
+
<span className="on" style={{ width: `${fill * 100}%` }}><PoadIcon name={icon} size={px} filled /></span>
|
|
93
|
+
</span>
|
|
94
|
+
);
|
|
95
|
+
})}
|
|
96
|
+
</div>
|
|
97
|
+
{showValue && <span className="poad-rating__val">{ratingText(ratingShown(state), options)}</span>}
|
|
98
|
+
</span>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadSegmented options={[{ label: 'Table', value: 't' }, { label: 'SQL', value: 's', icon: 'code' }]} value={view} onValueChange={setView} />
|
|
4
|
+
* <PoadSegmented options={days} multiple defaultValue={['mon']} optionLabel="name" optionValue="id" aria-label="Days" />
|
|
5
|
+
*
|
|
6
|
+
* Choix segmenté : machine `segmented` de @poadio/ui-core (choix unique ou multiple, `allowEmpty`, un seul arrêt de
|
|
7
|
+
* tabulation, flèches / Début / Fin qui déplacent le focus dans le sens de lecture, Espace / Entrée qui choisissent).
|
|
8
|
+
* Même DOM, mêmes classes, même ARIA que `poad-segmented` d'ui-angular. Style : `@poadio/ui-core/segmented.css`.
|
|
9
|
+
* `renderOption` remplace l'icône et le libellé d'une option (gabarit d'option en Angular).
|
|
10
|
+
*/
|
|
11
|
+
import { useRef, type ComponentPropsWithRef, type KeyboardEvent, type ReactNode } from 'react';
|
|
12
|
+
import {
|
|
13
|
+
classNames, segmentedKeydown, segmentedOptionProps, segmentedPick, segmentedRootClasses, type SegmentedItem,
|
|
14
|
+
} from '@poadio/ui-core';
|
|
15
|
+
import { useControllable } from './controllable.js';
|
|
16
|
+
import { PoadIcon } from './icon.js';
|
|
17
|
+
|
|
18
|
+
/** Une option : `label`, `value`, `icon` (nom du jeu de la bibliothèque), `disabled`, `title` — ou un objet lu par `optionLabel` / `optionValue`. */
|
|
19
|
+
export type PoadSegmentOption = { readonly label?: string; readonly value?: unknown; readonly icon?: string; readonly disabled?: boolean; readonly title?: string } & Readonly<Record<string, unknown>>;
|
|
20
|
+
|
|
21
|
+
export interface PoadSegmentedProps<T = unknown> extends Omit<ComponentPropsWithRef<'div'>, 'defaultValue' | 'onChange'> {
|
|
22
|
+
readonly options?: readonly PoadSegmentOption[];
|
|
23
|
+
/** Champ du libellé (chemin pointé). Défaut `label`. */
|
|
24
|
+
readonly optionLabel?: string;
|
|
25
|
+
/** Champ de la valeur (chemin pointé). Défaut `value`. */
|
|
26
|
+
readonly optionValue?: string;
|
|
27
|
+
readonly value?: T | T[] | null;
|
|
28
|
+
readonly defaultValue?: T | T[] | null;
|
|
29
|
+
/** Appelé quand l'UTILISATEUR change la valeur. */
|
|
30
|
+
readonly onValueChange?: (value: T | T[] | null) => void;
|
|
31
|
+
readonly multiple?: boolean;
|
|
32
|
+
readonly allowEmpty?: boolean;
|
|
33
|
+
readonly block?: boolean;
|
|
34
|
+
readonly size?: 'sm' | 'md' | 'lg';
|
|
35
|
+
readonly shape?: 'rounded' | 'pill';
|
|
36
|
+
readonly disabled?: boolean;
|
|
37
|
+
readonly 'aria-label'?: string;
|
|
38
|
+
readonly renderOption?: (option: PoadSegmentOption) => ReactNode;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const resolve = (o: unknown, path: string): unknown => path.split('.').reduce<unknown>((acc, k) => (acc == null ? acc : (acc as Record<string, unknown>)[k]), o);
|
|
42
|
+
|
|
43
|
+
export function PoadSegmented<T = unknown>({
|
|
44
|
+
options = [], optionLabel = 'label', optionValue = 'value', value, defaultValue = null, onValueChange, multiple, allowEmpty, block, size = 'md', shape = 'rounded',
|
|
45
|
+
disabled, 'aria-label': ariaLabel, renderOption, className, onKeyDown, ref, ...rest
|
|
46
|
+
}: PoadSegmentedProps<T>) {
|
|
47
|
+
const [current, setCurrent] = useControllable<T | T[] | null>(value, defaultValue);
|
|
48
|
+
const root = useRef<HTMLDivElement | null>(null);
|
|
49
|
+
const mode = { multiple, allowEmpty, disabled };
|
|
50
|
+
const items: SegmentedItem<T>[] = options.map((o) => ({ value: resolve(o, optionValue) as T, disabled: !!o.disabled }));
|
|
51
|
+
const pick = (index: number) => {
|
|
52
|
+
const next = segmentedPick(current, items[index]!.value, mode);
|
|
53
|
+
if (next === current) return;
|
|
54
|
+
setCurrent(next);
|
|
55
|
+
onValueChange?.(next);
|
|
56
|
+
};
|
|
57
|
+
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
58
|
+
onKeyDown?.(e);
|
|
59
|
+
const buttons = [...(root.current?.querySelectorAll<HTMLButtonElement>(':scope > button') ?? [])];
|
|
60
|
+
const from = buttons.indexOf(e.target as HTMLButtonElement);
|
|
61
|
+
if (from < 0) return;
|
|
62
|
+
const rtl = getComputedStyle(root.current!).direction === 'rtl';
|
|
63
|
+
const to = segmentedKeydown(items, from, e.key, { ...mode, rtl });
|
|
64
|
+
if (to === null) return;
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
buttons[to]?.focus();
|
|
67
|
+
};
|
|
68
|
+
return (
|
|
69
|
+
<div
|
|
70
|
+
{...rest}
|
|
71
|
+
ref={(el) => { root.current = el; if (typeof ref === 'function') ref(el); else if (ref) ref.current = el; }}
|
|
72
|
+
className={classNames('poad-segmented', segmentedRootClasses({ disabled, size, block, shape }), className)}
|
|
73
|
+
role="group" aria-label={ariaLabel || undefined} onKeyDown={onKey}
|
|
74
|
+
>
|
|
75
|
+
{options.map((o, i) => {
|
|
76
|
+
const attrs = segmentedOptionProps(current, items, i, mode);
|
|
77
|
+
const label = String(resolve(o, optionLabel) ?? '');
|
|
78
|
+
return (
|
|
79
|
+
<button
|
|
80
|
+
key={i} type="button" className={attrs['aria-pressed'] === 'true' ? 'is-on' : undefined} disabled={attrs.disabled}
|
|
81
|
+
aria-pressed={attrs['aria-pressed']} tabIndex={attrs.tabindex} title={o.title || undefined} onClick={() => pick(i)}
|
|
82
|
+
>
|
|
83
|
+
{renderOption ? renderOption(o) : <>{o.icon && <PoadIcon name={o.icon} size={14} />}{label && <span>{label}</span>}</>}
|
|
84
|
+
</button>
|
|
85
|
+
);
|
|
86
|
+
})}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
package/src/select.tsx
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadSelect options={runtimes} optionLabel="name" optionValue="id" value={id} onChange={setId} placeholder="Runtime" filter clearable />
|
|
4
|
+
* <PoadSelect options={['S', 'M', 'L']} defaultValue="M" onChange={save} aria-label="Size" /> non contrôlé
|
|
5
|
+
*
|
|
6
|
+
* La logique (options aplaties et groupes, filtre, ouverture, option active, clavier WAI-ARIA combobox, recherche au
|
|
7
|
+
* clavier, attributs ARIA, classes d'état, placement du panneau) est la machine `select` de @poadio/ui-core : la
|
|
8
|
+
* valeur vit dans React (prop `value`, ou état interne à partir de `defaultValue`), l'état de la machine dans un
|
|
9
|
+
* `useState` ; chaque geste passe par `selectTransition`, ce composant applique l'état suivant et exécute les effets.
|
|
10
|
+
* Même DOM, mêmes classes, même ARIA que `poad-select` d'ui-angular — le banc de conformité le vérifie.
|
|
11
|
+
* Style : `@poadio/ui-core/select.css`, `option-list.css` et `icon.css`. Icônes : le jeu nommé de la bibliothèque (`PoadIcon`).
|
|
12
|
+
*
|
|
13
|
+
* Panneau : couche flottante interne (`useFloating` : dans la racine, `position: fixed`, mêmes positions que
|
|
14
|
+
* l'overlay du CDK d'Angular — `SELECT_PLACEMENTS`, 4 px, poussé dans la fenêtre), fermée par un appui extérieur
|
|
15
|
+
* (`useDismiss`). Petit écran (`mobile="auto"`, sous 768 px) ou `mobile="sheet"` : feuille basse (`PoadSheet`, même DOM
|
|
16
|
+
* que celle d'Angular : glisser pour fermer, piège à focus, défilement bloqué). `ref` désigne le déclencheur
|
|
17
|
+
* (`role="combobox"`, focus) ; `className` et les autres props vont sur la racine `<div class="poad-select">`.
|
|
18
|
+
* Styles : `select.css`, `option-list.css`, `icon.css`, `floating.css`, `sheet.css`.
|
|
19
|
+
*/
|
|
20
|
+
import { useEffect, useId, useMemo, useRef, useState, type ComponentPropsWithoutRef, type KeyboardEvent, type ReactNode, type Ref } from 'react';
|
|
21
|
+
import {
|
|
22
|
+
FOCUS_AUTOFOCUS_ATTR, SELECT_INITIAL, SELECT_PLACEMENTS, SHEET_MOBILE_QUERY, classNames, selectEmptyMessage, selectEquals, selectFallbackLabel,
|
|
23
|
+
selectFilterProps, selectFindOption, selectItems, selectListboxProps, selectOptionProps, selectRootClasses, selectScrollTop, selectSheetTitle,
|
|
24
|
+
selectTransition, selectTriggerProps, selectUseSheet, selectVisibleItems,
|
|
25
|
+
type SelectContext, type SelectEffect, type SelectEvent, type SelectFields, type SelectMobileMode, type SelectState,
|
|
26
|
+
} from '@poadio/ui-core';
|
|
27
|
+
import { PoadSheet, useDismiss, useFloating, useMediaQuery } from './floating.js';
|
|
28
|
+
import { useUiI18n } from './i18n.js';
|
|
29
|
+
import { PoadIcon as Icon } from './icon.js';
|
|
30
|
+
|
|
31
|
+
/** Ce que reçoit `renderOption` (comme le contexte de `poadSelectItem` en Angular), plus `active`. */
|
|
32
|
+
export interface PoadSelectOptionContext {
|
|
33
|
+
readonly selected: boolean;
|
|
34
|
+
readonly active: boolean;
|
|
35
|
+
readonly index: number;
|
|
36
|
+
readonly label: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface PoadSelectProps<T = unknown, V = unknown>
|
|
40
|
+
extends Omit<ComponentPropsWithoutRef<'div'>, 'onChange' | 'defaultValue' | 'id' | 'children'>, SelectFields {
|
|
41
|
+
/** Options : primitives (libellé = valeur) ou objets, lus par `optionLabel` / `optionValue` / `optionDisabled`… */
|
|
42
|
+
readonly options?: readonly T[];
|
|
43
|
+
/** Valeur imposée (mode contrôlé) ; `null` = aucune. Absente : le composant garde la valeur, à partir de `defaultValue`. */
|
|
44
|
+
readonly value?: V | null;
|
|
45
|
+
readonly defaultValue?: V | null;
|
|
46
|
+
/** Appelé quand l'UTILISATEUR choisit ou efface (pas quand `value` change). */
|
|
47
|
+
readonly onChange?: (value: V | null) => void;
|
|
48
|
+
/** Comparaison de valeurs (objets). Défaut : `dataKey`, puis `===`. */
|
|
49
|
+
readonly compareWith?: (a: unknown, b: unknown) => boolean;
|
|
50
|
+
readonly dataKey?: string;
|
|
51
|
+
readonly placeholder?: string;
|
|
52
|
+
/** Champ de filtre dans le panneau. */
|
|
53
|
+
readonly filter?: boolean;
|
|
54
|
+
/** Indication du champ de filtre ; absente : message `select.filterPlaceholder` du catalogue. */
|
|
55
|
+
readonly filterPlaceholder?: string;
|
|
56
|
+
/** Chemins (séparés par des virgules) sur lesquels filtrer ; défaut : le libellé. */
|
|
57
|
+
readonly filterBy?: string;
|
|
58
|
+
/** Focus dans le filtre à l'ouverture (défaut : oui, sauf en feuille basse). */
|
|
59
|
+
readonly autofocusFilter?: boolean;
|
|
60
|
+
/** Bouton × et Suppr / Retour arrière pour effacer. */
|
|
61
|
+
readonly clearable?: boolean;
|
|
62
|
+
readonly loading?: boolean;
|
|
63
|
+
/** Absent : message `select.empty` du catalogue. */
|
|
64
|
+
readonly emptyMessage?: string;
|
|
65
|
+
/** Absent : message `select.emptyFilter` du catalogue. */
|
|
66
|
+
readonly emptyFilterMessage?: string;
|
|
67
|
+
readonly size?: 'sm' | 'md' | 'lg';
|
|
68
|
+
readonly invalid?: boolean;
|
|
69
|
+
readonly disabled?: boolean;
|
|
70
|
+
readonly readOnly?: boolean;
|
|
71
|
+
/** Hauteur maximale de la liste (px, défaut 260). */
|
|
72
|
+
readonly panelMaxHeight?: number;
|
|
73
|
+
/** Nom accessible du déclencheur. */
|
|
74
|
+
readonly 'aria-label'?: string;
|
|
75
|
+
/** `auto` : feuille basse sous 768 px ; `sheet` : toujours ; `popover` : jamais. */
|
|
76
|
+
readonly mobile?: SelectMobileMode;
|
|
77
|
+
/** Titre de la feuille basse (défaut : `aria-label`, puis `placeholder`). */
|
|
78
|
+
readonly sheetTitle?: string;
|
|
79
|
+
/** Rendu d'une option (remplace coche, libellé et indice) — `poadSelectItem` en Angular. */
|
|
80
|
+
readonly renderOption?: (option: T, context: PoadSelectOptionContext) => ReactNode;
|
|
81
|
+
/** Rendu de la valeur dans le déclencheur — `poadSelectSelected`. */
|
|
82
|
+
readonly renderValue?: (option: T, context: { readonly label: string }) => ReactNode;
|
|
83
|
+
/** Contenu quand la liste est vide — `poadSelectEmpty`. */
|
|
84
|
+
readonly renderEmpty?: (context: { readonly filtered: boolean; readonly query: string }) => ReactNode;
|
|
85
|
+
/**
|
|
86
|
+
* Icône d'une option (`optionIcon` donne son nom). Absent : l'icône nommée du jeu de la bibliothèque (`PoadIcon`, 14 px),
|
|
87
|
+
* comme `poad-select` d'Angular.
|
|
88
|
+
*/
|
|
89
|
+
readonly renderIcon?: (name: string) => ReactNode;
|
|
90
|
+
/** Bloc en tête / en pied du panneau — `poadSelectHeader` / `poadSelectFooter`. */
|
|
91
|
+
readonly header?: ReactNode;
|
|
92
|
+
readonly footer?: ReactNode;
|
|
93
|
+
readonly onOpenChange?: (open: boolean) => void;
|
|
94
|
+
readonly onFilter?: (query: string) => void;
|
|
95
|
+
readonly onClear?: () => void;
|
|
96
|
+
readonly onFocusChange?: (focused: boolean) => void;
|
|
97
|
+
/** Identifiant du déclencheur ; celui de la liste en dérive (`<id>-list`). Absent : généré. */
|
|
98
|
+
readonly id?: string;
|
|
99
|
+
/** Le déclencheur `role="combobox"`. */
|
|
100
|
+
readonly ref?: Ref<HTMLDivElement>;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Attribut « focus initial » du piège à focus de la feuille (ui-core). */
|
|
104
|
+
const autofocus = (on: boolean) => (on ? { [FOCUS_AUTOFOCUS_ATTR]: '' } : {});
|
|
105
|
+
|
|
106
|
+
function assignRef<E>(ref: Ref<E> | undefined, el: E | null) {
|
|
107
|
+
if (typeof ref === 'function') ref(el);
|
|
108
|
+
else if (ref) (ref as { current: E | null }).current = el;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function PoadSelect<T = unknown, V = unknown>({
|
|
112
|
+
options, value, defaultValue, onChange, compareWith, dataKey, optionLabel, optionValue, optionDisabled, optionHint, optionIcon,
|
|
113
|
+
optionGroupLabel, optionGroupChildren, placeholder = '', filter, filterPlaceholder, filterBy, autofocusFilter = true, clearable,
|
|
114
|
+
loading, emptyMessage, emptyFilterMessage, size = 'md', invalid, disabled, readOnly, panelMaxHeight = 260, 'aria-label': ariaLabel,
|
|
115
|
+
mobile = 'auto', sheetTitle, renderOption, renderValue, renderEmpty, renderIcon, header, footer, onOpenChange, onFilter, onClear,
|
|
116
|
+
onFocusChange, id: idProp, ref, className, ...rest
|
|
117
|
+
}: PoadSelectProps<T, V>) {
|
|
118
|
+
const { t, text } = useUiI18n();
|
|
119
|
+
const generatedId = useId();
|
|
120
|
+
const id = idProp ?? `poad-select-${generatedId}`;
|
|
121
|
+
const listId = `${id}-list`;
|
|
122
|
+
const [inner, setInner] = useState<V | null>(defaultValue ?? null);
|
|
123
|
+
const controlled = value !== undefined;
|
|
124
|
+
const current = controlled ? value : inner;
|
|
125
|
+
const hasValue = current != null;
|
|
126
|
+
const [state, setState] = useState<SelectState>(SELECT_INITIAL);
|
|
127
|
+
const latest = useRef(state); // état à jour entre deux rendus (évènements rapprochés)
|
|
128
|
+
latest.current = state;
|
|
129
|
+
const [focused, setFocused] = useState(false);
|
|
130
|
+
const [inSheet, setInSheet] = useState(false);
|
|
131
|
+
const [triggerWidth, setTriggerWidth] = useState(0);
|
|
132
|
+
const sheetPreferred = useMediaQuery(SHEET_MOBILE_QUERY);
|
|
133
|
+
const rootEl = useRef<HTMLDivElement | null>(null);
|
|
134
|
+
const triggerEl = useRef<HTMLDivElement | null>(null);
|
|
135
|
+
const floatEl = useRef<HTMLDivElement | null>(null);
|
|
136
|
+
const panelEl = useRef<HTMLDivElement | null>(null);
|
|
137
|
+
const listEl = useRef<HTMLDivElement | null>(null);
|
|
138
|
+
const filterEl = useRef<HTMLInputElement | null>(null);
|
|
139
|
+
|
|
140
|
+
const fields: SelectFields = { optionLabel, optionValue, optionDisabled, optionHint, optionIcon, optionGroupLabel, optionGroupChildren };
|
|
141
|
+
const items = useMemo(
|
|
142
|
+
() => selectItems(options, fields, listId),
|
|
143
|
+
[options, optionLabel, optionValue, optionDisabled, optionHint, optionIcon, optionGroupLabel, optionGroupChildren, listId], // eslint-disable-line react-hooks/exhaustive-deps
|
|
144
|
+
);
|
|
145
|
+
const compare = { compareWith, dataKey };
|
|
146
|
+
const interactive = !disabled && !readOnly;
|
|
147
|
+
const ctx: SelectContext<T> = {
|
|
148
|
+
items, filter: { enabled: !!filter, by: filterBy, grouped: !!optionGroupLabel }, interactive, clearable, modal: inSheet,
|
|
149
|
+
isSelected: (it) => hasValue && selectEquals(it.value, current, compare),
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const run = (effect: SelectEffect<T>) => {
|
|
153
|
+
switch (effect.type) {
|
|
154
|
+
case 'opened':
|
|
155
|
+
setTriggerWidth(triggerEl.current?.getBoundingClientRect().width ?? 0);
|
|
156
|
+
setInSheet(selectUseSheet(mobile, sheetPreferred));
|
|
157
|
+
return onOpenChange?.(true);
|
|
158
|
+
case 'closed':
|
|
159
|
+
setInSheet(false);
|
|
160
|
+
onOpenChange?.(false);
|
|
161
|
+
if (effect.refocus) triggerEl.current?.focus();
|
|
162
|
+
return;
|
|
163
|
+
case 'pick':
|
|
164
|
+
if (selectEquals(effect.item.value, current, compare)) return;
|
|
165
|
+
if (!controlled) setInner(effect.item.value as V);
|
|
166
|
+
return onChange?.(effect.item.value as V);
|
|
167
|
+
case 'clear':
|
|
168
|
+
if (!controlled) setInner(null);
|
|
169
|
+
onChange?.(null);
|
|
170
|
+
return onClear?.();
|
|
171
|
+
case 'filtered':
|
|
172
|
+
return onFilter?.(effect.query);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const send = (event: SelectEvent) => {
|
|
176
|
+
const step = selectTransition(latest.current, event, ctx);
|
|
177
|
+
if (step.state !== latest.current) {
|
|
178
|
+
latest.current = step.state;
|
|
179
|
+
setState(step.state);
|
|
180
|
+
}
|
|
181
|
+
step.effects.forEach(run);
|
|
182
|
+
return step;
|
|
183
|
+
};
|
|
184
|
+
const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
|
|
185
|
+
const step = send({
|
|
186
|
+
type: 'keydown', key: e.key, ctrlKey: e.ctrlKey, metaKey: e.metaKey, altKey: e.altKey,
|
|
187
|
+
inTextField: e.target instanceof HTMLInputElement, time: Date.now(),
|
|
188
|
+
});
|
|
189
|
+
if (step.preventDefault) e.preventDefault();
|
|
190
|
+
if (step.stopPropagation) e.stopPropagation();
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// ── après rendu, dans le navigateur seulement : placer le panneau, fermer au clic extérieur, focus du filtre
|
|
194
|
+
const floating = state.open && !inSheet;
|
|
195
|
+
const { style: floatStyle } = useFloating({
|
|
196
|
+
open: floating, anchor: triggerEl, floating: floatEl, candidates: SELECT_PLACEMENTS, offset: 4, shift: true, deps: [state.query, items],
|
|
197
|
+
});
|
|
198
|
+
// déclencheur, panneau, feuille basse : chacun gère son clic
|
|
199
|
+
useDismiss(state.open, [rootEl], () => send({ type: 'close', refocus: false }));
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (state.open && filter && autofocusFilter && !inSheet) filterEl.current?.focus();
|
|
202
|
+
}, [state.open, inSheet]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
203
|
+
useEffect(() => {
|
|
204
|
+
const list = listEl.current;
|
|
205
|
+
if (!state.open || state.activeIndex < 0 || !list) return;
|
|
206
|
+
const option = list.querySelector<HTMLElement>(`[id="${listId}-opt-${state.activeIndex}"]`);
|
|
207
|
+
if (!option) return;
|
|
208
|
+
const next = selectScrollTop({ top: option.offsetTop, height: option.offsetHeight }, { scrollTop: list.scrollTop, clientHeight: list.clientHeight });
|
|
209
|
+
if (next !== null) list.scrollTop = next;
|
|
210
|
+
}, [state.open, state.activeIndex, listId]);
|
|
211
|
+
|
|
212
|
+
// ── rendu
|
|
213
|
+
const trigger = selectTriggerProps(state, ctx, { id, listId, ariaLabel, invalid, disabled, readonly: readOnly });
|
|
214
|
+
const selected = hasValue ? selectFindOption(items, current, compare) : undefined;
|
|
215
|
+
const visible = selectVisibleItems(state, ctx);
|
|
216
|
+
const filtered = !!state.query;
|
|
217
|
+
const root = selectRootClasses({ open: state.open, disabled, readonly: readOnly, invalid, focused, hasValue, size });
|
|
218
|
+
|
|
219
|
+
const listbox = selectListboxProps(listId);
|
|
220
|
+
const list = (
|
|
221
|
+
<div ref={listEl} className="poad-optlist" role={listbox.role} id={listbox.id} style={{ maxHeight: panelMaxHeight }}>
|
|
222
|
+
{visible.length ? (
|
|
223
|
+
visible.map((it) => {
|
|
224
|
+
if (it.kind === 'group') return <div key={it.id} className="poad-optlist__group" role="presentation" id={it.id}>{it.label}</div>;
|
|
225
|
+
const isSel = ctx.isSelected(it);
|
|
226
|
+
const o = selectOptionProps(it, { active: it.index === state.activeIndex, selected: isSel });
|
|
227
|
+
return (
|
|
228
|
+
<div
|
|
229
|
+
key={it.id} id={o.id} role={o.role} className={classNames('poad-optlist__opt', o.classes)}
|
|
230
|
+
aria-selected={o['aria-selected']} aria-disabled={o['aria-disabled'] ?? undefined}
|
|
231
|
+
onPointerDown={(e) => e.preventDefault()}
|
|
232
|
+
onClick={() => !it.disabled && send({ type: 'pick', item: it })}
|
|
233
|
+
onPointerEnter={() => !it.disabled && send({ type: 'hover', index: it.index })}
|
|
234
|
+
>
|
|
235
|
+
{renderOption ? renderOption(it.option, { selected: isSel, active: o.classes['is-active'], index: it.index, label: it.label }) : (
|
|
236
|
+
<>
|
|
237
|
+
{isSel && <Icon name="check" size={12} strokeWidth={2.6} className="poad-optlist__tick" />}
|
|
238
|
+
{it.icon && (renderIcon ? <span className="poad-optlist__icon">{renderIcon(it.icon)}</span> : <Icon name={it.icon} size={14} className="poad-optlist__icon" />)}
|
|
239
|
+
<span className="poad-optlist__label">{it.label}</span>
|
|
240
|
+
{it.hint && <span className="poad-optlist__hint">{it.hint}</span>}
|
|
241
|
+
</>
|
|
242
|
+
)}
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
})
|
|
246
|
+
) : (
|
|
247
|
+
<div className="poad-optlist__empty" role="presentation">
|
|
248
|
+
{renderEmpty ? renderEmpty({ filtered, query: state.query }) : text(filtered ? emptyFilterMessage : emptyMessage, selectEmptyMessage(filtered))}
|
|
249
|
+
</div>
|
|
250
|
+
)}
|
|
251
|
+
</div>
|
|
252
|
+
);
|
|
253
|
+
const f = selectFilterProps(state, ctx, { listId });
|
|
254
|
+
const search = filter && (
|
|
255
|
+
<div className="poad-select__search">
|
|
256
|
+
<Icon name="search" size={13} />
|
|
257
|
+
<input
|
|
258
|
+
ref={filterEl} type="text" {...autofocus(inSheet)} placeholder={text(filterPlaceholder, 'select.filterPlaceholder')} value={state.query} autoComplete="off"
|
|
259
|
+
spellCheck={false} role={f.role} aria-autocomplete={f['aria-autocomplete']} aria-controls={f['aria-controls']}
|
|
260
|
+
aria-activedescendant={f['aria-activedescendant'] ?? undefined} onChange={(e) => send({ type: 'filter', query: e.target.value })}
|
|
261
|
+
/>
|
|
262
|
+
</div>
|
|
263
|
+
);
|
|
264
|
+
const panel = (
|
|
265
|
+
<div
|
|
266
|
+
ref={panelEl} className={classNames('poad-select__panel', { 'is-sheet': inSheet })} style={inSheet ? undefined : { minWidth: triggerWidth }}
|
|
267
|
+
tabIndex={inSheet ? -1 : undefined} {...autofocus(inSheet && !filter)} onKeyDown={onKeyDown}
|
|
268
|
+
>
|
|
269
|
+
{search}
|
|
270
|
+
{header}
|
|
271
|
+
{list}
|
|
272
|
+
{footer}
|
|
273
|
+
</div>
|
|
274
|
+
);
|
|
275
|
+
const titleId = `${id}-sheet-title`;
|
|
276
|
+
|
|
277
|
+
return (
|
|
278
|
+
<div {...rest} ref={rootEl} className={classNames('poad-select', root, className)}>
|
|
279
|
+
<div
|
|
280
|
+
ref={(el) => { triggerEl.current = el; assignRef(ref, el); }}
|
|
281
|
+
className="poad-select__trigger" role={trigger.role} tabIndex={trigger.tabIndex} id={trigger.id}
|
|
282
|
+
aria-haspopup={trigger['aria-haspopup']} aria-expanded={trigger['aria-expanded']} aria-controls={trigger['aria-controls']}
|
|
283
|
+
aria-activedescendant={trigger['aria-activedescendant'] ?? undefined} aria-label={trigger['aria-label'] ?? undefined}
|
|
284
|
+
aria-invalid={trigger['aria-invalid'] ?? undefined} aria-disabled={trigger['aria-disabled'] ?? undefined}
|
|
285
|
+
aria-readonly={trigger['aria-readonly'] ?? undefined}
|
|
286
|
+
onClick={() => send({ type: 'toggle' })} onKeyDown={onKeyDown}
|
|
287
|
+
onFocus={() => { setFocused(true); onFocusChange?.(true); }}
|
|
288
|
+
onBlur={() => { setFocused(false); onFocusChange?.(false); }}
|
|
289
|
+
>
|
|
290
|
+
<span className={classNames('poad-select__value', { 'is-placeholder': !hasValue })}>
|
|
291
|
+
{selected
|
|
292
|
+
? renderValue
|
|
293
|
+
? renderValue(selected.option, { label: selected.label })
|
|
294
|
+
: <>{selected.icon && (renderIcon ? <span className="poad-select__icon">{renderIcon(selected.icon)}</span> : <Icon name={selected.icon} size={14} className="poad-select__icon" />)}{selected.label}</>
|
|
295
|
+
: hasValue ? selectFallbackLabel(current) : placeholder}
|
|
296
|
+
</span>
|
|
297
|
+
{loading && <Icon name="loader" size={14} spin className="poad-select__spin" />}
|
|
298
|
+
{clearable && hasValue && interactive && (
|
|
299
|
+
<button type="button" className="poad-select__clear" tabIndex={-1} aria-label={t('common.clear')} onClick={(e) => { e.stopPropagation(); send({ type: 'clear' }); }}>
|
|
300
|
+
<Icon name="x" size={12} />
|
|
301
|
+
</button>
|
|
302
|
+
)}
|
|
303
|
+
<Icon name="chevron-down" size={13} className="poad-select__chev" />
|
|
304
|
+
</div>
|
|
305
|
+
{floating && <div ref={floatEl} className="poad-floating" style={floatStyle}>{panel}</div>}
|
|
306
|
+
<PoadSheet
|
|
307
|
+
open={state.open && inSheet} onClose={() => send({ type: 'close' })} titleId={titleId} restoreFocus={false}
|
|
308
|
+
title={selectSheetTitle({ sheetTitle, ariaLabel, placeholder })}
|
|
309
|
+
>
|
|
310
|
+
{inSheet && panel}
|
|
311
|
+
</PoadSheet>
|
|
312
|
+
</div>
|
|
313
|
+
);
|
|
314
|
+
}
|
package/src/skeleton.tsx
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <PoadSkeleton width="70%" /> <PoadSkeleton lines={3} /> <PoadSkeleton shape="circle" width="32px" height="32px" />
|
|
3
|
+
*
|
|
4
|
+
* Squelette de chargement : barres et classes de @poadio/ui-core (`skeletonBars`, `skeletonRootClasses`) — même DOM
|
|
5
|
+
* que `poad-skeleton` d'ui-angular (caché aux technologies d'assistance). Style : `@poadio/ui-core/skeleton.css`.
|
|
6
|
+
* Sans état : PAS de 'use client'.
|
|
7
|
+
*/
|
|
8
|
+
import type { ComponentPropsWithRef } from 'react';
|
|
9
|
+
import { classNames, skeletonBars, skeletonRootClasses, type SkeletonOptions } from '@poadio/ui-core';
|
|
10
|
+
|
|
11
|
+
export interface PoadSkeletonProps extends Omit<ComponentPropsWithRef<'div'>, 'children'>, SkeletonOptions {}
|
|
12
|
+
|
|
13
|
+
export function PoadSkeleton({ width, height, shape, lines, animation, lastWidth, className, ...rest }: PoadSkeletonProps) {
|
|
14
|
+
const options = { width, height, shape, lines, animation, lastWidth };
|
|
15
|
+
return (
|
|
16
|
+
<div {...rest} className={classNames('poad-skeleton', skeletonRootClasses(options), className)} aria-hidden="true">
|
|
17
|
+
{skeletonBars(options).map((bar, i) => <span key={i} className={bar.className} style={{ width: bar.width, height: bar.height }} />)}
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
}
|