@directo-ui/astro 1.1.0 → 1.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/README.md +2 -0
- package/components.css +475 -2
- package/index.d.ts +10 -0
- package/index.js +9 -0
- package/package.json +15 -7
- package/src/components/Autocomplete.astro +106 -0
- package/src/components/DataTable.astro +136 -0
- package/src/components/DateRangePicker.astro +105 -0
- package/src/components/Drawer.astro +40 -0
- package/src/components/FileUpload.astro +152 -0
- package/src/components/FilterBar.astro +66 -0
- package/src/components/Sidebar.astro +64 -0
- package/src/components/Timeline.astro +25 -0
- package/src/components/WariClientDetails.astro +87 -0
- package/src/components/WariClientSearch.astro +88 -0
- package/src/components/WariPassengerField.astro +84 -0
- package/src/components/WariRoute.astro +49 -0
- package/src/components/WariStepper.astro +9 -4
- package/src/components/WariSwitchList.astro +86 -8
- package/src/components/WariTripAttributes.astro +1 -1
- package/src/files.d.ts +27 -0
- package/src/files.js +52 -0
- package/src/modal.d.ts +3 -3
- package/src/modal.js +50 -39
- package/src/wari/motion.d.ts +14 -0
- package/src/wari/motion.js +60 -0
- package/src/wari/route-client.d.ts +10 -0
- package/src/wari/route-client.js +270 -0
- package/src/wari/route.d.ts +48 -0
- package/src/wari/route.js +71 -0
- package/src/wari/trip-client.js +152 -4
- package/src/wari/trip.d.ts +51 -0
- package/src/wari/trip.js +64 -4
- package/wari.d.ts +8 -2
- package/wari.js +6 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
options: readonly string[];
|
|
6
|
+
name?: string;
|
|
7
|
+
placeholder?: string;
|
|
8
|
+
value?: string;
|
|
9
|
+
supportingText?: string;
|
|
10
|
+
required?: boolean;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
/** Wait after the last key before emitting ds-search. */
|
|
13
|
+
debounceMs?: number;
|
|
14
|
+
/** Minimum characters for ds-search. */
|
|
15
|
+
minChars?: number;
|
|
16
|
+
loadingText?: string;
|
|
17
|
+
/** When false, leaving the field with a value that is not in the list shows invalidMessage. */
|
|
18
|
+
allowCustomValue?: boolean;
|
|
19
|
+
invalidMessage?: string;
|
|
20
|
+
class?: string;
|
|
21
|
+
}
|
|
22
|
+
const {
|
|
23
|
+
id, label, options, name, placeholder, value, supportingText, required = false, disabled = false, debounceMs = 250, minChars = 1,
|
|
24
|
+
loadingText = 'Buscando…', allowCustomValue = true, invalidMessage = 'Elige una opción de la lista.', class: className,
|
|
25
|
+
} = Astro.props;
|
|
26
|
+
---
|
|
27
|
+
<directo-autocomplete class:list={["ds-input", className]} data-size="md" data-error="false" data-disabled={String(disabled)}
|
|
28
|
+
data-debounce={debounceMs} data-min-chars={minChars} data-strict={allowCustomValue ? undefined : ''}
|
|
29
|
+
data-invalid-message={invalidMessage} data-loading-text={loadingText} data-hint={supportingText}>
|
|
30
|
+
<label for={id}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>
|
|
31
|
+
<div class="ds-input-shell">
|
|
32
|
+
<input id={id} type="text" name={name} list={`${id}-options`} autocomplete="off" placeholder={placeholder} value={value}
|
|
33
|
+
required={required} disabled={disabled} aria-describedby={`${id}-hint`} />
|
|
34
|
+
</div>
|
|
35
|
+
<small id={`${id}-hint`} hidden={!supportingText}>{supportingText}</small>
|
|
36
|
+
<datalist id={`${id}-options`}>{[...new Set(options)].map(option => <option value={option}></option>)}</datalist>
|
|
37
|
+
</directo-autocomplete>
|
|
38
|
+
|
|
39
|
+
<script>
|
|
40
|
+
class DirectoAutocompleteElement extends HTMLElement {
|
|
41
|
+
private timer?: ReturnType<typeof setTimeout>;
|
|
42
|
+
private invalid = false;
|
|
43
|
+
private busy = false;
|
|
44
|
+
|
|
45
|
+
connectedCallback() {
|
|
46
|
+
this.addEventListener('input', this.change);
|
|
47
|
+
this.addEventListener('focusout', this.leave);
|
|
48
|
+
}
|
|
49
|
+
disconnectedCallback() {
|
|
50
|
+
this.removeEventListener('input', this.change);
|
|
51
|
+
this.removeEventListener('focusout', this.leave);
|
|
52
|
+
clearTimeout(this.timer);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private get field() { return this.querySelector<HTMLInputElement>('input'); }
|
|
56
|
+
private get options() { return Array.from(this.querySelectorAll('datalist option'), option => (option as HTMLOptionElement).value); }
|
|
57
|
+
|
|
58
|
+
/** Replaces the suggestions (for example with the results of your API after ds-search) and ends the loading state. */
|
|
59
|
+
dsSetOptions(options: readonly string[]) {
|
|
60
|
+
const list = this.querySelector('datalist');
|
|
61
|
+
list?.replaceChildren(...[...new Set(options)].map(value => Object.assign(document.createElement('option'), { value })));
|
|
62
|
+
this.dsSetLoading(false);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Shows the loading text under the field. */
|
|
66
|
+
dsSetLoading(loading: boolean) {
|
|
67
|
+
this.busy = loading;
|
|
68
|
+
this.render();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private change = (event: Event) => {
|
|
72
|
+
const field = this.field;
|
|
73
|
+
if (!field || event.target !== field) return;
|
|
74
|
+
this.invalid = false;
|
|
75
|
+
this.render();
|
|
76
|
+
const value = field.value;
|
|
77
|
+
this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: value }));
|
|
78
|
+
if (this.options.includes(value)) this.dispatchEvent(new CustomEvent('ds-select', { bubbles: true, detail: value }));
|
|
79
|
+
clearTimeout(this.timer);
|
|
80
|
+
const query = value.trim();
|
|
81
|
+
if (query.length < Number(this.dataset.minChars ?? 1)) return;
|
|
82
|
+
this.timer = setTimeout(() => this.dispatchEvent(new CustomEvent('ds-search', { bubbles: true, detail: query })),
|
|
83
|
+
Math.max(0, Number(this.dataset.debounce ?? 250)));
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
private leave = () => {
|
|
87
|
+
const field = this.field;
|
|
88
|
+
if (!field || !this.hasAttribute('data-strict')) return;
|
|
89
|
+
this.invalid = !!field.value && !this.options.includes(field.value);
|
|
90
|
+
this.render();
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
private render() {
|
|
94
|
+
const field = this.field;
|
|
95
|
+
const hint = this.querySelector('small');
|
|
96
|
+
if (!field || !hint) return;
|
|
97
|
+
const text = this.invalid ? this.dataset.invalidMessage : this.busy ? this.dataset.loadingText : this.dataset.hint;
|
|
98
|
+
hint.textContent = text ?? '';
|
|
99
|
+
hint.hidden = !text;
|
|
100
|
+
this.dataset.error = String(this.invalid);
|
|
101
|
+
if (this.invalid) field.setAttribute('aria-invalid', 'true'); else field.removeAttribute('aria-invalid');
|
|
102
|
+
if (this.busy) field.setAttribute('aria-busy', 'true'); else field.removeAttribute('aria-busy');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!customElements.get('directo-autocomplete')) customElements.define('directo-autocomplete', DirectoAutocompleteElement);
|
|
106
|
+
</script>
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
---
|
|
2
|
+
type Row = Record<string, string | number | undefined>;
|
|
3
|
+
type Direction = 'ascending' | 'descending';
|
|
4
|
+
|
|
5
|
+
interface Props {
|
|
6
|
+
caption: string;
|
|
7
|
+
columns: readonly { key: string; label: string; sortable?: boolean; align?: 'start' | 'center' | 'end' }[];
|
|
8
|
+
rows: readonly Row[];
|
|
9
|
+
loading?: boolean;
|
|
10
|
+
loadingMessage?: string;
|
|
11
|
+
emptyMessage?: string;
|
|
12
|
+
actionLabel?: string;
|
|
13
|
+
actionHref?: (row: Row) => string;
|
|
14
|
+
/** Column key whose value identifies each row (sent in ds-row-click). Defaults to "id". */
|
|
15
|
+
rowKey?: string;
|
|
16
|
+
/** Sorted on the server before rendering and shown in the header. */
|
|
17
|
+
defaultSort?: { key: string; direction: Direction };
|
|
18
|
+
/** Do not reorder rows in the browser: listen to ds-sort and sort on the server. */
|
|
19
|
+
manualSort?: boolean;
|
|
20
|
+
/** Rows respond to click, Enter and Space and emit ds-row-click. */
|
|
21
|
+
clickableRows?: boolean;
|
|
22
|
+
density?: 'comfortable' | 'compact';
|
|
23
|
+
stickyHeader?: boolean;
|
|
24
|
+
id?: string;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
caption, columns, rows, loading = false, loadingMessage = 'Cargando registros…', emptyMessage = 'No hay resultados.',
|
|
30
|
+
actionLabel = 'Ver detalle', actionHref, rowKey = 'id', defaultSort, manualSort = false, clickableRows = false,
|
|
31
|
+
density = 'comfortable', stickyHeader = false, id, class: className,
|
|
32
|
+
} = Astro.props;
|
|
33
|
+
const collator = new Intl.Collator('es', { numeric: true, sensitivity: 'base' });
|
|
34
|
+
const sortColumn = defaultSort && columns.find(column => column.key === defaultSort.key && column.sortable);
|
|
35
|
+
const shownRows = sortColumn && !manualSort ? [...rows].sort((left, right) => {
|
|
36
|
+
const a = left[sortColumn.key];
|
|
37
|
+
const b = right[sortColumn.key];
|
|
38
|
+
const order = typeof a === 'number' && typeof b === 'number' ? a - b : collator.compare(String(a ?? ''), String(b ?? ''));
|
|
39
|
+
return defaultSort!.direction === 'ascending' ? order : -order;
|
|
40
|
+
}) : rows;
|
|
41
|
+
const directionOf = (key: string) => sortColumn?.key === key ? defaultSort!.direction : undefined;
|
|
42
|
+
const arrow = (key: string) => directionOf(key) === 'ascending' ? '↑' : directionOf(key) === 'descending' ? '↓' : '↕';
|
|
43
|
+
---
|
|
44
|
+
<directo-sortable-table id={id} class:list={['ds-data-table', className]} data-density={density}
|
|
45
|
+
data-sticky-header={stickyHeader ? '' : undefined} data-manual-sort={manualSort ? '' : undefined}>
|
|
46
|
+
<div class="ds-data-scroll" role="region" aria-label={caption} tabindex="0">
|
|
47
|
+
<table aria-busy={loading}>
|
|
48
|
+
<caption>{caption}</caption>
|
|
49
|
+
<thead><tr>
|
|
50
|
+
{columns.map((column, index) => <th scope="col" data-align={column.align} aria-sort={column.sortable ? directionOf(column.key) : undefined}>
|
|
51
|
+
{column.sortable ? <button type="button" class="ds-sort-button" data-column={index} data-key={column.key}>{column.label} <span aria-hidden="true">{arrow(column.key)}</span></button> : column.label}
|
|
52
|
+
</th>)}
|
|
53
|
+
{actionHref && <th scope="col">Acciones</th>}
|
|
54
|
+
</tr></thead>
|
|
55
|
+
<tbody>
|
|
56
|
+
{loading || !rows.length
|
|
57
|
+
? <tr><td colspan={Math.max(1, columns.length + Number(!!actionHref))}><span role="status">{loading ? loadingMessage : emptyMessage}</span></td></tr>
|
|
58
|
+
: shownRows.map((row, index) => <tr data-row data-index={index} data-key={row[rowKey] === undefined ? undefined : String(row[rowKey])}
|
|
59
|
+
tabindex={clickableRows ? 0 : undefined} data-clickable={clickableRows ? '' : undefined}>
|
|
60
|
+
{columns.map(column => <td data-align={column.align} data-number={typeof row[column.key] === 'number' ? '' : undefined}>{row[column.key]}</td>)}
|
|
61
|
+
{actionHref && <td><a href={actionHref(row)}>{actionLabel}</a></td>}
|
|
62
|
+
</tr>)}
|
|
63
|
+
</tbody>
|
|
64
|
+
</table>
|
|
65
|
+
</div>
|
|
66
|
+
<slot name="pagination" />
|
|
67
|
+
</directo-sortable-table>
|
|
68
|
+
|
|
69
|
+
<script>
|
|
70
|
+
const collator = new Intl.Collator('es', { numeric: true, sensitivity: 'base' });
|
|
71
|
+
const interactive = 'a, button, input, select, textarea, label, [role="button"], [contenteditable="true"]';
|
|
72
|
+
|
|
73
|
+
class DirectoSortableTable extends HTMLElement {
|
|
74
|
+
connectedCallback() {
|
|
75
|
+
this.addEventListener('click', this.handleClick);
|
|
76
|
+
this.addEventListener('keydown', this.handleKey);
|
|
77
|
+
}
|
|
78
|
+
disconnectedCallback() {
|
|
79
|
+
this.removeEventListener('click', this.handleClick);
|
|
80
|
+
this.removeEventListener('keydown', this.handleKey);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private handleClick = (event: MouseEvent) => {
|
|
84
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
85
|
+
const trigger = target?.closest<HTMLButtonElement>('button[data-column]');
|
|
86
|
+
if (trigger) { this.sort(trigger); return; }
|
|
87
|
+
const row = target?.closest<HTMLTableRowElement>('tr[data-clickable]');
|
|
88
|
+
if (!row || !this.contains(row)) return;
|
|
89
|
+
const inner = target?.closest(interactive);
|
|
90
|
+
if (inner && row.contains(inner)) return;
|
|
91
|
+
this.emitRow(row);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
private handleKey = (event: KeyboardEvent) => {
|
|
95
|
+
const row = event.target instanceof HTMLTableRowElement && event.target.hasAttribute('data-clickable') ? event.target : null;
|
|
96
|
+
if (!row || (event.key !== 'Enter' && event.key !== ' ')) return;
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
this.emitRow(row);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
private emitRow(row: HTMLTableRowElement) {
|
|
102
|
+
this.dispatchEvent(new CustomEvent('ds-row-click', {
|
|
103
|
+
bubbles: true, detail: { key: row.dataset.key, index: Number(row.dataset.index), row },
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private sort(trigger: HTMLButtonElement) {
|
|
108
|
+
const body = this.querySelector('tbody');
|
|
109
|
+
const header = trigger.closest('th');
|
|
110
|
+
if (!body || !header) return;
|
|
111
|
+
const column = Number(trigger.dataset.column);
|
|
112
|
+
const direction = header.getAttribute('aria-sort') === 'ascending' ? 'descending' : 'ascending';
|
|
113
|
+
for (const th of this.querySelectorAll('th[aria-sort]')) {
|
|
114
|
+
th.removeAttribute('aria-sort');
|
|
115
|
+
const icon = th.querySelector('span');
|
|
116
|
+
if (icon) icon.textContent = '↕';
|
|
117
|
+
}
|
|
118
|
+
header.setAttribute('aria-sort', direction);
|
|
119
|
+
const icon = trigger.querySelector('span');
|
|
120
|
+
if (icon) icon.textContent = direction === 'ascending' ? '↑' : '↓';
|
|
121
|
+
this.dispatchEvent(new CustomEvent('ds-sort', { bubbles: true, detail: { key: trigger.dataset.key, column, direction } }));
|
|
122
|
+
if (this.hasAttribute('data-manual-sort')) return;
|
|
123
|
+
const rows = Array.from(body.querySelectorAll<HTMLTableRowElement>('tr[data-row]'));
|
|
124
|
+
rows.sort((left, right) => {
|
|
125
|
+
const a = left.cells[column];
|
|
126
|
+
const b = right.cells[column];
|
|
127
|
+
const x = a.textContent ?? '';
|
|
128
|
+
const y = b.textContent ?? '';
|
|
129
|
+
const order = a.hasAttribute('data-number') && b.hasAttribute('data-number') ? Number(x) - Number(y) : collator.compare(x, y);
|
|
130
|
+
return direction === 'ascending' ? order : -order;
|
|
131
|
+
});
|
|
132
|
+
body.append(...rows);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (!customElements.get('directo-sortable-table')) customElements.define('directo-sortable-table', DirectoSortableTable);
|
|
136
|
+
</script>
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
import TextField from './TextField.astro';
|
|
3
|
+
|
|
4
|
+
interface Props {
|
|
5
|
+
id: string;
|
|
6
|
+
label?: string;
|
|
7
|
+
startLabel?: string;
|
|
8
|
+
endLabel?: string;
|
|
9
|
+
start?: string;
|
|
10
|
+
end?: string;
|
|
11
|
+
startName?: string;
|
|
12
|
+
endName?: string;
|
|
13
|
+
min?: string;
|
|
14
|
+
max?: string;
|
|
15
|
+
required?: boolean;
|
|
16
|
+
disabled?: boolean;
|
|
17
|
+
/** Your own error message (server validation). The picker also warns when the end is before the start. */
|
|
18
|
+
error?: string;
|
|
19
|
+
/** Quick ranges shown as buttons. */
|
|
20
|
+
presets?: readonly { label: string; range: { start: string; end: string } }[];
|
|
21
|
+
class?: string;
|
|
22
|
+
}
|
|
23
|
+
const {
|
|
24
|
+
id, label = 'Rango de fechas', startLabel = 'Desde', endLabel = 'Hasta', start = '', end = '', startName, endName, min, max,
|
|
25
|
+
required = false, disabled = false, error, presets = [], class: className,
|
|
26
|
+
} = Astro.props;
|
|
27
|
+
const startMax = end && (!max || end < max) ? end : max;
|
|
28
|
+
const endMin = start && (!min || start > min) ? start : min;
|
|
29
|
+
const orderMessage = 'La fecha final no puede ser anterior a la inicial.';
|
|
30
|
+
const message = error ?? (start && end && end < start ? orderMessage : '');
|
|
31
|
+
---
|
|
32
|
+
<directo-date-range data-message={orderMessage}>
|
|
33
|
+
<fieldset id={id} class:list={['ds-date-range', className]} data-min={min} data-max={max} disabled={disabled}
|
|
34
|
+
data-error={error} aria-describedby={`${id}-error`}>
|
|
35
|
+
<legend>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</legend>
|
|
36
|
+
{presets.length > 0 && <div class="ds-date-range-presets">
|
|
37
|
+
{presets.map(preset => <button type="button" class="ds-filter-chip" data-start={preset.range.start} data-end={preset.range.end}
|
|
38
|
+
aria-pressed={String(preset.range.start === start && preset.range.end === end)}>{preset.label}</button>)}
|
|
39
|
+
</div>}
|
|
40
|
+
<div class="ds-date-range-fields">
|
|
41
|
+
<TextField id={`${id}-start`} label={startLabel} type="date" name={startName} value={start} min={min} max={startMax}
|
|
42
|
+
required={required} error={!!message} />
|
|
43
|
+
<TextField id={`${id}-end`} label={endLabel} type="date" name={endName} value={end} min={endMin} max={max}
|
|
44
|
+
required={required} error={!!message} />
|
|
45
|
+
</div>
|
|
46
|
+
<p id={`${id}-error`} class="ds-date-range-error" role="status">{message}</p>
|
|
47
|
+
</fieldset>
|
|
48
|
+
</directo-date-range>
|
|
49
|
+
|
|
50
|
+
<script>
|
|
51
|
+
class DirectoDateRange extends HTMLElement {
|
|
52
|
+
connectedCallback() {
|
|
53
|
+
this.addEventListener('input', this.sync);
|
|
54
|
+
this.addEventListener('click', this.preset);
|
|
55
|
+
}
|
|
56
|
+
disconnectedCallback() {
|
|
57
|
+
this.removeEventListener('input', this.sync);
|
|
58
|
+
this.removeEventListener('click', this.preset);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Current range (YYYY-MM-DD). */
|
|
62
|
+
get dsValue() {
|
|
63
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
64
|
+
return { start: start?.value ?? '', end: end?.value ?? '' };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Sets both dates and emits ds-change (and ds-complete when valid). */
|
|
68
|
+
dsSetValue(range: { start: string; end: string }) {
|
|
69
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
70
|
+
if (!start || !end) return;
|
|
71
|
+
start.value = range.start;
|
|
72
|
+
end.value = range.end;
|
|
73
|
+
this.sync();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private preset = (event: MouseEvent) => {
|
|
77
|
+
const button = event.target instanceof Element ? event.target.closest<HTMLButtonElement>('button[data-start]') : null;
|
|
78
|
+
if (button) this.dsSetValue({ start: button.dataset.start ?? '', end: button.dataset.end ?? '' });
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
private sync = () => {
|
|
82
|
+
const fields = this.querySelector('fieldset');
|
|
83
|
+
const [start, end] = this.querySelectorAll<HTMLInputElement>('input[type="date"]');
|
|
84
|
+
if (!fields || !start || !end) return;
|
|
85
|
+
const { min = '', max = '' } = fields.dataset;
|
|
86
|
+
start.max = end.value && (!max || end.value < max) ? end.value : max;
|
|
87
|
+
end.min = start.value && (!min || start.value > min) ? start.value : min;
|
|
88
|
+
const reversed = !!start.value && !!end.value && end.value < start.value;
|
|
89
|
+
const message = reversed ? this.dataset.message ?? '' : fields.dataset.error ?? '';
|
|
90
|
+
const status = this.querySelector('.ds-date-range-error');
|
|
91
|
+
if (status) status.textContent = message;
|
|
92
|
+
for (const input of [start, end]) {
|
|
93
|
+
input.closest('.ds-input')?.setAttribute('data-error', String(!!message));
|
|
94
|
+
if (message) input.setAttribute('aria-invalid', 'true'); else input.removeAttribute('aria-invalid');
|
|
95
|
+
}
|
|
96
|
+
for (const button of this.querySelectorAll<HTMLButtonElement>('button[data-start]')) {
|
|
97
|
+
button.setAttribute('aria-pressed', String(button.dataset.start === start.value && button.dataset.end === end.value));
|
|
98
|
+
}
|
|
99
|
+
const value = { start: start.value, end: end.value };
|
|
100
|
+
this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: value }));
|
|
101
|
+
if (value.start && value.end && !reversed) this.dispatchEvent(new CustomEvent('ds-complete', { bubbles: true, detail: value }));
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (!customElements.get('directo-date-range')) customElements.define('directo-date-range', DirectoDateRange);
|
|
105
|
+
</script>
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
description?: string;
|
|
6
|
+
side?: 'start' | 'end';
|
|
7
|
+
/** Width: sm 360px, md 440px, lg 640px. */
|
|
8
|
+
size?: 'sm' | 'md' | 'lg';
|
|
9
|
+
/** Opens when the page loads. */
|
|
10
|
+
open?: boolean;
|
|
11
|
+
closeOnBackdrop?: boolean;
|
|
12
|
+
closeOnEscape?: boolean;
|
|
13
|
+
closeLabel?: string;
|
|
14
|
+
class?: string;
|
|
15
|
+
}
|
|
16
|
+
const {
|
|
17
|
+
id, title, description, side = 'end', size = 'md', open = false, closeOnBackdrop = true, closeOnEscape = true,
|
|
18
|
+
closeLabel = 'Cerrar panel', class: className,
|
|
19
|
+
} = Astro.props;
|
|
20
|
+
---
|
|
21
|
+
<dialog id={id} class:list={['ds-drawer', className]} data-side={side} data-size={size} data-ds-modal
|
|
22
|
+
data-ds-open-initially={open ? '' : undefined} data-ds-backdrop={closeOnBackdrop ? undefined : 'static'}
|
|
23
|
+
data-ds-escape={closeOnEscape ? undefined : 'false'}
|
|
24
|
+
aria-labelledby={`${id}-title`} aria-describedby={description ? `${id}-description` : undefined}>
|
|
25
|
+
<header class="ds-drawer-header">
|
|
26
|
+
<div>
|
|
27
|
+
<h2 id={`${id}-title`}>{title}</h2>
|
|
28
|
+
{description && <p class="ds-drawer-description" id={`${id}-description`}>{description}</p>}
|
|
29
|
+
</div>
|
|
30
|
+
<button type="button" class="ds-sheet-close" aria-label={closeLabel} data-ds-close>×</button>
|
|
31
|
+
</header>
|
|
32
|
+
<div class="ds-drawer-body"><slot /></div>
|
|
33
|
+
{Astro.slots.has('footer') && <footer class="ds-drawer-footer"><slot name="footer" /></footer>}
|
|
34
|
+
</dialog>
|
|
35
|
+
|
|
36
|
+
<script>
|
|
37
|
+
import { initModals } from '../modal.js';
|
|
38
|
+
initModals();
|
|
39
|
+
document.addEventListener('astro:page-load', () => initModals());
|
|
40
|
+
</script>
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface Props {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
accept?: string;
|
|
7
|
+
multiple?: boolean;
|
|
8
|
+
/** Maximum size of each file, in bytes. */
|
|
9
|
+
maxBytes?: number;
|
|
10
|
+
/** Maximum number of files (with multiple). */
|
|
11
|
+
maxFiles?: number;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
supportingText?: string;
|
|
15
|
+
/** Allows dropping files onto the component. */
|
|
16
|
+
dropzone?: boolean;
|
|
17
|
+
class?: string;
|
|
18
|
+
}
|
|
19
|
+
const {
|
|
20
|
+
id, label, name, accept, multiple = false, maxBytes, maxFiles, required = false, disabled = false, supportingText,
|
|
21
|
+
dropzone = true, class: className,
|
|
22
|
+
} = Astro.props;
|
|
23
|
+
const describedBy = [supportingText ? `${id}-hint` : '', `${id}-error`].filter(Boolean).join(' ');
|
|
24
|
+
---
|
|
25
|
+
<directo-file-picker class:list={['ds-file-upload', className]} data-max-bytes={maxBytes} data-max-files={maxFiles}
|
|
26
|
+
data-dropzone={dropzone ? '' : undefined} data-disabled={disabled ? '' : undefined} data-required={required ? '' : undefined}>
|
|
27
|
+
<label for={id}>{label}{required && <span class="ds-input-required" aria-hidden="true">*</span>}</label>
|
|
28
|
+
{supportingText && <small id={`${id}-hint`}>{supportingText}</small>}
|
|
29
|
+
<input id={id} type="file" name={name} accept={accept} multiple={multiple} required={required} disabled={disabled}
|
|
30
|
+
aria-describedby={describedBy} />
|
|
31
|
+
<div id={`${id}-error`} role="status"></div>
|
|
32
|
+
<ul hidden></ul>
|
|
33
|
+
</directo-file-picker>
|
|
34
|
+
|
|
35
|
+
<script>
|
|
36
|
+
import { formatBytes, selectFiles } from '../files.js';
|
|
37
|
+
|
|
38
|
+
class DirectoFilePicker extends HTMLElement {
|
|
39
|
+
private files: File[] = [];
|
|
40
|
+
private urls: string[] = [];
|
|
41
|
+
|
|
42
|
+
connectedCallback() {
|
|
43
|
+
this.addEventListener('change', this.select);
|
|
44
|
+
this.addEventListener('dragover', this.dragOver);
|
|
45
|
+
this.addEventListener('dragleave', this.dragLeave);
|
|
46
|
+
this.addEventListener('drop', this.drop);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
disconnectedCallback() {
|
|
50
|
+
this.removeEventListener('change', this.select);
|
|
51
|
+
this.removeEventListener('dragover', this.dragOver);
|
|
52
|
+
this.removeEventListener('dragleave', this.dragLeave);
|
|
53
|
+
this.removeEventListener('drop', this.drop);
|
|
54
|
+
this.releasePreviews();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Files currently kept by the component. */
|
|
58
|
+
get dsFiles(): readonly File[] { return this.files; }
|
|
59
|
+
|
|
60
|
+
private get input() { return this.querySelector<HTMLInputElement>('input[type="file"]'); }
|
|
61
|
+
private get canDrop() { return this.hasAttribute('data-dropzone') && !this.input?.disabled; }
|
|
62
|
+
|
|
63
|
+
private dragOver = (event: DragEvent) => {
|
|
64
|
+
if (!this.canDrop) return;
|
|
65
|
+
event.preventDefault();
|
|
66
|
+
this.setAttribute('data-dragging', '');
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
private dragLeave = (event: DragEvent) => {
|
|
70
|
+
if (!this.contains(event.relatedTarget as Node | null)) this.removeAttribute('data-dragging');
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
private drop = (event: DragEvent) => {
|
|
74
|
+
if (!this.canDrop) return;
|
|
75
|
+
event.preventDefault();
|
|
76
|
+
this.removeAttribute('data-dragging');
|
|
77
|
+
this.add(Array.from(event.dataTransfer?.files ?? []));
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
private select = (event: Event) => {
|
|
81
|
+
if (event.target !== this.input) return;
|
|
82
|
+
this.add(Array.from(this.input?.files ?? []));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
private add(incoming: File[]) {
|
|
86
|
+
const input = this.input;
|
|
87
|
+
if (!input) return;
|
|
88
|
+
const number = (value?: string) => value === undefined || value === '' ? undefined : Number(value);
|
|
89
|
+
const result = selectFiles(this.files, incoming, {
|
|
90
|
+
accept: input.accept, multiple: input.multiple,
|
|
91
|
+
maxBytes: number(this.dataset.maxBytes), maxFiles: number(this.dataset.maxFiles),
|
|
92
|
+
});
|
|
93
|
+
this.renderFiles(result.files, result.rejections.map(rejection => rejection.message).join(' '));
|
|
94
|
+
if (result.rejections.length) {
|
|
95
|
+
this.dispatchEvent(new CustomEvent('ds-reject', { bubbles: true, detail: result.rejections }));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private releasePreviews() {
|
|
100
|
+
this.urls.forEach(url => URL.revokeObjectURL(url));
|
|
101
|
+
this.urls = [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private renderFiles(files: File[], error = '') {
|
|
105
|
+
const input = this.input;
|
|
106
|
+
const status = this.querySelector('[role="status"]');
|
|
107
|
+
const list = this.querySelector('ul');
|
|
108
|
+
if (!input || !status || !list) return;
|
|
109
|
+
const changed = files.length !== this.files.length || files.some((file, index) => file !== this.files[index]);
|
|
110
|
+
this.files = files;
|
|
111
|
+
const transfer = new DataTransfer();
|
|
112
|
+
files.forEach(file => transfer.items.add(file));
|
|
113
|
+
input.files = transfer.files;
|
|
114
|
+
input.required = this.hasAttribute('data-required') && !files.length;
|
|
115
|
+
if (error) input.setAttribute('aria-invalid', 'true'); else input.removeAttribute('aria-invalid');
|
|
116
|
+
this.releasePreviews();
|
|
117
|
+
status.textContent = error;
|
|
118
|
+
list.hidden = !files.length;
|
|
119
|
+
list.replaceChildren(...files.map((file, index) => {
|
|
120
|
+
const item = document.createElement('li');
|
|
121
|
+
if (file.type.startsWith('image/')) {
|
|
122
|
+
const image = document.createElement('img');
|
|
123
|
+
image.src = URL.createObjectURL(file);
|
|
124
|
+
image.alt = '';
|
|
125
|
+
this.urls.push(image.src);
|
|
126
|
+
item.append(image);
|
|
127
|
+
}
|
|
128
|
+
const name = document.createElement('span');
|
|
129
|
+
name.textContent = file.name;
|
|
130
|
+
const size = document.createElement('small');
|
|
131
|
+
size.textContent = formatBytes(file.size);
|
|
132
|
+
const button = document.createElement('button');
|
|
133
|
+
button.type = 'button';
|
|
134
|
+
button.className = 'ds-button';
|
|
135
|
+
button.dataset.variant = 'text';
|
|
136
|
+
button.dataset.size = 'sm';
|
|
137
|
+
button.disabled = input.disabled;
|
|
138
|
+
button.textContent = 'Quitar';
|
|
139
|
+
button.setAttribute('aria-label', 'Quitar ' + file.name);
|
|
140
|
+
button.addEventListener('click', () => {
|
|
141
|
+
this.renderFiles(files.filter((_, position) => position !== index));
|
|
142
|
+
this.dispatchEvent(new CustomEvent('ds-remove', { bubbles: true, detail: file }));
|
|
143
|
+
input.focus();
|
|
144
|
+
});
|
|
145
|
+
item.append(name, size, button);
|
|
146
|
+
return item;
|
|
147
|
+
}));
|
|
148
|
+
if (changed) this.dispatchEvent(new CustomEvent('ds-change', { bubbles: true, detail: files }));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (!customElements.get('directo-file-picker')) customElements.define('directo-file-picker', DirectoFilePicker);
|
|
152
|
+
</script>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
---
|
|
2
|
+
interface ActiveFilter {
|
|
3
|
+
label: string;
|
|
4
|
+
/** Link that removes the filter (server-rendered pages). */
|
|
5
|
+
removeHref?: string;
|
|
6
|
+
/** Without removeHref the chip is a button that emits ds-filter-remove with this id. */
|
|
7
|
+
id?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface Props {
|
|
11
|
+
label?: string;
|
|
12
|
+
activeFilters?: readonly ActiveFilter[];
|
|
13
|
+
/** Link that removes every filter. */
|
|
14
|
+
clearHref?: string;
|
|
15
|
+
/** Without clearHref: shows a button that emits ds-filters-clear. */
|
|
16
|
+
clearable?: boolean;
|
|
17
|
+
clearLabel?: string;
|
|
18
|
+
/** Number of results, announced to screen readers. */
|
|
19
|
+
resultCount?: number;
|
|
20
|
+
resultSingular?: string;
|
|
21
|
+
resultPlural?: string;
|
|
22
|
+
/** Renders a GET search form that sends the named fields to this URL (use "" for the same page). */
|
|
23
|
+
action?: string;
|
|
24
|
+
id?: string;
|
|
25
|
+
class?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
label = 'Filtros', activeFilters = [], clearHref, clearable = false, clearLabel = 'Limpiar filtros',
|
|
30
|
+
resultCount, resultSingular = 'resultado', resultPlural = 'resultados', action, id, class: className,
|
|
31
|
+
} = Astro.props;
|
|
32
|
+
const Tag = action === undefined ? 'div' : 'form';
|
|
33
|
+
const formProps = action === undefined ? { role: 'group' } : { role: 'search', method: 'get', action };
|
|
34
|
+
---
|
|
35
|
+
<directo-filter-bar style="display: contents">
|
|
36
|
+
<Tag id={id} class:list={['ds-filter-bar', className]} aria-label={label} {...formProps}>
|
|
37
|
+
<div class="ds-filter-fields"><slot /></div>
|
|
38
|
+
{Astro.slots.has('actions') && <div class="ds-filter-actions"><slot name="actions" /></div>}
|
|
39
|
+
{(activeFilters.length > 0 || resultCount !== undefined) && <div class="ds-filter-active">
|
|
40
|
+
{resultCount !== undefined && <span class="ds-filter-count" role="status">{resultCount} {resultCount === 1 ? resultSingular : resultPlural}</span>}
|
|
41
|
+
{activeFilters.map(filter => filter.removeHref !== undefined
|
|
42
|
+
? <a class="ds-filter-chip" href={filter.removeHref} aria-label={`Quitar filtro: ${filter.label}`}>{filter.label} <span aria-hidden="true">×</span></a>
|
|
43
|
+
: <button class="ds-filter-chip" type="button" data-filter-id={filter.id} aria-label={`Quitar filtro: ${filter.label}`}>{filter.label} <span aria-hidden="true">×</span></button>)}
|
|
44
|
+
{activeFilters.length > 0 && clearHref && <a href={clearHref} class="ds-button" data-variant="text" data-size="sm">{clearLabel}</a>}
|
|
45
|
+
{activeFilters.length > 0 && !clearHref && clearable && <button type="button" class="ds-button" data-variant="text" data-size="sm" data-filters-clear>{clearLabel}</button>}
|
|
46
|
+
</div>}
|
|
47
|
+
</Tag>
|
|
48
|
+
</directo-filter-bar>
|
|
49
|
+
|
|
50
|
+
<script>
|
|
51
|
+
class DirectoFilterBarElement extends HTMLElement {
|
|
52
|
+
connectedCallback() { this.addEventListener('click', this.handleClick); }
|
|
53
|
+
disconnectedCallback() { this.removeEventListener('click', this.handleClick); }
|
|
54
|
+
|
|
55
|
+
private handleClick = (event: MouseEvent) => {
|
|
56
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
57
|
+
const chip = target?.closest<HTMLButtonElement>('button[data-filter-id]');
|
|
58
|
+
if (chip) {
|
|
59
|
+
this.dispatchEvent(new CustomEvent('ds-filter-remove', { bubbles: true, detail: chip.dataset.filterId }));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (target?.closest('button[data-filters-clear]')) this.dispatchEvent(new CustomEvent('ds-filters-clear', { bubbles: true }));
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (!customElements.get('directo-filter-bar')) customElements.define('directo-filter-bar', DirectoFilterBarElement);
|
|
66
|
+
</script>
|