@poadio/ui-react 0.0.0-stage → 0.2.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 +101 -0
- package/LICENSE +22 -0
- package/README.md +161 -2
- package/dist/accordion.d.ts +43 -0
- package/dist/accordion.d.ts.map +1 -0
- package/dist/accordion.js +57 -0
- package/dist/accordion.js.map +1 -0
- 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/combo.d.ts +53 -0
- package/dist/combo.d.ts.map +1 -0
- package/dist/combo.js +145 -0
- package/dist/combo.js.map +1 -0
- package/dist/confirm.d.ts +27 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +64 -0
- package/dist/confirm.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/datepicker.d.ts +70 -0
- package/dist/datepicker.d.ts.map +1 -0
- package/dist/datepicker.js +313 -0
- package/dist/datepicker.js.map +1 -0
- package/dist/dialog.d.ts +72 -0
- package/dist/dialog.d.ts.map +1 -0
- package/dist/dialog.js +96 -0
- package/dist/dialog.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 +87 -0
- package/dist/floating.d.ts.map +1 -0
- package/dist/floating.js +237 -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 +67 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -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/menu.d.ts +67 -0
- package/dist/menu.d.ts.map +1 -0
- package/dist/menu.js +151 -0
- package/dist/menu.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/multiselect.d.ts +72 -0
- package/dist/multiselect.d.ts.map +1 -0
- package/dist/multiselect.js +159 -0
- package/dist/multiselect.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/option-list.d.ts +30 -0
- package/dist/option-list.d.ts.map +1 -0
- package/dist/option-list.js +17 -0
- package/dist/option-list.js.map +1 -0
- package/dist/paginator.d.ts +36 -0
- package/dist/paginator.d.ts.map +1 -0
- package/dist/paginator.js +36 -0
- package/dist/paginator.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 +91 -0
- package/dist/select.d.ts.map +1 -0
- package/dist/select.js +150 -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/split-button.d.ts +34 -0
- package/dist/split-button.d.ts.map +1 -0
- package/dist/split-button.js +25 -0
- package/dist/split-button.js.map +1 -0
- package/dist/stepper.d.ts +44 -0
- package/dist/stepper.d.ts.map +1 -0
- package/dist/stepper.js +47 -0
- package/dist/stepper.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/table.d.ts +135 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +169 -0
- package/dist/table.js.map +1 -0
- package/dist/tabs.d.ts +56 -0
- package/dist/tabs.d.ts.map +1 -0
- package/dist/tabs.js +79 -0
- package/dist/tabs.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/toast.d.ts +50 -0
- package/dist/toast.d.ts.map +1 -0
- package/dist/toast.js +90 -0
- package/dist/toast.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/accordion.tsx +110 -0
- 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/combo.tsx +204 -0
- package/src/confirm.tsx +105 -0
- package/src/controllable.ts +11 -0
- package/src/datepicker.tsx +407 -0
- package/src/dialog.tsx +177 -0
- package/src/divider.tsx +21 -0
- package/src/field.tsx +123 -0
- package/src/floating.tsx +309 -0
- package/src/i18n.tsx +54 -0
- package/src/icon.tsx +77 -0
- package/src/index.ts +70 -0
- package/src/inplace.tsx +101 -0
- package/src/mask.ts +142 -0
- package/src/menu.tsx +239 -0
- package/src/message.tsx +42 -0
- package/src/meter.tsx +71 -0
- package/src/multiselect.tsx +298 -0
- package/src/number.tsx +90 -0
- package/src/option-list.tsx +69 -0
- package/src/paginator.tsx +102 -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 +282 -0
- package/src/skeleton.tsx +20 -0
- package/src/slider.tsx +166 -0
- package/src/split-button.tsx +66 -0
- package/src/stepper.tsx +97 -0
- package/src/switch.tsx +79 -0
- package/src/table.tsx +386 -0
- package/src/tabs.tsx +166 -0
- package/src/tag.tsx +28 -0
- package/src/toast.tsx +162 -0
- package/src/toggle-button.tsx +60 -0
- package/src/tooltip.tsx +118 -0
package/src/select.tsx
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
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, 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
|
+
import { OptionList, type PoadSelectOptionContext } from './option-list.js';
|
|
31
|
+
|
|
32
|
+
export type { PoadSelectOptionContext };
|
|
33
|
+
|
|
34
|
+
export interface PoadSelectProps<T = unknown, V = unknown>
|
|
35
|
+
extends Omit<ComponentPropsWithoutRef<'div'>, 'onChange' | 'defaultValue' | 'id' | 'children'>, SelectFields {
|
|
36
|
+
/** Options : primitives (libellé = valeur) ou objets, lus par `optionLabel` / `optionValue` / `optionDisabled`… */
|
|
37
|
+
readonly options?: readonly T[];
|
|
38
|
+
/** Valeur imposée (mode contrôlé) ; `null` = aucune. Absente : le composant garde la valeur, à partir de `defaultValue`. */
|
|
39
|
+
readonly value?: V | null;
|
|
40
|
+
readonly defaultValue?: V | null;
|
|
41
|
+
/** Appelé quand l'UTILISATEUR choisit ou efface (pas quand `value` change). */
|
|
42
|
+
readonly onChange?: (value: V | null) => void;
|
|
43
|
+
/** Comparaison de valeurs (objets). Défaut : `dataKey`, puis `===`. */
|
|
44
|
+
readonly compareWith?: (a: unknown, b: unknown) => boolean;
|
|
45
|
+
readonly dataKey?: string;
|
|
46
|
+
readonly placeholder?: string;
|
|
47
|
+
/** Champ de filtre dans le panneau. */
|
|
48
|
+
readonly filter?: boolean;
|
|
49
|
+
/** Indication du champ de filtre ; absente : message `select.filterPlaceholder` du catalogue. */
|
|
50
|
+
readonly filterPlaceholder?: string;
|
|
51
|
+
/** Chemins (séparés par des virgules) sur lesquels filtrer ; défaut : le libellé. */
|
|
52
|
+
readonly filterBy?: string;
|
|
53
|
+
/** Focus dans le filtre à l'ouverture (défaut : oui, sauf en feuille basse). */
|
|
54
|
+
readonly autofocusFilter?: boolean;
|
|
55
|
+
/** Bouton × et Suppr / Retour arrière pour effacer. */
|
|
56
|
+
readonly clearable?: boolean;
|
|
57
|
+
readonly loading?: boolean;
|
|
58
|
+
/** Absent : message `select.empty` du catalogue. */
|
|
59
|
+
readonly emptyMessage?: string;
|
|
60
|
+
/** Absent : message `select.emptyFilter` du catalogue. */
|
|
61
|
+
readonly emptyFilterMessage?: string;
|
|
62
|
+
readonly size?: 'sm' | 'md' | 'lg';
|
|
63
|
+
readonly invalid?: boolean;
|
|
64
|
+
readonly disabled?: boolean;
|
|
65
|
+
readonly readOnly?: boolean;
|
|
66
|
+
/** Hauteur maximale de la liste (px, défaut 260). */
|
|
67
|
+
readonly panelMaxHeight?: number;
|
|
68
|
+
/** Nom accessible du déclencheur. */
|
|
69
|
+
readonly 'aria-label'?: string;
|
|
70
|
+
/** `auto` : feuille basse sous 768 px ; `sheet` : toujours ; `popover` : jamais. */
|
|
71
|
+
readonly mobile?: SelectMobileMode;
|
|
72
|
+
/** Titre de la feuille basse (défaut : `aria-label`, puis `placeholder`). */
|
|
73
|
+
readonly sheetTitle?: string;
|
|
74
|
+
/** Rendu d'une option (remplace coche, libellé et indice) — `poadSelectItem` en Angular. */
|
|
75
|
+
readonly renderOption?: (option: T, context: PoadSelectOptionContext) => ReactNode;
|
|
76
|
+
/** Rendu de la valeur dans le déclencheur — `poadSelectSelected`. */
|
|
77
|
+
readonly renderValue?: (option: T, context: { readonly label: string }) => ReactNode;
|
|
78
|
+
/** Contenu quand la liste est vide — `poadSelectEmpty`. */
|
|
79
|
+
readonly renderEmpty?: (context: { readonly filtered: boolean; readonly query: string }) => ReactNode;
|
|
80
|
+
/**
|
|
81
|
+
* Icône d'une option (`optionIcon` donne son nom). Absent : l'icône nommée du jeu de la bibliothèque (`PoadIcon`, 14 px),
|
|
82
|
+
* comme `poad-select` d'Angular.
|
|
83
|
+
*/
|
|
84
|
+
readonly renderIcon?: (name: string) => ReactNode;
|
|
85
|
+
/** Bloc en tête / en pied du panneau — `poadSelectHeader` / `poadSelectFooter`. */
|
|
86
|
+
readonly header?: ReactNode;
|
|
87
|
+
readonly footer?: ReactNode;
|
|
88
|
+
readonly onOpenChange?: (open: boolean) => void;
|
|
89
|
+
readonly onFilter?: (query: string) => void;
|
|
90
|
+
readonly onClear?: () => void;
|
|
91
|
+
readonly onFocusChange?: (focused: boolean) => void;
|
|
92
|
+
/** Identifiant du déclencheur ; celui de la liste en dérive (`<id>-list`). Absent : généré. */
|
|
93
|
+
readonly id?: string;
|
|
94
|
+
/** Le déclencheur `role="combobox"`. */
|
|
95
|
+
readonly ref?: Ref<HTMLDivElement>;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Attribut « focus initial » du piège à focus de la feuille (ui-core). */
|
|
99
|
+
const autofocus = (on: boolean) => (on ? { [FOCUS_AUTOFOCUS_ATTR]: '' } : {});
|
|
100
|
+
|
|
101
|
+
function assignRef<E>(ref: Ref<E> | undefined, el: E | null) {
|
|
102
|
+
if (typeof ref === 'function') ref(el);
|
|
103
|
+
else if (ref) (ref as { current: E | null }).current = el;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function PoadSelect<T = unknown, V = unknown>({
|
|
107
|
+
options, value, defaultValue, onChange, compareWith, dataKey, optionLabel, optionValue, optionDisabled, optionHint, optionIcon,
|
|
108
|
+
optionGroupLabel, optionGroupChildren, placeholder = '', filter, filterPlaceholder, filterBy, autofocusFilter = true, clearable,
|
|
109
|
+
loading, emptyMessage, emptyFilterMessage, size = 'md', invalid, disabled, readOnly, panelMaxHeight = 260, 'aria-label': ariaLabel,
|
|
110
|
+
mobile = 'auto', sheetTitle, renderOption, renderValue, renderEmpty, renderIcon, header, footer, onOpenChange, onFilter, onClear,
|
|
111
|
+
onFocusChange, id: idProp, ref, className, ...rest
|
|
112
|
+
}: PoadSelectProps<T, V>) {
|
|
113
|
+
const { t, text } = useUiI18n();
|
|
114
|
+
const generatedId = useId();
|
|
115
|
+
const id = idProp ?? `poad-select-${generatedId}`;
|
|
116
|
+
const listId = `${id}-list`;
|
|
117
|
+
const [inner, setInner] = useState<V | null>(defaultValue ?? null);
|
|
118
|
+
const controlled = value !== undefined;
|
|
119
|
+
const current = controlled ? value : inner;
|
|
120
|
+
const hasValue = current != null;
|
|
121
|
+
const [state, setState] = useState<SelectState>(SELECT_INITIAL);
|
|
122
|
+
const latest = useRef(state); // état à jour entre deux rendus (évènements rapprochés)
|
|
123
|
+
latest.current = state;
|
|
124
|
+
const [focused, setFocused] = useState(false);
|
|
125
|
+
const [inSheet, setInSheet] = useState(false);
|
|
126
|
+
const [triggerWidth, setTriggerWidth] = useState(0);
|
|
127
|
+
const sheetPreferred = useMediaQuery(SHEET_MOBILE_QUERY);
|
|
128
|
+
const rootEl = useRef<HTMLDivElement | null>(null);
|
|
129
|
+
const triggerEl = useRef<HTMLDivElement | null>(null);
|
|
130
|
+
const floatEl = useRef<HTMLDivElement | null>(null);
|
|
131
|
+
const panelEl = useRef<HTMLDivElement | null>(null);
|
|
132
|
+
const listEl = useRef<HTMLDivElement | null>(null);
|
|
133
|
+
const filterEl = useRef<HTMLInputElement | null>(null);
|
|
134
|
+
|
|
135
|
+
const fields: SelectFields = { optionLabel, optionValue, optionDisabled, optionHint, optionIcon, optionGroupLabel, optionGroupChildren };
|
|
136
|
+
const items = useMemo(
|
|
137
|
+
() => selectItems(options, fields, listId),
|
|
138
|
+
[options, optionLabel, optionValue, optionDisabled, optionHint, optionIcon, optionGroupLabel, optionGroupChildren, listId], // eslint-disable-line react-hooks/exhaustive-deps
|
|
139
|
+
);
|
|
140
|
+
const compare = { compareWith, dataKey };
|
|
141
|
+
const interactive = !disabled && !readOnly;
|
|
142
|
+
const ctx: SelectContext<T> = {
|
|
143
|
+
items, filter: { enabled: !!filter, by: filterBy, grouped: !!optionGroupLabel }, interactive, clearable, modal: inSheet,
|
|
144
|
+
isSelected: (it) => hasValue && selectEquals(it.value, current, compare),
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const run = (effect: SelectEffect<T>) => {
|
|
148
|
+
switch (effect.type) {
|
|
149
|
+
case 'opened':
|
|
150
|
+
setTriggerWidth(triggerEl.current?.getBoundingClientRect().width ?? 0);
|
|
151
|
+
setInSheet(selectUseSheet(mobile, sheetPreferred));
|
|
152
|
+
return onOpenChange?.(true);
|
|
153
|
+
case 'closed':
|
|
154
|
+
setInSheet(false);
|
|
155
|
+
onOpenChange?.(false);
|
|
156
|
+
if (effect.refocus) triggerEl.current?.focus();
|
|
157
|
+
return;
|
|
158
|
+
case 'pick':
|
|
159
|
+
if (selectEquals(effect.item.value, current, compare)) return;
|
|
160
|
+
if (!controlled) setInner(effect.item.value as V);
|
|
161
|
+
return onChange?.(effect.item.value as V);
|
|
162
|
+
case 'clear':
|
|
163
|
+
if (!controlled) setInner(null);
|
|
164
|
+
onChange?.(null);
|
|
165
|
+
return onClear?.();
|
|
166
|
+
case 'filtered':
|
|
167
|
+
return onFilter?.(effect.query);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const send = (event: SelectEvent) => {
|
|
171
|
+
const step = selectTransition(latest.current, event, ctx);
|
|
172
|
+
if (step.state !== latest.current) {
|
|
173
|
+
latest.current = step.state;
|
|
174
|
+
setState(step.state);
|
|
175
|
+
}
|
|
176
|
+
step.effects.forEach(run);
|
|
177
|
+
return step;
|
|
178
|
+
};
|
|
179
|
+
const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {
|
|
180
|
+
const step = send({
|
|
181
|
+
type: 'keydown', key: e.key, ctrlKey: e.ctrlKey, metaKey: e.metaKey, altKey: e.altKey,
|
|
182
|
+
inTextField: e.target instanceof HTMLInputElement, time: Date.now(),
|
|
183
|
+
});
|
|
184
|
+
if (step.preventDefault) e.preventDefault();
|
|
185
|
+
if (step.stopPropagation) e.stopPropagation();
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// ── après rendu, dans le navigateur seulement : placer le panneau, fermer au clic extérieur, focus du filtre
|
|
189
|
+
const floating = state.open && !inSheet;
|
|
190
|
+
const { style: floatStyle } = useFloating({
|
|
191
|
+
open: floating, anchor: triggerEl, floating: floatEl, candidates: SELECT_PLACEMENTS, offset: 4, shift: true, deps: [state.query, items],
|
|
192
|
+
});
|
|
193
|
+
// déclencheur, panneau, feuille basse : chacun gère son clic
|
|
194
|
+
useDismiss(state.open, [rootEl], () => send({ type: 'close', refocus: false }));
|
|
195
|
+
useEffect(() => {
|
|
196
|
+
if (state.open && filter && autofocusFilter && !inSheet) filterEl.current?.focus();
|
|
197
|
+
}, [state.open, inSheet]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
const list = listEl.current;
|
|
200
|
+
if (!state.open || state.activeIndex < 0 || !list) return;
|
|
201
|
+
const option = list.querySelector<HTMLElement>(`[id="${listId}-opt-${state.activeIndex}"]`);
|
|
202
|
+
if (!option) return;
|
|
203
|
+
const next = selectScrollTop({ top: option.offsetTop, height: option.offsetHeight }, { scrollTop: list.scrollTop, clientHeight: list.clientHeight });
|
|
204
|
+
if (next !== null) list.scrollTop = next;
|
|
205
|
+
}, [state.open, state.activeIndex, listId]);
|
|
206
|
+
|
|
207
|
+
// ── rendu
|
|
208
|
+
const trigger = selectTriggerProps(state, ctx, { id, listId, ariaLabel, invalid, disabled, readonly: readOnly });
|
|
209
|
+
const selected = hasValue ? selectFindOption(items, current, compare) : undefined;
|
|
210
|
+
const visible = selectVisibleItems(state, ctx);
|
|
211
|
+
const filtered = !!state.query;
|
|
212
|
+
const root = selectRootClasses({ open: state.open, disabled, readonly: readOnly, invalid, focused, hasValue, size });
|
|
213
|
+
|
|
214
|
+
const list = (
|
|
215
|
+
<OptionList
|
|
216
|
+
listRef={listEl} listId={listId} items={visible} activeIndex={state.activeIndex} isSelected={ctx.isSelected} maxHeight={panelMaxHeight}
|
|
217
|
+
renderOption={renderOption} renderIcon={renderIcon} onPick={(item) => send({ type: 'pick', item })} onHover={(index) => send({ type: 'hover', index })}
|
|
218
|
+
empty={renderEmpty ? renderEmpty({ filtered, query: state.query }) : text(filtered ? emptyFilterMessage : emptyMessage, selectEmptyMessage(filtered))}
|
|
219
|
+
/>
|
|
220
|
+
);
|
|
221
|
+
const f = selectFilterProps(state, ctx, { listId });
|
|
222
|
+
const search = filter && (
|
|
223
|
+
<div className="poad-select__search">
|
|
224
|
+
<Icon name="search" size={13} />
|
|
225
|
+
<input
|
|
226
|
+
ref={filterEl} type="text" {...autofocus(inSheet)} placeholder={text(filterPlaceholder, 'select.filterPlaceholder')} value={state.query} autoComplete="off"
|
|
227
|
+
spellCheck={false} role={f.role} aria-autocomplete={f['aria-autocomplete']} aria-controls={f['aria-controls']}
|
|
228
|
+
aria-activedescendant={f['aria-activedescendant'] ?? undefined} onChange={(e) => send({ type: 'filter', query: e.target.value })}
|
|
229
|
+
/>
|
|
230
|
+
</div>
|
|
231
|
+
);
|
|
232
|
+
const panel = (
|
|
233
|
+
<div
|
|
234
|
+
ref={panelEl} className={classNames('poad-select__panel', { 'is-sheet': inSheet })} style={inSheet ? undefined : { minWidth: triggerWidth }}
|
|
235
|
+
tabIndex={inSheet ? -1 : undefined} {...autofocus(inSheet && !filter)} onKeyDown={onKeyDown}
|
|
236
|
+
>
|
|
237
|
+
{search}
|
|
238
|
+
{header}
|
|
239
|
+
{list}
|
|
240
|
+
{footer}
|
|
241
|
+
</div>
|
|
242
|
+
);
|
|
243
|
+
const titleId = `${id}-sheet-title`;
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
<div {...rest} ref={rootEl} className={classNames('poad-select', root, className)}>
|
|
247
|
+
<div
|
|
248
|
+
ref={(el) => { triggerEl.current = el; assignRef(ref, el); }}
|
|
249
|
+
className="poad-select__trigger" role={trigger.role} tabIndex={trigger.tabIndex} id={trigger.id}
|
|
250
|
+
aria-haspopup={trigger['aria-haspopup']} aria-expanded={trigger['aria-expanded']} aria-controls={trigger['aria-controls']}
|
|
251
|
+
aria-activedescendant={trigger['aria-activedescendant'] ?? undefined} aria-label={trigger['aria-label'] ?? undefined}
|
|
252
|
+
aria-invalid={trigger['aria-invalid'] ?? undefined} aria-disabled={trigger['aria-disabled'] ?? undefined}
|
|
253
|
+
aria-readonly={trigger['aria-readonly'] ?? undefined}
|
|
254
|
+
onClick={() => send({ type: 'toggle' })} onKeyDown={onKeyDown}
|
|
255
|
+
onFocus={() => { setFocused(true); onFocusChange?.(true); }}
|
|
256
|
+
onBlur={() => { setFocused(false); onFocusChange?.(false); }}
|
|
257
|
+
>
|
|
258
|
+
<span className={classNames('poad-select__value', { 'is-placeholder': !hasValue })}>
|
|
259
|
+
{selected
|
|
260
|
+
? renderValue
|
|
261
|
+
? renderValue(selected.option, { label: selected.label })
|
|
262
|
+
: <>{selected.icon && (renderIcon ? <span className="poad-select__icon">{renderIcon(selected.icon)}</span> : <Icon name={selected.icon} size={14} className="poad-select__icon" />)}{selected.label}</>
|
|
263
|
+
: hasValue ? selectFallbackLabel(current) : placeholder}
|
|
264
|
+
</span>
|
|
265
|
+
{loading && <Icon name="loader" size={14} spin className="poad-select__spin" />}
|
|
266
|
+
{clearable && hasValue && interactive && (
|
|
267
|
+
<button type="button" className="poad-select__clear" tabIndex={-1} aria-label={t('common.clear')} onClick={(e) => { e.stopPropagation(); send({ type: 'clear' }); }}>
|
|
268
|
+
<Icon name="x" size={12} />
|
|
269
|
+
</button>
|
|
270
|
+
)}
|
|
271
|
+
<Icon name="chevron-down" size={13} className="poad-select__chev" />
|
|
272
|
+
</div>
|
|
273
|
+
{floating && <div ref={floatEl} className="poad-floating" style={floatStyle}>{panel}</div>}
|
|
274
|
+
<PoadSheet
|
|
275
|
+
open={state.open && inSheet} onClose={() => send({ type: 'close' })} titleId={titleId} restoreFocus={false}
|
|
276
|
+
title={selectSheetTitle({ sheetTitle, ariaLabel, placeholder })}
|
|
277
|
+
>
|
|
278
|
+
{inSheet && panel}
|
|
279
|
+
</PoadSheet>
|
|
280
|
+
</div>
|
|
281
|
+
);
|
|
282
|
+
}
|
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
|
+
}
|
package/src/slider.tsx
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadSlider value={v} onValueChange={setV} min={0} max={100} step={5} showValue="always" marks="steps" />
|
|
4
|
+
* <PoadSlider range defaultValue={[20, 60]} marks={[0, 25, 50, 75, 100]} onChange={save} />
|
|
5
|
+
*
|
|
6
|
+
* La logique (valeurs bornées et au pas, intervalle sans croisement, positions, graduations, valeur sous le pointeur,
|
|
7
|
+
* poignée la plus proche, ← → ↑ ↓ / PageUp / PageDown / Home / End avec Maj × 10, miroir horizontal de droite à
|
|
8
|
+
* gauche, règle d'émission pendant un glissement, attributs des poignées) est `slider` de @poadio/ui-core ; ce
|
|
9
|
+
* composant écoute le pointeur et le clavier. Même DOM, mêmes classes, même ARIA que `poad-slider` d'ui-angular.
|
|
10
|
+
* Style : `@poadio/ui-core/slider.css`.
|
|
11
|
+
*
|
|
12
|
+
* Évènements : `onValueChange` (la valeur suit le pointeur ; avec `lazy`, seulement au relâchement), `onChange` (fin
|
|
13
|
+
* d'un geste, si la valeur a changé), `onSliding` (chaque mouvement), `onSlideStart`, `onSlideEnd`. `ref` désigne la
|
|
14
|
+
* racine `<div class="poad-slider">`, qui reçoit aussi `className` et les autres props.
|
|
15
|
+
*/
|
|
16
|
+
import { useId, useRef, useState, type ComponentPropsWithoutRef, type CSSProperties, type KeyboardEvent, type PointerEvent as ReactPointerEvent, type Ref } from 'react';
|
|
17
|
+
import {
|
|
18
|
+
SLIDER_KEYS, classNames, elementIsRtl, sliderApply, sliderBubbleVisible, sliderCompose, sliderFill, sliderFillSize, sliderFormat, sliderHandleId,
|
|
19
|
+
sliderHandleProps, sliderKeyTarget, sliderLabelId, sliderMarks, sliderNearest, sliderPercent, sliderPosition, sliderRootClasses, sliderValueAt,
|
|
20
|
+
sliderValueOf, sliderValues, type SliderMark, type SliderOptions, type SliderSeverity, type SliderShowValue, type SliderValue,
|
|
21
|
+
} from '@poadio/ui-core';
|
|
22
|
+
import { useControllable } from './controllable.js';
|
|
23
|
+
|
|
24
|
+
export interface PoadSliderProps extends Omit<ComponentPropsWithoutRef<'div'>, 'onChange' | 'defaultValue' | 'id'>, SliderOptions {
|
|
25
|
+
readonly value?: SliderValue | null;
|
|
26
|
+
readonly defaultValue?: SliderValue | null;
|
|
27
|
+
/** La valeur change (pendant le glissement ; avec `lazy`, au relâchement). */
|
|
28
|
+
readonly onValueChange?: (value: SliderValue) => void;
|
|
29
|
+
/** Fin d'un geste (relâchement, touche), si la valeur a changé. */
|
|
30
|
+
readonly onChange?: (value: SliderValue) => void;
|
|
31
|
+
readonly onSliding?: (value: SliderValue) => void;
|
|
32
|
+
readonly onSlideStart?: (value: SliderValue) => void;
|
|
33
|
+
readonly onSlideEnd?: (value: SliderValue) => void;
|
|
34
|
+
/** N'écrit la valeur qu'au relâchement (un brouillon s'affiche pendant). */
|
|
35
|
+
readonly lazy?: boolean;
|
|
36
|
+
readonly showValue?: SliderShowValue;
|
|
37
|
+
readonly formatValue?: (v: number) => string;
|
|
38
|
+
readonly marks?: readonly SliderMark[] | 'steps' | null;
|
|
39
|
+
readonly showTicks?: boolean;
|
|
40
|
+
readonly disabled?: boolean;
|
|
41
|
+
readonly readOnly?: boolean;
|
|
42
|
+
readonly invalid?: boolean;
|
|
43
|
+
readonly size?: 'sm' | 'md';
|
|
44
|
+
readonly severity?: SliderSeverity;
|
|
45
|
+
/** Libellé visible ; absent : donner `aria-label`. */
|
|
46
|
+
readonly label?: string;
|
|
47
|
+
readonly 'aria-label'?: string;
|
|
48
|
+
/** Préfixe des identifiants (libellé, poignées). Absent : généré. */
|
|
49
|
+
readonly id?: string;
|
|
50
|
+
readonly ref?: Ref<HTMLDivElement>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Style = CSSProperties & Record<string, string>;
|
|
54
|
+
/** Propriété CSS du socle (`inset-inline-start`) → nom de style React (`insetInlineStart`). */
|
|
55
|
+
const camel = (property: string): string => property.replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
|
|
56
|
+
|
|
57
|
+
export function PoadSlider({
|
|
58
|
+
value, defaultValue = null, onValueChange, onChange, onSliding, onSlideStart, onSlideEnd, lazy, showValue = 'drag', formatValue, marks, showTicks,
|
|
59
|
+
disabled, readOnly, invalid, size = 'md', severity = 'primary', label, 'aria-label': ariaLabel, id: idProp, min = 0, max = 100, step = 1, range,
|
|
60
|
+
orientation = 'horizontal', ref, className, ...rest
|
|
61
|
+
}: PoadSliderProps) {
|
|
62
|
+
const generatedId = useId();
|
|
63
|
+
const id = idProp ?? generatedId;
|
|
64
|
+
const rootEl = useRef<HTMLDivElement | null>(null);
|
|
65
|
+
const trackEl = useRef<HTMLDivElement>(null);
|
|
66
|
+
const [current, setCurrent] = useControllable(value, defaultValue);
|
|
67
|
+
const [draft, setDraft] = useState<SliderValue | null>(null);
|
|
68
|
+
const [dragging, setDragging] = useState<number | null>(null);
|
|
69
|
+
const [focused, setFocused] = useState<number | null>(null);
|
|
70
|
+
const [hovered, setHovered] = useState<number | null>(null);
|
|
71
|
+
const options: SliderOptions = { min, max, step, range, orientation };
|
|
72
|
+
const vals = sliderValues(draft ?? current, options);
|
|
73
|
+
const markList = sliderMarks(marks, options);
|
|
74
|
+
const editable = !disabled && !readOnly;
|
|
75
|
+
const element = { ...options, id, label, ariaLabel, invalid, disabled, formatValue };
|
|
76
|
+
|
|
77
|
+
// Ce que lisent les gestionnaires du document pendant un glissement : la dernière valeur rendue.
|
|
78
|
+
const live = useRef({ vals, value: current ?? null, draft, options, lazy, start: null as SliderValue | null });
|
|
79
|
+
live.current = { ...live.current, vals, value: current ?? null, draft, options, lazy };
|
|
80
|
+
|
|
81
|
+
const apply = (next: SliderValue, final: boolean) => {
|
|
82
|
+
const l = live.current;
|
|
83
|
+
const fx = sliderApply({ value: l.value, draft: l.draft, lazy: l.lazy, start: l.start ?? undefined }, next, final);
|
|
84
|
+
l.draft = fx.draft;
|
|
85
|
+
setDraft(fx.draft);
|
|
86
|
+
onSliding?.(fx.sliding);
|
|
87
|
+
if (fx.commit != null) { l.value = fx.commit; l.vals = sliderValues(fx.commit, l.options); setCurrent(fx.commit); onValueChange?.(fx.commit); }
|
|
88
|
+
else if (fx.draft != null) l.vals = sliderValues(fx.draft, l.options);
|
|
89
|
+
if (fx.changed != null) onChange?.(fx.changed);
|
|
90
|
+
};
|
|
91
|
+
const valueAt = (x: number, y: number) => sliderValueAt({ x, y }, trackEl.current!.getBoundingClientRect(), live.current.options, elementIsRtl(rootEl.current));
|
|
92
|
+
|
|
93
|
+
const startDrag = (e: ReactPointerEvent, index: number, initial: number | null) => {
|
|
94
|
+
e.preventDefault();
|
|
95
|
+
setDragging(index);
|
|
96
|
+
const doc = rootEl.current!.ownerDocument;
|
|
97
|
+
live.current.start = sliderValueOf(live.current.vals, options);
|
|
98
|
+
doc.getElementById(sliderHandleId(id, index))?.focus({ preventScroll: true });
|
|
99
|
+
onSlideStart?.(sliderValueOf(live.current.vals, options));
|
|
100
|
+
if (initial != null) apply(sliderCompose(index, initial, live.current.vals, options), false);
|
|
101
|
+
const move = (ev: PointerEvent) => apply(sliderCompose(index, valueAt(ev.clientX, ev.clientY), live.current.vals, live.current.options), false);
|
|
102
|
+
const up = () => {
|
|
103
|
+
doc.removeEventListener('pointermove', move); doc.removeEventListener('pointerup', up); doc.removeEventListener('pointercancel', up);
|
|
104
|
+
const v = sliderValueOf(live.current.vals, live.current.options);
|
|
105
|
+
apply(v, true);
|
|
106
|
+
live.current.start = null;
|
|
107
|
+
setDragging(null);
|
|
108
|
+
onSlideEnd?.(v);
|
|
109
|
+
};
|
|
110
|
+
doc.addEventListener('pointermove', move); doc.addEventListener('pointerup', up); doc.addEventListener('pointercancel', up);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const onKey = (e: KeyboardEvent, index: number) => {
|
|
114
|
+
if (!editable || !SLIDER_KEYS.includes(e.key)) return;
|
|
115
|
+
const next = sliderKeyTarget(vals[index]!, e.key, { ...options, shiftKey: e.shiftKey, rtl: elementIsRtl(rootEl.current) });
|
|
116
|
+
if (next == null) return;
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
apply(sliderCompose(index, next, vals, options), true);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const pos = (v: number): Style => { const p = sliderPosition(sliderPercent(v, options), options); return { [camel(p.property)]: p.value } as Style; };
|
|
122
|
+
const fill = sliderFill(vals, options);
|
|
123
|
+
const fillStart = sliderPosition(fill.start, options), fillSize = sliderFillSize(fill.size, options);
|
|
124
|
+
const fmt = (v: number) => sliderFormat(v, formatValue);
|
|
125
|
+
|
|
126
|
+
return (
|
|
127
|
+
<div
|
|
128
|
+
{...rest}
|
|
129
|
+
ref={(el) => { rootEl.current = el; if (typeof ref === 'function') ref(el); else if (ref) (ref as { current: HTMLDivElement | null }).current = el; }}
|
|
130
|
+
className={classNames('poad-slider', sliderRootClasses({ ...options, disabled, readonly: readOnly, invalid, dragging: dragging !== null, size, severity, hasMarks: markList.length > 0 }), className)}
|
|
131
|
+
>
|
|
132
|
+
{label && <span className="poad-slider__label" id={sliderLabelId(id)}>{label}</span>}
|
|
133
|
+
<div className="poad-slider__wrap">
|
|
134
|
+
<div
|
|
135
|
+
ref={trackEl} className="poad-slider__track"
|
|
136
|
+
onPointerDown={(e) => { if (!editable) return; const v = valueAt(e.clientX, e.clientY); startDrag(e, sliderNearest(v, vals, options), v); }}
|
|
137
|
+
>
|
|
138
|
+
<div className="poad-slider__fill" style={{ [camel(fillStart.property)]: fillStart.value, [camel(fillSize.property)]: fillSize.value } as Style} />
|
|
139
|
+
{showTicks && markList.map((m) => <i key={m.value} className="poad-slider__tick" style={pos(m.value)} />)}
|
|
140
|
+
{vals.map((v, index) => {
|
|
141
|
+
const p = sliderHandleProps(index, vals, element);
|
|
142
|
+
return (
|
|
143
|
+
<button
|
|
144
|
+
key={index} type="button" className={classNames('poad-slider__handle', { 'is-active': dragging === index || focused === index })}
|
|
145
|
+
role={p.role} id={p.id} tabIndex={p.tabindex} aria-valuemin={p['aria-valuemin']} aria-valuemax={p['aria-valuemax']} aria-valuenow={p['aria-valuenow']}
|
|
146
|
+
aria-valuetext={p['aria-valuetext']} aria-orientation={p['aria-orientation']} aria-label={p['aria-label'] ?? undefined}
|
|
147
|
+
aria-labelledby={p['aria-labelledby'] ?? undefined} aria-disabled={p['aria-disabled'] ?? undefined} aria-invalid={p['aria-invalid'] ?? undefined}
|
|
148
|
+
style={pos(v)}
|
|
149
|
+
onPointerDown={(e) => { if (!editable) return; e.stopPropagation(); startDrag(e, index, null); }}
|
|
150
|
+
onKeyDown={(e) => onKey(e, index)} onFocus={() => setFocused(index)} onBlur={() => setFocused(null)}
|
|
151
|
+
onPointerEnter={() => setHovered(index)} onPointerLeave={() => setHovered(null)}
|
|
152
|
+
>
|
|
153
|
+
{sliderBubbleVisible(showValue, index, { dragging, focused, hovered }) && <span className="poad-slider__bubble">{fmt(v)}</span>}
|
|
154
|
+
</button>
|
|
155
|
+
);
|
|
156
|
+
})}
|
|
157
|
+
</div>
|
|
158
|
+
{markList.length > 0 && (
|
|
159
|
+
<div className="poad-slider__marks">
|
|
160
|
+
{markList.map((m) => <span key={m.value} className="poad-slider__mark" style={pos(m.value)}>{m.label ?? fmt(m.value)}</span>)}
|
|
161
|
+
</div>
|
|
162
|
+
)}
|
|
163
|
+
</div>
|
|
164
|
+
</div>
|
|
165
|
+
);
|
|
166
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadSplitButton label="Save" icon="save" items={[{ label: 'Save and close', command: close }]} onClick={save} />
|
|
4
|
+
*
|
|
5
|
+
* Bouton principal + bouton chevron accolé qui ouvre un `PoadMenu` flottant ancré sur lui. Mêmes options d'apparence
|
|
6
|
+
* que `PoadButton` (`severity`, `variant`, `size`, `loading`, `disabled`). Attributs du chevron (`aria-haspopup="menu"`,
|
|
7
|
+
* `aria-expanded`) et tailles d'icône : `menu` de @poadio/ui-core. Même DOM que `poad-split-button` d'ui-angular.
|
|
8
|
+
* Styles : `@poadio/ui-core/menu.css`, `button.css` (et ceux du menu). `ref`, `className` : la racine.
|
|
9
|
+
*/
|
|
10
|
+
import { useRef, useState, type MouseEvent, type ReactNode, type Ref } from 'react';
|
|
11
|
+
import { classNames, splitButtonIconSize, splitButtonToggleProps, type ButtonSeverity, type ButtonSize, type ButtonVariant } from '@poadio/ui-core';
|
|
12
|
+
import { PoadButton } from './button.js';
|
|
13
|
+
import { useUiI18n } from './i18n.js';
|
|
14
|
+
import { PoadIcon } from './icon.js';
|
|
15
|
+
import { PoadMenu, type PoadMenuItem, type PoadMenuItemClick } from './menu.js';
|
|
16
|
+
|
|
17
|
+
export interface PoadSplitButtonProps {
|
|
18
|
+
readonly label?: string;
|
|
19
|
+
readonly icon?: string;
|
|
20
|
+
readonly items?: readonly PoadMenuItem[];
|
|
21
|
+
readonly severity?: ButtonSeverity;
|
|
22
|
+
readonly variant?: ButtonVariant;
|
|
23
|
+
readonly size?: ButtonSize;
|
|
24
|
+
readonly loading?: boolean;
|
|
25
|
+
readonly disabled?: boolean;
|
|
26
|
+
/** Nom accessible du chevron ; absent : message `nav.moreActions` du catalogue. */
|
|
27
|
+
readonly dropdownAriaLabel?: string;
|
|
28
|
+
readonly menuMinWidth?: number;
|
|
29
|
+
readonly mobile?: 'auto' | 'sheet' | 'popover';
|
|
30
|
+
/** Clic sur le bouton principal. */
|
|
31
|
+
readonly onClick?: (e: MouseEvent<HTMLButtonElement>) => void;
|
|
32
|
+
readonly onItemClick?: (click: PoadMenuItemClick) => void;
|
|
33
|
+
readonly onOpenChange?: (open: boolean) => void;
|
|
34
|
+
readonly className?: string;
|
|
35
|
+
readonly ref?: Ref<HTMLDivElement>;
|
|
36
|
+
readonly children?: ReactNode;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function PoadSplitButton({
|
|
40
|
+
label, icon, items = [], severity = 'primary', variant = 'solid', size = 'md', loading, disabled, dropdownAriaLabel, menuMinWidth = 180, mobile = 'auto',
|
|
41
|
+
onClick, onItemClick, onOpenChange, className, ref, children,
|
|
42
|
+
}: PoadSplitButtonProps) {
|
|
43
|
+
const { text } = useUiI18n();
|
|
44
|
+
const [open, setOpen] = useState(false);
|
|
45
|
+
const toggleEl = useRef<HTMLButtonElement | null>(null);
|
|
46
|
+
const iconSize = splitButtonIconSize(size);
|
|
47
|
+
const tg = splitButtonToggleProps(open, disabled, loading);
|
|
48
|
+
return (
|
|
49
|
+
<div ref={ref} className={classNames('poad-split-button', { 'is-disabled': !!disabled }, className)}>
|
|
50
|
+
<PoadButton className="poad-split-button__main" severity={severity} variant={variant} size={size} loading={loading} disabled={disabled} onClick={onClick}>
|
|
51
|
+
{icon && <PoadIcon name={icon} size={iconSize} />}{label && <span>{label}</span>}{children}
|
|
52
|
+
</PoadButton>
|
|
53
|
+
<PoadButton
|
|
54
|
+
ref={toggleEl} className="poad-split-button__toggle" severity={severity} variant={variant} size={size} disabled={tg.disabled} icon
|
|
55
|
+
aria-label={text(dropdownAriaLabel, 'nav.moreActions')} aria-haspopup={tg['aria-haspopup']} aria-expanded={tg['aria-expanded']}
|
|
56
|
+
onClick={() => { setOpen(!open); onOpenChange?.(!open); }}
|
|
57
|
+
>
|
|
58
|
+
<PoadIcon name="chevron-down" size={iconSize - 2} />
|
|
59
|
+
</PoadButton>
|
|
60
|
+
<PoadMenu
|
|
61
|
+
items={items} anchor={toggleEl} open={open} onOpenChange={(o) => { setOpen(o); onOpenChange?.(o); }} minWidth={menuMinWidth} mobile={mobile}
|
|
62
|
+
onItemClick={onItemClick}
|
|
63
|
+
/>
|
|
64
|
+
</div>
|
|
65
|
+
);
|
|
66
|
+
}
|
package/src/stepper.tsx
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
/**
|
|
3
|
+
* <PoadStepper activeIndex={step} onActiveIndexChange={setStep} linear aria-label="Deployment">
|
|
4
|
+
* <PoadStep label="Domain" description="DNS" completed={dnsOk}>…contenu…</PoadStep>
|
|
5
|
+
* <PoadStep label="Certificate" optional>…</PoadStep>
|
|
6
|
+
* <PoadStep label="Go live" />
|
|
7
|
+
* </PoadStepper>
|
|
8
|
+
*
|
|
9
|
+
* Étapes : machine `stepper` de @poadio/ui-core (état affiché de chaque étape, étapes atteignables en `linear`,
|
|
10
|
+
* `aria-current="step"`). Étape courante contrôlée (`activeIndex`) ou non (`defaultActiveIndex`) ; l'application avance
|
|
11
|
+
* en changeant `activeIndex` (les boutons « suivant » sont les siens). Même DOM, mêmes classes, même ARIA que
|
|
12
|
+
* `poad-stepper` d'ui-angular. Style : `@poadio/ui-core/stepper.css` (et `icon.css`). `PoadStep` ne rend rien seul.
|
|
13
|
+
*/
|
|
14
|
+
import { Children, isValidElement, type ComponentPropsWithRef, type ReactElement, type ReactNode } from 'react';
|
|
15
|
+
import { classNames, stepperGoTo, stepperItemProps, stepperRootClasses, type StepItem, type StepState } from '@poadio/ui-core';
|
|
16
|
+
import { useControllable } from './controllable.js';
|
|
17
|
+
import { useUiI18n } from './i18n.js';
|
|
18
|
+
import { PoadIcon } from './icon.js';
|
|
19
|
+
|
|
20
|
+
export interface PoadStepProps {
|
|
21
|
+
readonly label?: string;
|
|
22
|
+
readonly description?: string;
|
|
23
|
+
readonly icon?: string;
|
|
24
|
+
readonly optional?: boolean;
|
|
25
|
+
readonly disabled?: boolean;
|
|
26
|
+
/** Étape validée (utilisé par `linear`). */
|
|
27
|
+
readonly completed?: boolean;
|
|
28
|
+
/** État forcé ; sinon dérivé de l'étape courante. */
|
|
29
|
+
readonly state?: StepState | null;
|
|
30
|
+
readonly children?: ReactNode;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Une étape : déclaration lue par `PoadStepper` (ne rend rien seule). */
|
|
34
|
+
export function PoadStep(_: PoadStepProps): null {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface PoadStepperProps extends Omit<ComponentPropsWithRef<'div'>, 'onChange'> {
|
|
39
|
+
readonly activeIndex?: number;
|
|
40
|
+
readonly defaultActiveIndex?: number;
|
|
41
|
+
/** L'UTILISATEUR va à une autre étape (clic). */
|
|
42
|
+
readonly onActiveIndexChange?: (index: number, from: number) => void;
|
|
43
|
+
/** Clic sur une étape (même non atteignable par `clickable`). */
|
|
44
|
+
readonly onStepClick?: (index: number) => void;
|
|
45
|
+
readonly linear?: boolean;
|
|
46
|
+
/** Un clic sur une étape y va (défaut oui). */
|
|
47
|
+
readonly clickable?: boolean;
|
|
48
|
+
readonly orientation?: 'horizontal' | 'vertical';
|
|
49
|
+
readonly size?: 'sm' | 'md';
|
|
50
|
+
readonly 'aria-label'?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function PoadStepper({
|
|
54
|
+
activeIndex: indexProp, defaultActiveIndex = 0, onActiveIndexChange, onStepClick, linear, clickable = true, orientation = 'horizontal', size = 'md',
|
|
55
|
+
'aria-label': ariaLabel, className, children, ref, ...rest
|
|
56
|
+
}: PoadStepperProps) {
|
|
57
|
+
const { t } = useUiI18n();
|
|
58
|
+
const [active, setActive] = useControllable<number>(indexProp, defaultActiveIndex);
|
|
59
|
+
const steps = Children.toArray(children).filter((c): c is ReactElement<PoadStepProps> => isValidElement(c) && c.type === PoadStep).map((c) => c.props);
|
|
60
|
+
const items: StepItem[] = steps.map((s) => ({ disabled: s.disabled, completed: s.completed, optional: s.optional, state: s.state }));
|
|
61
|
+
const click = (i: number) => {
|
|
62
|
+
onStepClick?.(i);
|
|
63
|
+
if (!clickable) return;
|
|
64
|
+
const to = stepperGoTo(items, active, i, linear);
|
|
65
|
+
if (to === null) return;
|
|
66
|
+
setActive(to);
|
|
67
|
+
onActiveIndexChange?.(to, active);
|
|
68
|
+
};
|
|
69
|
+
const vertical = orientation === 'vertical';
|
|
70
|
+
return (
|
|
71
|
+
<div {...rest} ref={ref} className={classNames('poad-stepper', stepperRootClasses({ orientation, size }), className)}>
|
|
72
|
+
<ol className="poad-stepper__list" aria-label={ariaLabel || undefined}>
|
|
73
|
+
{steps.map((s, i) => {
|
|
74
|
+
const a = stepperItemProps(items, i, active, { linear, clickable });
|
|
75
|
+
return (
|
|
76
|
+
<li key={i} className={classNames('poad-stepper__item', a.classes)} data-state={a.state}>
|
|
77
|
+
<button type="button" className="poad-stepper__btn" disabled={a.disabled} aria-current={a['aria-current'] ?? undefined} onClick={() => click(i)}>
|
|
78
|
+
<span className="poad-stepper__dot" aria-hidden="true">
|
|
79
|
+
{a.state === 'done' ? <PoadIcon name="check" size={11} strokeWidth={3} />
|
|
80
|
+
: a.state === 'error' ? <PoadIcon name="x" size={11} strokeWidth={3} />
|
|
81
|
+
: s.icon ? <PoadIcon name={s.icon} size={12} /> : i + 1}
|
|
82
|
+
</span>
|
|
83
|
+
<span className="poad-stepper__txt">
|
|
84
|
+
<span className="poad-stepper__label">{s.label}{s.optional && <small> {t('nav.optional')}</small>}</span>
|
|
85
|
+
{s.description && <span className="poad-stepper__desc">{s.description}</span>}
|
|
86
|
+
</span>
|
|
87
|
+
</button>
|
|
88
|
+
{i < steps.length - 1 && <span className="poad-stepper__line" aria-hidden="true" />}
|
|
89
|
+
{vertical && i === active && <div className="poad-stepper__panel">{s.children}</div>}
|
|
90
|
+
</li>
|
|
91
|
+
);
|
|
92
|
+
})}
|
|
93
|
+
</ol>
|
|
94
|
+
{!vertical && steps[active] && <div className="poad-stepper__panel">{steps[active]!.children}</div>}
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|