@poadio/ui-elements 0.1.0 → 0.3.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 +42 -0
- package/README.md +47 -1
- package/dist/accordion.d.ts +31 -0
- package/dist/accordion.d.ts.map +1 -0
- package/dist/accordion.js +67 -0
- package/dist/accordion.js.map +1 -0
- package/dist/checkbox.d.ts +15 -1
- package/dist/checkbox.d.ts.map +1 -1
- package/dist/checkbox.js +18 -0
- package/dist/checkbox.js.map +1 -1
- package/dist/combo.d.ts +88 -0
- package/dist/combo.d.ts.map +1 -0
- package/dist/combo.js +238 -0
- package/dist/combo.js.map +1 -0
- package/dist/confirm.d.ts +35 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +94 -0
- package/dist/confirm.js.map +1 -0
- package/dist/datepicker.d.ts +146 -0
- package/dist/datepicker.d.ts.map +1 -0
- package/dist/datepicker.js +468 -0
- package/dist/datepicker.js.map +1 -0
- package/dist/declaration.d.ts +25 -0
- package/dist/declaration.d.ts.map +1 -0
- package/dist/declaration.js +35 -0
- package/dist/declaration.js.map +1 -0
- package/dist/dialog.d.ts +137 -0
- package/dist/dialog.d.ts.map +1 -0
- package/dist/dialog.js +174 -0
- package/dist/dialog.js.map +1 -0
- package/dist/element.d.ts +4 -0
- package/dist/element.d.ts.map +1 -1
- package/dist/element.js +5 -2
- package/dist/element.js.map +1 -1
- package/dist/index.d.ts +40 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -1
- package/dist/index.js.map +1 -1
- package/dist/menu.d.ts +121 -0
- package/dist/menu.d.ts.map +1 -0
- package/dist/menu.js +245 -0
- package/dist/menu.js.map +1 -0
- package/dist/meter.d.ts +1 -0
- package/dist/meter.d.ts.map +1 -1
- package/dist/meter.js +1 -2
- package/dist/meter.js.map +1 -1
- package/dist/multiselect.d.ts +133 -0
- package/dist/multiselect.d.ts.map +1 -0
- package/dist/multiselect.js +230 -0
- package/dist/multiselect.js.map +1 -0
- package/dist/option-list.d.ts +28 -0
- package/dist/option-list.d.ts.map +1 -0
- package/dist/option-list.js +31 -0
- package/dist/option-list.js.map +1 -0
- package/dist/paginator.d.ts +65 -0
- package/dist/paginator.d.ts.map +1 -0
- package/dist/paginator.js +110 -0
- package/dist/paginator.js.map +1 -0
- package/dist/segmented.d.ts +1 -0
- package/dist/segmented.d.ts.map +1 -1
- package/dist/segmented.js +1 -2
- package/dist/segmented.js.map +1 -1
- package/dist/select.d.ts +3 -6
- package/dist/select.d.ts.map +1 -1
- package/dist/select.js +8 -24
- package/dist/select.js.map +1 -1
- package/dist/services.d.ts +3 -0
- package/dist/services.d.ts.map +1 -0
- package/dist/services.js +10 -0
- package/dist/services.js.map +1 -0
- package/dist/slider.d.ts +1 -0
- package/dist/slider.d.ts.map +1 -1
- package/dist/slider.js +1 -2
- package/dist/slider.js.map +1 -1
- package/dist/split-button.d.ts +47 -0
- package/dist/split-button.d.ts.map +1 -0
- package/dist/split-button.js +64 -0
- package/dist/split-button.js.map +1 -0
- package/dist/stepper.d.ts +56 -0
- package/dist/stepper.d.ts.map +1 -0
- package/dist/stepper.js +69 -0
- package/dist/stepper.js.map +1 -0
- package/dist/table.d.ts +176 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +293 -0
- package/dist/table.js.map +1 -0
- package/dist/tabs.d.ts +62 -0
- package/dist/tabs.d.ts.map +1 -0
- package/dist/tabs.js +118 -0
- package/dist/tabs.js.map +1 -0
- package/dist/toast.d.ts +79 -0
- package/dist/toast.d.ts.map +1 -0
- package/dist/toast.js +176 -0
- package/dist/toast.js.map +1 -0
- package/package.json +3 -3
- package/src/accordion.ts +80 -0
- package/src/checkbox.ts +21 -1
- package/src/combo.ts +220 -0
- package/src/confirm.ts +106 -0
- package/src/datepicker.ts +413 -0
- package/src/declaration.ts +37 -0
- package/src/dialog.ts +217 -0
- package/src/element.ts +7 -2
- package/src/index.ts +33 -2
- package/src/menu.ts +281 -0
- package/src/meter.ts +2 -1
- package/src/multiselect.ts +273 -0
- package/src/option-list.ts +51 -0
- package/src/paginator.ts +151 -0
- package/src/segmented.ts +2 -1
- package/src/select.ts +14 -30
- package/src/services.ts +10 -0
- package/src/slider.ts +2 -1
- package/src/split-button.ts +76 -0
- package/src/stepper.ts +86 -0
- package/src/table.ts +364 -0
- package/src/tabs.ts +129 -0
- package/src/toast.ts +203 -0
package/src/combo.ts
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <poad-combo name="city" placeholder="City…" min-length="2" force-selection suggestions='[{"label":"Lyon"},{"label":"Lens"}]'></poad-combo>
|
|
3
|
+
* combo.suggestions = async (query) => (await fetch(`/cities?q=${query}`)).json(); une source qui cherche
|
|
4
|
+
* <poad-combo multiple dropdown suggestions='["Concrete","Steel","Wood"]'></poad-combo>
|
|
5
|
+
*
|
|
6
|
+
* Saisie avec suggestions (« autocomplete ») : machine `combo` de @poadio/ui-core (filtre sans accents ni casse, clavier
|
|
7
|
+
* ↓ ↑ Entrée Échap Tab et Retour arrière en choix multiple, choix, sortie du champ — texte libre ou `force-selection` —,
|
|
8
|
+
* attributs `combobox` / `listbox`). `suggestions` : un tableau (filtré ici) ou, en propriété, une fonction de la requête
|
|
9
|
+
* qui rend un tableau ou une promesse (la dernière réponse gagne). Propriété `value` : texte libre (chaîne), suggestion
|
|
10
|
+
* (objet) ou, en `multiple`, tableau (puces). Évènements : `change` (et `input`) quand l'utilisateur choisit, efface ou
|
|
11
|
+
* quitte le champ ; en texte libre, `input` seul à chaque frappe (la valeur suit) ; `search` (detail : la requête),
|
|
12
|
+
* `openchange`. Formulaire : `name` (le libellé ; une entrée par puce en `multiple`), `required`. Même DOM, mêmes
|
|
13
|
+
* classes, même ARIA que `poad-combo` d'ui-angular. Styles : `combo.css`, `icon.css`, `floating.css`.
|
|
14
|
+
*/
|
|
15
|
+
import {
|
|
16
|
+
SELECT_PLACEMENTS, comboBlur, comboFilter, comboInput, comboInputProps, comboIsSelected, comboKeydown, comboLabel, comboOptionProps, comboPick, comboResults,
|
|
17
|
+
comboRootClasses, comboTextOf, comboValues,
|
|
18
|
+
} from '@poadio/ui-core';
|
|
19
|
+
import { classOf, h } from './dom.js';
|
|
20
|
+
import { PoadFormElement } from './element.js';
|
|
21
|
+
import { FloatingTracker, Listening, listenOutside } from './floating.js';
|
|
22
|
+
import { icon } from './icon.js';
|
|
23
|
+
|
|
24
|
+
export type PoadComboSource = readonly unknown[] | ((query: string) => readonly unknown[] | Promise<readonly unknown[]>);
|
|
25
|
+
|
|
26
|
+
export class PoadComboElement extends PoadFormElement {
|
|
27
|
+
static override props = {
|
|
28
|
+
value: 'json', suggestions: 'json', optionLabel: 'string', minLength: 'number', delay: 'number', dropdown: 'boolean', forceSelection: 'boolean',
|
|
29
|
+
multiple: 'boolean', clearable: 'boolean', readonly: 'boolean', disabled: 'boolean', invalid: 'boolean', placeholder: 'string', emptyMessage: 'string',
|
|
30
|
+
size: 'string', panelMaxHeight: 'number', ariaLabel: 'string', name: 'string', required: 'boolean',
|
|
31
|
+
} as const;
|
|
32
|
+
static override functions = ['renderOption'] as const;
|
|
33
|
+
declare value?: unknown;
|
|
34
|
+
declare suggestions?: PoadComboSource;
|
|
35
|
+
/** Chemin du libellé d'une suggestion objet (défaut `label`). */
|
|
36
|
+
declare optionLabel?: string;
|
|
37
|
+
declare minLength?: number;
|
|
38
|
+
/** Délai avant la recherche (ms, défaut 250). */
|
|
39
|
+
declare delay?: number;
|
|
40
|
+
declare dropdown?: boolean;
|
|
41
|
+
declare forceSelection?: boolean;
|
|
42
|
+
declare multiple?: boolean;
|
|
43
|
+
declare clearable?: boolean;
|
|
44
|
+
declare readonly?: boolean;
|
|
45
|
+
declare disabled?: boolean;
|
|
46
|
+
declare invalid?: boolean;
|
|
47
|
+
declare placeholder?: string;
|
|
48
|
+
declare emptyMessage?: string;
|
|
49
|
+
declare size?: 'sm' | 'md' | 'lg';
|
|
50
|
+
declare panelMaxHeight?: number;
|
|
51
|
+
declare ariaLabel: string | null;
|
|
52
|
+
declare name?: string;
|
|
53
|
+
declare required?: boolean;
|
|
54
|
+
/** Rendu d'une suggestion. */
|
|
55
|
+
declare renderOption?: (option: unknown, context: { readonly selected: boolean; readonly index: number; readonly label: string }) => string | Node;
|
|
56
|
+
protected readonly resetProps = ['value'];
|
|
57
|
+
|
|
58
|
+
private text = '';
|
|
59
|
+
/** Valeur dont le texte du champ est tiré (choix unique) : une autre valeur posée par la page récrit le texte. */
|
|
60
|
+
private shown: unknown = undefined;
|
|
61
|
+
private open = false;
|
|
62
|
+
private loading = false;
|
|
63
|
+
private items: readonly unknown[] = [];
|
|
64
|
+
private active = -1;
|
|
65
|
+
private width = 0;
|
|
66
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
67
|
+
private seq = 0;
|
|
68
|
+
private readonly tracker = new FloatingTracker(() => this.update());
|
|
69
|
+
private readonly outside = new Listening(() => listenOutside(() => [this], () => this.close()));
|
|
70
|
+
|
|
71
|
+
private get path() { return this.optionLabel ?? 'label'; }
|
|
72
|
+
private get values() { return comboValues(this.value, !!this.multiple); }
|
|
73
|
+
private input(): HTMLInputElement | null { return this.querySelector<HTMLInputElement>(':scope > .poad-combo input.st__input'); }
|
|
74
|
+
|
|
75
|
+
protected formValue() {
|
|
76
|
+
if (this.multiple) {
|
|
77
|
+
const values = this.values;
|
|
78
|
+
if (!this.name || !values.length) return { value: null, missing: !values.length };
|
|
79
|
+
const data = new FormData();
|
|
80
|
+
for (const v of values) data.append(this.name, comboLabel(v, this.path));
|
|
81
|
+
return { value: data, missing: false };
|
|
82
|
+
}
|
|
83
|
+
const text = comboTextOf(this.value, this.path);
|
|
84
|
+
return { value: text || null, missing: !text };
|
|
85
|
+
}
|
|
86
|
+
override disconnectedCallback(): void {
|
|
87
|
+
super.disconnectedCallback();
|
|
88
|
+
if (this.timer) clearTimeout(this.timer);
|
|
89
|
+
this.open = false;
|
|
90
|
+
this.tracker.sync(null, null);
|
|
91
|
+
this.outside.toggle(false);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private write(next: unknown, commit = true) {
|
|
95
|
+
if (!this.multiple) this.shown = next;
|
|
96
|
+
this.value = next;
|
|
97
|
+
if (commit) this.changed(next); else this.emit('input', next);
|
|
98
|
+
}
|
|
99
|
+
private setOpen(next: boolean) {
|
|
100
|
+
if (next === this.open) return;
|
|
101
|
+
this.open = next;
|
|
102
|
+
if (next) this.width = this.querySelector(':scope > .poad-combo > .st')?.getBoundingClientRect().width ?? 0;
|
|
103
|
+
this.update();
|
|
104
|
+
this.emit('openchange', next);
|
|
105
|
+
}
|
|
106
|
+
private close() {
|
|
107
|
+
if (!this.open) return;
|
|
108
|
+
this.active = -1;
|
|
109
|
+
this.setOpen(false);
|
|
110
|
+
}
|
|
111
|
+
private async search(q: string) {
|
|
112
|
+
this.emit('search', q);
|
|
113
|
+
const my = ++this.seq;
|
|
114
|
+
const source = this.suggestions ?? [];
|
|
115
|
+
let res: readonly unknown[] = [];
|
|
116
|
+
if (typeof source === 'function') {
|
|
117
|
+
this.loading = true;
|
|
118
|
+
this.update();
|
|
119
|
+
try { res = await source(q); } catch { res = []; } finally { if (my === this.seq) { this.loading = false; this.update(); } }
|
|
120
|
+
} else res = comboFilter(source, q, this.path);
|
|
121
|
+
if (my !== this.seq) return;
|
|
122
|
+
this.items = res;
|
|
123
|
+
this.active = comboResults(res.length).active;
|
|
124
|
+
if (!this.open) this.setOpen(true); else this.update();
|
|
125
|
+
}
|
|
126
|
+
private onInput(v: string) {
|
|
127
|
+
this.text = v;
|
|
128
|
+
// texte libre : la valeur suit la frappe (évènement `input` seul, comme un champ natif)
|
|
129
|
+
if (!this.multiple && !this.forceSelection) this.write(v || null, false);
|
|
130
|
+
if (comboInput(v, this.minLength ?? 1) === 'close') { this.close(); this.update(); return; }
|
|
131
|
+
if (this.timer) clearTimeout(this.timer);
|
|
132
|
+
this.timer = setTimeout(() => void this.search(v), this.delay ?? 250);
|
|
133
|
+
}
|
|
134
|
+
private pick(o: unknown) {
|
|
135
|
+
const r = comboPick(this.value, o, { multiple: this.multiple, labelPath: this.path });
|
|
136
|
+
this.text = r.text;
|
|
137
|
+
if (r.changed) this.write(r.value);
|
|
138
|
+
this.close();
|
|
139
|
+
this.update();
|
|
140
|
+
}
|
|
141
|
+
private onBlur() {
|
|
142
|
+
const r = comboBlur(this.value, this.text, { forceSelection: this.forceSelection, multiple: this.multiple, labelPath: this.path });
|
|
143
|
+
if (!r) return;
|
|
144
|
+
if (r.text !== undefined) this.text = r.text;
|
|
145
|
+
if ('value' in r) this.write(r.value);
|
|
146
|
+
this.update();
|
|
147
|
+
}
|
|
148
|
+
private readonly onKeydown = (e: KeyboardEvent) => {
|
|
149
|
+
const values = this.values;
|
|
150
|
+
const r = comboKeydown({ open: this.open, active: this.active, count: this.items.length, multiple: !!this.multiple, text: this.text, valuesCount: values.length }, e.key);
|
|
151
|
+
if (r.preventDefault) e.preventDefault();
|
|
152
|
+
const a = r.action;
|
|
153
|
+
if (a.type === 'search') void this.search(this.text);
|
|
154
|
+
else if (a.type === 'move') { this.active = a.active; this.update(); }
|
|
155
|
+
else if (a.type === 'pick') this.pick(this.items[a.index]);
|
|
156
|
+
else if (a.type === 'close') this.close();
|
|
157
|
+
else if (a.type === 'remove-last') this.write(values.slice(0, -1));
|
|
158
|
+
};
|
|
159
|
+
protected override updated(): void {
|
|
160
|
+
super.updated();
|
|
161
|
+
const pane = this.open ? this.querySelector<HTMLElement>(':scope > .poad-combo > .poad-combo-pane') : null;
|
|
162
|
+
this.tracker.sync(pane ? this.querySelector<HTMLElement>(':scope > .poad-combo > .st') : null, pane, { candidates: SELECT_PLACEMENTS, offset: 4, shift: true });
|
|
163
|
+
this.outside.toggle(this.open);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
protected render() {
|
|
167
|
+
const { t, text: tx } = this.i18n();
|
|
168
|
+
const registry = this.icons();
|
|
169
|
+
const multiple = !!this.multiple;
|
|
170
|
+
// valeur posée par la page (choix unique) → texte du champ
|
|
171
|
+
if (!multiple && this.value !== this.shown) { this.shown = this.value; this.text = comboTextOf(this.value, this.path); }
|
|
172
|
+
const values = this.values;
|
|
173
|
+
const listId = `${this.uid}-list`;
|
|
174
|
+
const ia = comboInputProps({ open: this.open, listId, active: this.active, invalid: this.invalid });
|
|
175
|
+
const disabled = this.inactive;
|
|
176
|
+
const input = h('input', {
|
|
177
|
+
class: 'st__input', role: ia.role, placeholder: multiple && values.length ? '' : (this.placeholder ?? ''), value: this.text, disabled,
|
|
178
|
+
readonly: !multiple && this.readonly, autocomplete: 'off', spellcheck: 'false', 'aria-autocomplete': ia['aria-autocomplete'], 'aria-expanded': ia['aria-expanded'],
|
|
179
|
+
'aria-controls': ia['aria-controls'], 'aria-activedescendant': ia['aria-activedescendant'], 'aria-invalid': multiple ? undefined : ia['aria-invalid'],
|
|
180
|
+
'aria-label': this.ariaLabel || undefined, oninput: (e: Event) => this.onInput((e.target as HTMLInputElement).value), onkeydown: this.onKeydown,
|
|
181
|
+
onblur: () => this.onBlur(),
|
|
182
|
+
});
|
|
183
|
+
const clear = (e: MouseEvent) => { e.stopPropagation(); this.text = ''; this.write(multiple ? [] : null); this.close(); this.update(); };
|
|
184
|
+
const removeAt = (i: number, e: MouseEvent) => { e.stopPropagation(); if (disabled) return; this.write(values.filter((_, k) => k !== i)); };
|
|
185
|
+
return h('div', { class: ['poad-combo', comboRootClasses({ open: this.open, disabled, invalid: this.invalid, size: this.size ?? 'md' })] }, [
|
|
186
|
+
h('div', { class: ['st', { 'st--multi': multiple }], onclick: () => this.input()?.focus() }, [
|
|
187
|
+
multiple ? h('span', { class: 'st__chips' }, [
|
|
188
|
+
values.map((v, i) => h('span', { class: 'st__chip' }, [
|
|
189
|
+
comboLabel(v, this.path),
|
|
190
|
+
h('button', { type: 'button', tabindex: -1, 'aria-label': t('common.remove'), onclick: (e: MouseEvent) => removeAt(i, e) }, icon(registry, 'x', { size: 10 })),
|
|
191
|
+
])),
|
|
192
|
+
input,
|
|
193
|
+
]) : input,
|
|
194
|
+
this.loading && icon(registry, 'loader', { size: 14, spin: true, class: 'st__spin' }),
|
|
195
|
+
this.clearable && (this.text || values.length > 0) && !disabled && h('button', {
|
|
196
|
+
type: 'button', class: 'st__clear', tabindex: -1, 'aria-label': t('common.clear'), onclick: clear,
|
|
197
|
+
}, icon(registry, 'x', { size: 12 })),
|
|
198
|
+
this.dropdown && h('button', {
|
|
199
|
+
type: 'button', class: 'st__dd', tabindex: -1, 'aria-label': t('select.showSuggestions'),
|
|
200
|
+
onclick: (e: MouseEvent) => {
|
|
201
|
+
e.stopPropagation();
|
|
202
|
+
if (disabled) return;
|
|
203
|
+
if (this.open) { this.close(); return; }
|
|
204
|
+
this.input()?.focus();
|
|
205
|
+
void this.search('');
|
|
206
|
+
},
|
|
207
|
+
}, icon(registry, 'chevron-down', { size: 13, class: 'st__chev' })),
|
|
208
|
+
]),
|
|
209
|
+
this.open && h('div', { class: 'poad-floating poad-combo-pane', style: this.tracker.style }, h('div', { class: 'sp', style: { minWidth: `${this.width}px` } },
|
|
210
|
+
h('ul', { class: 'cb__list', role: 'listbox', id: listId, style: { maxHeight: `${this.panelMaxHeight ?? 260}px` } },
|
|
211
|
+
this.items.length ? this.items.map((o, i) => {
|
|
212
|
+
const op = comboOptionProps(listId, i, i === this.active, comboIsSelected(this.value, o, multiple));
|
|
213
|
+
return h('li', {
|
|
214
|
+
role: op.role, id: op.id, class: classOf(op.classes) || undefined, 'aria-selected': op['aria-selected'],
|
|
215
|
+
onmousedown: (e: MouseEvent) => e.preventDefault(), onmouseenter: () => { this.active = i; this.update(); }, onclick: () => this.pick(o),
|
|
216
|
+
}, this.renderOption ? this.renderOption(o, { selected: op.classes['is-on'], index: i, label: comboLabel(o, this.path) }) : comboLabel(o, this.path));
|
|
217
|
+
}) : !this.loading && h('li', { class: 'sp__empty' }, tx(this.emptyMessage, 'select.noSuggestions'))))),
|
|
218
|
+
]);
|
|
219
|
+
}
|
|
220
|
+
}
|
package/src/confirm.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* const ok = await poadConfirm({ header: 'Delete?', message: 'This cannot be undone.', severity: 'danger', requireTyping: 'prod' });
|
|
3
|
+
*
|
|
4
|
+
* Confirmation : le pendant de `PoadConfirmService` d'ui-angular, en une fonction. `poadConfirm(options)` rend une
|
|
5
|
+
* promesse de `true` (accepté) ou `false` (refusé, croix, Échap, fond) et appelle `accept` / `reject` s'ils sont
|
|
6
|
+
* donnés. Elle se sert d'un `<poad-confirm-dialog>` de la page : le premier connecté, sinon un élément créé à la
|
|
7
|
+
* première demande en fin de `body` et gardé (préfixe de balise : celui du premier `defineElements()`). Une seule
|
|
8
|
+
* confirmation à la fois : une nouvelle demande refuse la précédente. Décisions : `confirm*` d'ui-core (configuration
|
|
9
|
+
* de la boîte — même conteneur que `<poad-dialog>` —, sévérité du bouton, icône, saisie exigée). `message` : un texte ou
|
|
10
|
+
* un nœud du DOM. Styles : `dialog.css`, `button.css`, `field.css`, `icon.css`.
|
|
11
|
+
*/
|
|
12
|
+
import { confirmAcceptSeverity, confirmCanAccept, confirmClasses, confirmDialogConfig, confirmIcon, inputClasses, type ConfirmOptions } from '@poadio/ui-core';
|
|
13
|
+
import { button } from './button.js';
|
|
14
|
+
import { DialogLayer } from './dialog.js';
|
|
15
|
+
import { h } from './dom.js';
|
|
16
|
+
import { PoadElement } from './element.js';
|
|
17
|
+
import { icon } from './icon.js';
|
|
18
|
+
import { servicePrefix } from './services.js';
|
|
19
|
+
|
|
20
|
+
export interface PoadConfirmOptions extends ConfirmOptions {
|
|
21
|
+
/** Texte, ou nœud du DOM. */
|
|
22
|
+
readonly message?: string | Node;
|
|
23
|
+
readonly accept?: () => void;
|
|
24
|
+
readonly reject?: () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface Pending {
|
|
28
|
+
readonly options: PoadConfirmOptions;
|
|
29
|
+
readonly resolve: (ok: boolean) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class PoadConfirmDialogElement extends PoadElement {
|
|
33
|
+
private pending: Pending | null = null;
|
|
34
|
+
private current: Pending | null = null;
|
|
35
|
+
private open = false;
|
|
36
|
+
private typed = '';
|
|
37
|
+
private readonly layer = new DialogLayer(this);
|
|
38
|
+
|
|
39
|
+
/** Ouvre la confirmation ; rend la réponse de l'utilisateur. */
|
|
40
|
+
confirm(options: PoadConfirmOptions): Promise<boolean> {
|
|
41
|
+
return new Promise<boolean>((resolve) => {
|
|
42
|
+
this.current?.resolve(false); // une seule à la fois : la précédente est refusée
|
|
43
|
+
this.current = this.pending = { options, resolve };
|
|
44
|
+
this.typed = '';
|
|
45
|
+
this.open = true;
|
|
46
|
+
this.update();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
private settle(ok: boolean) {
|
|
50
|
+
const p = this.current;
|
|
51
|
+
if (!p) return;
|
|
52
|
+
this.current = null;
|
|
53
|
+
this.open = false;
|
|
54
|
+
this.update();
|
|
55
|
+
(ok ? p.options.accept : p.options.reject)?.();
|
|
56
|
+
p.resolve(ok);
|
|
57
|
+
}
|
|
58
|
+
override disconnectedCallback(): void {
|
|
59
|
+
super.disconnectedCallback();
|
|
60
|
+
this.layer.dispose();
|
|
61
|
+
this.settle(false);
|
|
62
|
+
}
|
|
63
|
+
protected render() {
|
|
64
|
+
const o = this.pending?.options;
|
|
65
|
+
if (!o) return null;
|
|
66
|
+
const { t, text } = this.i18n();
|
|
67
|
+
const registry = this.icons();
|
|
68
|
+
const name = confirmIcon(o);
|
|
69
|
+
const accept = { severity: confirmAcceptSeverity(o), disabled: !confirmCanAccept(o, this.typed) } as const;
|
|
70
|
+
return this.layer.render({
|
|
71
|
+
config: confirmDialogConfig(o, t('overlay.confirmHeader')), open: this.open, titleId: `${this.uid}-title`, t, icons: registry,
|
|
72
|
+
onClose: () => this.settle(false),
|
|
73
|
+
body: h('div', { class: ['poad-confirm', confirmClasses(o)] }, [
|
|
74
|
+
h('div', { class: 'poad-confirm__row' }, [
|
|
75
|
+
name && icon(registry, name, { size: 20, class: 'poad-confirm__icon' }),
|
|
76
|
+
h('div', { class: 'poad-confirm__msg' }, h('div', {}, o.message ?? '')),
|
|
77
|
+
]),
|
|
78
|
+
o.requireTyping && h('label', { class: 'poad-confirm__typing' }, [
|
|
79
|
+
h('span', {}, text(o.requireTypingLabel || undefined, 'overlay.typeToConfirm', { text: o.requireTyping })),
|
|
80
|
+
h('input', {
|
|
81
|
+
class: ['poad-input', inputClasses({ mono: true })], value: this.typed, placeholder: o.requireTyping, autocomplete: 'off', spellcheck: 'false',
|
|
82
|
+
oninput: (e: Event) => { this.typed = (e.target as HTMLInputElement).value; this.update(); },
|
|
83
|
+
}),
|
|
84
|
+
]),
|
|
85
|
+
]),
|
|
86
|
+
footer: [
|
|
87
|
+
o.rejectVisible !== false && button({ severity: 'secondary' }, { type: 'button', onclick: () => this.settle(false) }, o.rejectLabel || t('common.cancel')),
|
|
88
|
+
button(accept, { type: 'button', onclick: () => this.settle(true) }, [o.acceptIcon && icon(registry, o.acceptIcon, { size: 14 }), o.acceptLabel || t('common.confirm')]),
|
|
89
|
+
],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
protected override updated(): void {
|
|
93
|
+
this.layer.sync();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Demande une confirmation : `true` si l'utilisateur accepte. Navigateur seulement. */
|
|
98
|
+
export function poadConfirm(options: PoadConfirmOptions): Promise<boolean> {
|
|
99
|
+
const tag = `${servicePrefix()}-confirm-dialog`;
|
|
100
|
+
let el = document.querySelector(tag) as PoadConfirmDialogElement | null;
|
|
101
|
+
if (!el) {
|
|
102
|
+
el = document.createElement(tag) as PoadConfirmDialogElement;
|
|
103
|
+
document.body.append(el);
|
|
104
|
+
}
|
|
105
|
+
return el.confirm(options);
|
|
106
|
+
}
|