@happyvertical/smrt-ui 0.43.4 → 0.43.5
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/AGENTS.md +33 -0
- package/README.md +50 -4
- package/dist/components/forms/Checkbox.svelte +15 -6
- package/dist/components/forms/Checkbox.svelte.d.ts.map +1 -1
- package/dist/components/forms/Combobox.svelte +45 -10
- package/dist/components/forms/Combobox.svelte.d.ts.map +1 -1
- package/dist/components/forms/FilePicker.svelte +5 -2
- package/dist/components/forms/Form.svelte +57 -0
- package/dist/components/forms/Form.svelte.d.ts +2 -0
- package/dist/components/forms/Form.svelte.d.ts.map +1 -1
- package/dist/components/forms/Input.svelte +59 -13
- package/dist/components/forms/Input.svelte.d.ts.map +1 -1
- package/dist/components/forms/Listbox.svelte +33 -6
- package/dist/components/forms/Listbox.svelte.d.ts.map +1 -1
- package/dist/components/forms/MultiSelect.svelte +41 -19
- package/dist/components/forms/MultiSelect.svelte.d.ts.map +1 -1
- package/dist/components/forms/RadioGroup.svelte +14 -3
- package/dist/components/forms/RadioGroup.svelte.d.ts.map +1 -1
- package/dist/components/forms/RangeSlider.svelte +61 -6
- package/dist/components/forms/RangeSlider.svelte.d.ts.map +1 -1
- package/dist/components/forms/SegmentedControl.svelte +33 -6
- package/dist/components/forms/SegmentedControl.svelte.d.ts.map +1 -1
- package/dist/components/forms/Select.svelte +32 -9
- package/dist/components/forms/Select.svelte.d.ts.map +1 -1
- package/dist/components/forms/Slider.svelte +26 -6
- package/dist/components/forms/Slider.svelte.d.ts.map +1 -1
- package/dist/components/forms/StagedControlReview.svelte +637 -0
- package/dist/components/forms/StagedControlReview.svelte.d.ts +13 -0
- package/dist/components/forms/StagedControlReview.svelte.d.ts.map +1 -0
- package/dist/components/forms/Switch.svelte +16 -7
- package/dist/components/forms/Switch.svelte.d.ts.map +1 -1
- package/dist/components/forms/TagsInput.svelte +36 -8
- package/dist/components/forms/TagsInput.svelte.d.ts.map +1 -1
- package/dist/components/forms/Textarea.svelte +20 -8
- package/dist/components/forms/Textarea.svelte.d.ts.map +1 -1
- package/dist/components/forms/ToggleButton.svelte +20 -3
- package/dist/components/forms/ToggleButton.svelte.d.ts.map +1 -1
- package/dist/components/forms/__tests__/Form.test.js +106 -0
- package/dist/components/forms/__tests__/StagedControlReview.test.js +668 -0
- package/dist/components/forms/__tests__/composite-controls.fixture.svelte +22 -4
- package/dist/components/forms/__tests__/composite-controls.fixture.svelte.d.ts +6 -2
- package/dist/components/forms/__tests__/composite-controls.fixture.svelte.d.ts.map +1 -1
- package/dist/components/forms/__tests__/composite-controls.test.js +96 -1
- package/dist/components/forms/__tests__/composite-user-edit.fixture.svelte +41 -0
- package/dist/components/forms/__tests__/composite-user-edit.fixture.svelte.d.ts +13 -0
- package/dist/components/forms/__tests__/composite-user-edit.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/__tests__/control-interaction.test.js +3323 -15
- package/dist/components/forms/__tests__/core-controls.fixture.svelte +16 -5
- package/dist/components/forms/__tests__/core-controls.fixture.svelte.d.ts +1 -0
- package/dist/components/forms/__tests__/core-controls.fixture.svelte.d.ts.map +1 -1
- package/dist/components/forms/__tests__/core-controls.test.js +223 -2
- package/dist/components/forms/__tests__/decimal-sliders.fixture.svelte +25 -0
- package/dist/components/forms/__tests__/decimal-sliders.fixture.svelte.d.ts +8 -0
- package/dist/components/forms/__tests__/decimal-sliders.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/__tests__/select-interaction.fixture.svelte +25 -0
- package/dist/components/forms/__tests__/select-interaction.fixture.svelte.d.ts +9 -0
- package/dist/components/forms/__tests__/select-interaction.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/__tests__/staged-review-fieldset.fixture.svelte +21 -0
- package/dist/components/forms/__tests__/staged-review-fieldset.fixture.svelte.d.ts +8 -0
- package/dist/components/forms/__tests__/staged-review-fieldset.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/__tests__/staged-review-outer-fieldset.fixture.svelte +23 -0
- package/dist/components/forms/__tests__/staged-review-outer-fieldset.fixture.svelte.d.ts +8 -0
- package/dist/components/forms/__tests__/staged-review-outer-fieldset.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/__tests__/staged-review.fixture.svelte +56 -0
- package/dist/components/forms/__tests__/staged-review.fixture.svelte.d.ts +12 -0
- package/dist/components/forms/__tests__/staged-review.fixture.svelte.d.ts.map +1 -0
- package/dist/components/forms/control-interaction-context.d.ts +9 -1
- package/dist/components/forms/control-interaction-context.d.ts.map +1 -1
- package/dist/components/forms/control-interaction-context.js +16 -0
- package/dist/components/forms/control-interaction.d.ts +97 -7
- package/dist/components/forms/control-interaction.d.ts.map +1 -1
- package/dist/components/forms/control-interaction.js +1019 -92
- package/dist/components/forms/control-value-validation.d.ts +19 -0
- package/dist/components/forms/control-value-validation.d.ts.map +1 -0
- package/dist/components/forms/control-value-validation.js +152 -0
- package/dist/components/forms/index.d.ts +3 -1
- package/dist/components/forms/index.d.ts.map +1 -1
- package/dist/components/forms/index.js +2 -1
- package/dist/components/forms/staged-control-review.d.ts +21 -0
- package/dist/components/forms/staged-control-review.d.ts.map +1 -0
- package/dist/components/forms/staged-control-review.js +1 -0
- package/dist/components/forms/use-control-registration.svelte.d.ts.map +1 -1
- package/dist/components/forms/use-control-registration.svelte.js +23 -4
- package/dist/i18n/strings.ui.d.ts +18 -0
- package/dist/i18n/strings.ui.d.ts.map +1 -1
- package/dist/i18n/strings.ui.js +19 -0
- package/dist/svelte/playground/InteractiveControlsPreview.svelte +3 -4
- package/dist/svelte/playground/InteractiveControlsPreview.svelte.d.ts.map +1 -1
- package/package.json +2 -2
|
@@ -1,11 +1,24 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { tick } from 'svelte';
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
emitControlChange,
|
|
5
|
+
highlightControl,
|
|
6
|
+
revealControl,
|
|
7
|
+
} from './control-dom.js';
|
|
4
8
|
import type {
|
|
5
9
|
ControlInteractionOptions,
|
|
6
10
|
ControlOption,
|
|
7
11
|
} from './control-interaction.js';
|
|
8
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
recordControlUserEdit,
|
|
14
|
+
tryGetControlInteractionContext,
|
|
15
|
+
} from './control-interaction-context.js';
|
|
16
|
+
import {
|
|
17
|
+
normalizeCurrentOptionValues,
|
|
18
|
+
normalizeEnabledOptions,
|
|
19
|
+
prepareEnabledOptionValues,
|
|
20
|
+
validatesEnabledOptions,
|
|
21
|
+
} from './control-value-validation.js';
|
|
9
22
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
10
23
|
export interface Props {
|
|
11
24
|
options: ControlOption[];
|
|
@@ -40,30 +53,32 @@ let open = $state(false);
|
|
|
40
53
|
const controlId = $derived(
|
|
41
54
|
interaction === false ? undefined : (interaction?.id ?? name ?? listId),
|
|
42
55
|
);
|
|
56
|
+
const canonicalValues = $derived(normalizeCurrentOptionValues(options, values));
|
|
43
57
|
const selectedLabels = $derived(
|
|
44
58
|
options
|
|
45
59
|
.filter((option) =>
|
|
46
|
-
|
|
60
|
+
canonicalValues.some((value) => Object.is(value, option.value)),
|
|
47
61
|
)
|
|
48
62
|
.map((option) => option.label),
|
|
49
63
|
);
|
|
50
64
|
function toggle(option: ControlOption) {
|
|
51
65
|
if (option.disabled || disabled) return;
|
|
52
|
-
const has =
|
|
66
|
+
const has = canonicalValues.some((value) => Object.is(value, option.value));
|
|
53
67
|
values = has
|
|
54
|
-
?
|
|
55
|
-
: [...
|
|
68
|
+
? canonicalValues.filter((value) => !Object.is(value, option.value))
|
|
69
|
+
: [...canonicalValues, option.value];
|
|
56
70
|
onvalueschange?.(values);
|
|
71
|
+
recordControlUserEdit(
|
|
72
|
+
interactionContext,
|
|
73
|
+
controlId,
|
|
74
|
+
interaction === false ? undefined : interaction?.subject,
|
|
75
|
+
);
|
|
76
|
+
if (rootEl) emitControlChange(rootEl);
|
|
57
77
|
}
|
|
58
78
|
function setValues(next: unknown) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
(option) =>
|
|
63
|
-
next.some((value) => String(value) === String(option.value)) &&
|
|
64
|
-
!option.disabled,
|
|
65
|
-
)
|
|
66
|
-
.map((option) => option.value);
|
|
79
|
+
const normalized = normalizeEnabledOptions(options, next);
|
|
80
|
+
if (!normalized) return;
|
|
81
|
+
values = normalized;
|
|
67
82
|
onvalueschange?.(values);
|
|
68
83
|
}
|
|
69
84
|
async function openOptions(focusFirst = false) {
|
|
@@ -136,19 +151,26 @@ useControlRegistration(() => {
|
|
|
136
151
|
writable: interaction?.writable,
|
|
137
152
|
options,
|
|
138
153
|
},
|
|
139
|
-
getValue: () => [...
|
|
154
|
+
getValue: () => [...canonicalValues],
|
|
155
|
+
prepareValue: (next) => prepareEnabledOptionValues(options, next),
|
|
140
156
|
setValue: setValues,
|
|
141
|
-
clear: () =>
|
|
157
|
+
clear: () => {
|
|
158
|
+
setValues([]);
|
|
159
|
+
return true;
|
|
160
|
+
},
|
|
142
161
|
focus: () => trigger.focus(),
|
|
143
162
|
reveal: () => revealControl(root),
|
|
144
163
|
highlight: (durationMs) => highlightControl(root, durationMs),
|
|
145
|
-
|
|
164
|
+
validateValue: (next) => validatesEnabledOptions(options, next),
|
|
165
|
+
getState: () => ({ disabled: trigger.matches(':disabled') }),
|
|
146
166
|
};
|
|
147
167
|
});
|
|
148
168
|
</script>
|
|
149
|
-
<div bind:this={rootEl} class="multi-select {className}" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
169
|
+
<div bind:this={rootEl} class="multi-select {className}" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
170
|
+
data-smrt-subject-type={interaction === false ? undefined : interaction?.subject?.type}
|
|
171
|
+
data-smrt-subject-id={interaction === false ? undefined : interaction?.subject?.id}>
|
|
150
172
|
<span class="label" id={`${listId}-label`}>{label}</span><button bind:this={triggerEl} id={triggerId} type="button" class="trigger" {disabled} aria-haspopup="listbox" aria-expanded={open} aria-controls={listId} aria-labelledby={`${listId}-label ${triggerId}`} onclick={() => open ? closeOptions() : openOptions()} onkeydown={handleTriggerKeydown}>{selectedLabels.length ? selectedLabels.join(', ') : placeholder}</button>
|
|
151
|
-
{#if open}<div id={listId} class="options" role="listbox" tabindex="-1" aria-multiselectable="true" aria-labelledby={`${listId}-label`} onkeydown={handleOptionsKeydown}>{#each options as option, index (option.value)}<button bind:this={optionEls[index]} type="button" role="option" tabindex="-1" aria-selected={
|
|
173
|
+
{#if open}<div id={listId} class="options" role="listbox" tabindex="-1" aria-multiselectable="true" aria-labelledby={`${listId}-label`} onkeydown={handleOptionsKeydown}>{#each options as option, index (option.value)}<button bind:this={optionEls[index]} type="button" role="option" tabindex="-1" aria-selected={canonicalValues.some((value) => Object.is(value, option.value))} disabled={option.disabled} onclick={() => toggle(option)}><span aria-hidden="true">{canonicalValues.some((value) => Object.is(value, option.value)) ? '✓' : ''}</span>{option.label}</button>{/each}</div>{/if}
|
|
152
174
|
</div>
|
|
153
175
|
<style>
|
|
154
176
|
.multi-select { position: relative; display: grid; gap: var(--smrt-spacing-1); color: var(--smrt-color-on-surface); } .label { font: var(--smrt-typography-label-large-font); }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultiSelect.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/MultiSelect.svelte.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MultiSelect.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/MultiSelect.svelte.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EACV,yBAAyB,EACzB,aAAa,EACd,MAAM,0BAA0B,CAAC;AAalC,MAAM,WAAW,KAAK;IACpB,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,MAAM,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,KAAK,IAAI,CAAC;IAC1D,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAwJD,QAAA,MAAM,WAAW,iDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -7,6 +7,10 @@ import type {
|
|
|
7
7
|
ControlOption,
|
|
8
8
|
} from './control-interaction.js';
|
|
9
9
|
import { tryGetControlInteractionContext } from './control-interaction-context.js';
|
|
10
|
+
import {
|
|
11
|
+
prepareEnabledOptionValue,
|
|
12
|
+
validatesEnabledOption,
|
|
13
|
+
} from './control-value-validation.js';
|
|
10
14
|
import { setRadioGroupContext } from './radio-group-context.js';
|
|
11
15
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
12
16
|
export interface Props {
|
|
@@ -90,6 +94,7 @@ useControlRegistration(() => {
|
|
|
90
94
|
options,
|
|
91
95
|
},
|
|
92
96
|
getValue: () => value,
|
|
97
|
+
prepareValue: (next) => String(prepareEnabledOptionValue(options, next)),
|
|
93
98
|
setValue: (next) => {
|
|
94
99
|
const candidate = String(next);
|
|
95
100
|
if (
|
|
@@ -99,14 +104,18 @@ useControlRegistration(() => {
|
|
|
99
104
|
)
|
|
100
105
|
setValue(candidate);
|
|
101
106
|
},
|
|
102
|
-
clear: () =>
|
|
107
|
+
clear: () => {
|
|
108
|
+
setValue('');
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
103
111
|
focus: () =>
|
|
104
112
|
element.querySelector<HTMLInputElement>('input:not(:disabled)')?.focus(),
|
|
105
113
|
reveal: () => revealControl(element),
|
|
106
114
|
highlight: (durationMs) => highlightControl(element, durationMs),
|
|
107
115
|
validate: () => element.reportValidity(),
|
|
116
|
+
validateValue: (next) => validatesEnabledOption(options, next),
|
|
108
117
|
getState: () => ({
|
|
109
|
-
disabled,
|
|
118
|
+
disabled: element.matches(':disabled'),
|
|
110
119
|
valid: !required || !!value,
|
|
111
120
|
validationMessage: error,
|
|
112
121
|
}),
|
|
@@ -114,7 +123,9 @@ useControlRegistration(() => {
|
|
|
114
123
|
});
|
|
115
124
|
</script>
|
|
116
125
|
<fieldset bind:this={fieldsetEl} class="radio-group {className}" {disabled}
|
|
117
|
-
aria-describedby={description ? `${name}-description` : undefined} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
126
|
+
aria-describedby={description ? `${name}-description` : undefined} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
127
|
+
data-smrt-subject-type={interaction === false ? undefined : interaction?.subject?.type}
|
|
128
|
+
data-smrt-subject-id={interaction === false ? undefined : interaction?.subject?.id}>
|
|
118
129
|
{#if label}<legend>{label}{#if required}<span aria-hidden="true"> *</span>{/if}</legend>{/if}
|
|
119
130
|
{#if description}<p id={`${name}-description`} class="description">{description}</p>{/if}
|
|
120
131
|
<div class="options">{@render children()}</div>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RadioGroup.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/RadioGroup.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,KAAK,EACV,yBAAyB,EAE1B,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"RadioGroup.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/RadioGroup.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAGtC,OAAO,KAAK,EACV,yBAAyB,EAE1B,MAAM,0BAA0B,CAAC;AASlC,MAAM,WAAW,KAAK;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,QAAQ,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAqHD,QAAA,MAAM,UAAU,gDAAwC,CAAC;AACzD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;AAChD,eAAe,UAAU,CAAC"}
|
|
@@ -6,6 +6,10 @@ import {
|
|
|
6
6
|
} from './control-dom.js';
|
|
7
7
|
import type { ControlInteractionOptions } from './control-interaction.js';
|
|
8
8
|
import { tryGetControlInteractionContext } from './control-interaction-context.js';
|
|
9
|
+
import {
|
|
10
|
+
snapSteppedNumber,
|
|
11
|
+
validatesSteppedNumber,
|
|
12
|
+
} from './control-value-validation.js';
|
|
9
13
|
import { tryGetFormGroupContext } from './form-group-context.js';
|
|
10
14
|
import type { RangeSliderValue } from './types.js';
|
|
11
15
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
@@ -61,7 +65,22 @@ const controlId = $derived(
|
|
|
61
65
|
`range-${instanceId}`),
|
|
62
66
|
);
|
|
63
67
|
function snap(next: number) {
|
|
64
|
-
return
|
|
68
|
+
return snapSteppedNumber(next, min, max, step);
|
|
69
|
+
}
|
|
70
|
+
function validateRange(next: unknown) {
|
|
71
|
+
if (!next || typeof next !== 'object' || Array.isArray(next)) return false;
|
|
72
|
+
const candidate = next as Record<string, unknown>;
|
|
73
|
+
if (
|
|
74
|
+
!Object.hasOwn(candidate, 'min') ||
|
|
75
|
+
!Object.hasOwn(candidate, 'max') ||
|
|
76
|
+
Object.keys(candidate).some((key) => key !== 'min' && key !== 'max')
|
|
77
|
+
)
|
|
78
|
+
return false;
|
|
79
|
+
return (
|
|
80
|
+
validatesSteppedNumber(candidate.min, min, max, step) &&
|
|
81
|
+
validatesSteppedNumber(candidate.max, min, max, step) &&
|
|
82
|
+
Number(candidate.min) <= Number(candidate.max)
|
|
83
|
+
);
|
|
65
84
|
}
|
|
66
85
|
function setRange(next: unknown) {
|
|
67
86
|
if (!next || typeof next !== 'object') return;
|
|
@@ -70,8 +89,31 @@ function setRange(next: unknown) {
|
|
|
70
89
|
const high = snap(Number(candidate.max ?? value.max));
|
|
71
90
|
value = { min: Math.min(low, high), max: Math.max(low, high) };
|
|
72
91
|
onvaluechange?.(value);
|
|
73
|
-
if (
|
|
74
|
-
|
|
92
|
+
if (rootEl) emitControlChange(rootEl);
|
|
93
|
+
}
|
|
94
|
+
function prepareRange(next: unknown): RangeSliderValue {
|
|
95
|
+
if (!next || typeof next !== 'object' || Array.isArray(next)) {
|
|
96
|
+
throw new Error('staged_value_invalid');
|
|
97
|
+
}
|
|
98
|
+
const candidate = next as Partial<RangeSliderValue>;
|
|
99
|
+
if (
|
|
100
|
+
(typeof candidate.min !== 'number' && typeof candidate.min !== 'string') ||
|
|
101
|
+
(typeof candidate.max !== 'number' && typeof candidate.max !== 'string')
|
|
102
|
+
) {
|
|
103
|
+
throw new Error('staged_value_invalid');
|
|
104
|
+
}
|
|
105
|
+
const prepared = { min: Number(candidate.min), max: Number(candidate.max) };
|
|
106
|
+
if (!Number.isFinite(prepared.min) || !Number.isFinite(prepared.max)) {
|
|
107
|
+
throw new Error('staged_value_invalid');
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
min: validatesSteppedNumber(prepared.min, min, max, step)
|
|
111
|
+
? snap(prepared.min)
|
|
112
|
+
: prepared.min,
|
|
113
|
+
max: validatesSteppedNumber(prepared.max, min, max, step)
|
|
114
|
+
? snap(prepared.max)
|
|
115
|
+
: prepared.max,
|
|
116
|
+
};
|
|
75
117
|
}
|
|
76
118
|
function setMin(next: number) {
|
|
77
119
|
setRange({ min: Math.min(next, value.max), max: value.max });
|
|
@@ -97,17 +139,30 @@ useControlRegistration(() => {
|
|
|
97
139
|
unit,
|
|
98
140
|
},
|
|
99
141
|
getValue: () => ({ ...value }),
|
|
142
|
+
prepareValue: prepareRange,
|
|
100
143
|
setValue: setRange,
|
|
101
|
-
clear: () =>
|
|
144
|
+
clear: () => {
|
|
145
|
+
setRange({ min, max });
|
|
146
|
+
return true;
|
|
147
|
+
},
|
|
102
148
|
focus: () => minEl?.focus(),
|
|
103
149
|
reveal: () => revealControl(root),
|
|
104
150
|
highlight: (durationMs) => highlightControl(root, durationMs),
|
|
105
151
|
validate: () => value.min <= value.max,
|
|
106
|
-
|
|
152
|
+
validateValue: validateRange,
|
|
153
|
+
getState: () => ({
|
|
154
|
+
disabled:
|
|
155
|
+
disabled ||
|
|
156
|
+
minEl?.matches(':disabled') === true ||
|
|
157
|
+
maxEl?.matches(':disabled') === true,
|
|
158
|
+
valid: value.min <= value.max,
|
|
159
|
+
}),
|
|
107
160
|
};
|
|
108
161
|
});
|
|
109
162
|
</script>
|
|
110
|
-
<div bind:this={rootEl} class="range-slider {className}" class:disabled data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
163
|
+
<div bind:this={rootEl} class="range-slider {className}" class:disabled data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
164
|
+
data-smrt-subject-type={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.type}
|
|
165
|
+
data-smrt-subject-id={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.id} role="group" aria-label={label ?? formGroup?.().label ?? 'Range'}>
|
|
111
166
|
{#if label}<div class="range-slider__header"><span>{label}</span><output>{value.min}{unit ?? ''} – {value.max}{unit ?? ''}</output></div>{/if}
|
|
112
167
|
<div class="range-slider__track">
|
|
113
168
|
<input bind:this={minEl} id={minId} type="range" name={name ? `${name}[min]` : undefined} {min} max={value.max} {step} {disabled} value={value.min}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RangeSlider.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/RangeSlider.svelte.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"RangeSlider.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/RangeSlider.svelte.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAO1E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGnD,MAAM,WAAW,KAAK;IACpB,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,IAAI,CAAC;IAClD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AA6JD,QAAA,MAAM,WAAW,gDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
emitControlChange,
|
|
4
|
+
highlightControl,
|
|
5
|
+
revealControl,
|
|
6
|
+
} from './control-dom.js';
|
|
3
7
|
import type { ControlInteractionOptions } from './control-interaction.js';
|
|
4
|
-
import {
|
|
8
|
+
import {
|
|
9
|
+
recordControlUserEdit,
|
|
10
|
+
tryGetControlInteractionContext,
|
|
11
|
+
} from './control-interaction-context.js';
|
|
12
|
+
import {
|
|
13
|
+
prepareEnabledOptionValue,
|
|
14
|
+
validatesEnabledOption,
|
|
15
|
+
} from './control-value-validation.js';
|
|
5
16
|
import type { SegmentedControlOption } from './types.js';
|
|
6
17
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
7
18
|
export interface Props {
|
|
@@ -34,11 +45,20 @@ const controlId = $derived(
|
|
|
34
45
|
? undefined
|
|
35
46
|
: (interaction?.id ?? name ?? `segment-${instanceId}`),
|
|
36
47
|
);
|
|
37
|
-
function setValue(next: unknown) {
|
|
48
|
+
function setValue(next: unknown, userEdit = false) {
|
|
38
49
|
const option = options.find((item) => String(item.value) === String(next));
|
|
39
50
|
if (!option || option.disabled) return;
|
|
51
|
+
const changed = !Object.is(value, option.value);
|
|
40
52
|
value = option.value;
|
|
41
53
|
onvaluechange?.(option.value);
|
|
54
|
+
if (userEdit && changed) {
|
|
55
|
+
recordControlUserEdit(
|
|
56
|
+
interactionContext,
|
|
57
|
+
controlId,
|
|
58
|
+
interaction === false ? undefined : interaction?.subject,
|
|
59
|
+
);
|
|
60
|
+
if (rootEl) emitControlChange(rootEl);
|
|
61
|
+
}
|
|
42
62
|
}
|
|
43
63
|
useControlRegistration(() => {
|
|
44
64
|
const root = rootEl;
|
|
@@ -56,9 +76,11 @@ useControlRegistration(() => {
|
|
|
56
76
|
options,
|
|
57
77
|
},
|
|
58
78
|
getValue: () => value,
|
|
79
|
+
prepareValue: (next) => prepareEnabledOptionValue(options, next),
|
|
59
80
|
setValue,
|
|
60
81
|
clear: () => {
|
|
61
82
|
value = undefined;
|
|
83
|
+
return true;
|
|
62
84
|
},
|
|
63
85
|
focus: () =>
|
|
64
86
|
root
|
|
@@ -66,15 +88,20 @@ useControlRegistration(() => {
|
|
|
66
88
|
?.focus(),
|
|
67
89
|
reveal: () => revealControl(root),
|
|
68
90
|
highlight: (durationMs) => highlightControl(root, durationMs),
|
|
69
|
-
|
|
91
|
+
validateValue: (next) => validatesEnabledOption(options, next),
|
|
92
|
+
getState: () => ({
|
|
93
|
+
disabled: disabled || root.closest('fieldset:disabled') !== null,
|
|
94
|
+
}),
|
|
70
95
|
};
|
|
71
96
|
});
|
|
72
97
|
</script>
|
|
73
|
-
<div bind:this={rootEl} class="segmented {className}" class:full-width={fullWidth} role="radiogroup" aria-label={label} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
98
|
+
<div bind:this={rootEl} class="segmented {className}" class:full-width={fullWidth} role="radiogroup" aria-label={label} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
99
|
+
data-smrt-subject-type={interaction === false ? undefined : interaction?.subject?.type}
|
|
100
|
+
data-smrt-subject-id={interaction === false ? undefined : interaction?.subject?.id}>
|
|
74
101
|
{#each options as option (option.value)}
|
|
75
102
|
<button type="button" role="radio" aria-checked={value === option.value} disabled={disabled || option.disabled}
|
|
76
103
|
tabindex={value === option.value || (value === undefined && option === options[0]) ? 0 : -1}
|
|
77
|
-
class:selected={value === option.value} onclick={() => setValue(option.value)}>{option.label}</button>
|
|
104
|
+
class:selected={value === option.value} onclick={() => setValue(option.value, true)}>{option.label}</button>
|
|
78
105
|
{/each}
|
|
79
106
|
</div>
|
|
80
107
|
<style>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SegmentedControl.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/SegmentedControl.svelte.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"SegmentedControl.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/SegmentedControl.svelte.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAS1E,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAGzD,MAAM,WAAW,KAAK;IACpB,OAAO,EAAE,sBAAsB,EAAE,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,CAAC;IACjD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAuFD,QAAA,MAAM,gBAAgB,gDAAwC,CAAC;AAC/D,KAAK,gBAAgB,GAAG,UAAU,CAAC,OAAO,gBAAgB,CAAC,CAAC;AAC5D,eAAe,gBAAgB,CAAC"}
|
|
@@ -8,7 +8,9 @@ import {
|
|
|
8
8
|
} from './control-dom.js';
|
|
9
9
|
import type { ControlInteractionOptions } from './control-interaction.js';
|
|
10
10
|
import { tryGetControlInteractionContext } from './control-interaction-context.js';
|
|
11
|
+
import { prepareTextControlValue } from './control-value-validation.js';
|
|
11
12
|
import { tryGetFormGroupContext } from './form-group-context.js';
|
|
13
|
+
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
12
14
|
|
|
13
15
|
export interface Props extends Omit<HTMLSelectAttributes, 'class' | 'value'> {
|
|
14
16
|
value?: string;
|
|
@@ -59,14 +61,28 @@ function setControlValue(next: unknown) {
|
|
|
59
61
|
if (selectEl) emitControlChange(selectEl);
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
function isOptionDisabled(option: HTMLOptionElement) {
|
|
65
|
+
return option.disabled || option.closest('optgroup')?.disabled === true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function validateControlValue(next: unknown) {
|
|
69
|
+
const element = selectEl;
|
|
70
|
+
if (!element) return true;
|
|
71
|
+
const candidate = String(next ?? '');
|
|
72
|
+
if (candidate === '') return !required;
|
|
73
|
+
return Array.from(element.options).some(
|
|
74
|
+
(option) => option.value === candidate && !isOptionDisabled(option),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
useControlRegistration(() => {
|
|
64
79
|
const element = selectEl;
|
|
65
80
|
const controlId = resolvedControlId;
|
|
66
81
|
const options = resolvedInteraction;
|
|
67
|
-
if (!
|
|
68
|
-
return
|
|
69
|
-
|
|
82
|
+
if (!element || !controlId || options === false) return false;
|
|
83
|
+
return {
|
|
84
|
+
controlId,
|
|
85
|
+
subject: options.subject,
|
|
70
86
|
metadata: {
|
|
71
87
|
kind: 'select',
|
|
72
88
|
label: formGroup?.().label ?? ariaLabel ?? undefined,
|
|
@@ -78,22 +94,27 @@ $effect(() => {
|
|
|
78
94
|
options: Array.from(element.options).map((option) => ({
|
|
79
95
|
value: option.value,
|
|
80
96
|
label: option.label,
|
|
81
|
-
disabled: option
|
|
97
|
+
disabled: isOptionDisabled(option),
|
|
82
98
|
})),
|
|
83
99
|
},
|
|
84
100
|
getValue: () => value,
|
|
101
|
+
prepareValue: prepareTextControlValue,
|
|
85
102
|
setValue: setControlValue,
|
|
86
|
-
clear: () =>
|
|
103
|
+
clear: () => {
|
|
104
|
+
setControlValue('');
|
|
105
|
+
return true;
|
|
106
|
+
},
|
|
87
107
|
focus: () => element.focus(),
|
|
88
108
|
reveal: () => revealControl(element),
|
|
89
109
|
highlight: (durationMs) => highlightControl(element, durationMs),
|
|
90
110
|
validate: () => element.reportValidity(),
|
|
111
|
+
validateValue: validateControlValue,
|
|
91
112
|
getState: () => ({
|
|
92
|
-
disabled: element.disabled,
|
|
113
|
+
disabled: element.matches(':disabled'),
|
|
93
114
|
valid: element.validity.valid,
|
|
94
115
|
validationMessage: element.validationMessage || undefined,
|
|
95
116
|
}),
|
|
96
|
-
}
|
|
117
|
+
};
|
|
97
118
|
});
|
|
98
119
|
|
|
99
120
|
export function focus(): void {
|
|
@@ -118,6 +139,8 @@ export function getElement(): HTMLSelectElement | null {
|
|
|
118
139
|
class="select {className}"
|
|
119
140
|
data-smrt-control={resolvedControlId}
|
|
120
141
|
data-smrt-form={controlInteraction?.formId}
|
|
142
|
+
data-smrt-subject-type={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.type}
|
|
143
|
+
data-smrt-subject-id={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.id}
|
|
121
144
|
{...rest}
|
|
122
145
|
>
|
|
123
146
|
{@render children()}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Select.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Select.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAM5D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Select.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Select.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAM5D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAO1E,MAAM,WAAW,KAAM,SAAQ,IAAI,CAAC,oBAAoB,EAAE,OAAO,GAAG,OAAO,CAAC;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,QAAQ,EAAE,OAAO,CAAC;CACnB;AA+HD,QAAA,MAAM,MAAM;iBAjBO,IAAI;sBAIC,iBAAiB,GAAG,IAAI;WAaI,CAAC;AACrD,KAAK,MAAM,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC;AACxC,eAAe,MAAM,CAAC"}
|
|
@@ -7,6 +7,11 @@ import {
|
|
|
7
7
|
} from './control-dom.js';
|
|
8
8
|
import type { ControlInteractionOptions } from './control-interaction.js';
|
|
9
9
|
import { tryGetControlInteractionContext } from './control-interaction-context.js';
|
|
10
|
+
import {
|
|
11
|
+
prepareNumberControlValue,
|
|
12
|
+
snapSteppedNumber,
|
|
13
|
+
validatesSteppedNumber,
|
|
14
|
+
} from './control-value-validation.js';
|
|
10
15
|
import { tryGetFormGroupContext } from './form-group-context.js';
|
|
11
16
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
12
17
|
export interface Props
|
|
@@ -61,7 +66,7 @@ const controlId = $derived(
|
|
|
61
66
|
);
|
|
62
67
|
const percent = $derived(max === min ? 0 : ((value - min) / (max - min)) * 100);
|
|
63
68
|
function clamp(next: number) {
|
|
64
|
-
return
|
|
69
|
+
return snapSteppedNumber(next, min, max, step);
|
|
65
70
|
}
|
|
66
71
|
function setValue(next: unknown) {
|
|
67
72
|
const parsed = Number(next);
|
|
@@ -69,11 +74,19 @@ function setValue(next: unknown) {
|
|
|
69
74
|
value = clamp(parsed);
|
|
70
75
|
if (rangeEl) emitControlChange(rangeEl);
|
|
71
76
|
}
|
|
77
|
+
function prepareValue(next: unknown) {
|
|
78
|
+
const prepared = prepareNumberControlValue(next);
|
|
79
|
+
if (prepared === '') return clamp(0);
|
|
80
|
+
return typeof prepared === 'number' &&
|
|
81
|
+
validatesSteppedNumber(prepared, min, max, step)
|
|
82
|
+
? clamp(prepared)
|
|
83
|
+
: prepared;
|
|
84
|
+
}
|
|
72
85
|
function handleInput(event: Event & { currentTarget: HTMLInputElement }) {
|
|
73
|
-
value = Number(event.currentTarget.value);
|
|
86
|
+
value = clamp(Number(event.currentTarget.value));
|
|
74
87
|
}
|
|
75
88
|
function handleChange(event: Event & { currentTarget: HTMLInputElement }) {
|
|
76
|
-
value = Number(event.currentTarget.value);
|
|
89
|
+
value = clamp(Number(event.currentTarget.value));
|
|
77
90
|
onchange?.(event);
|
|
78
91
|
}
|
|
79
92
|
useControlRegistration(() => {
|
|
@@ -94,22 +107,29 @@ useControlRegistration(() => {
|
|
|
94
107
|
unit,
|
|
95
108
|
},
|
|
96
109
|
getValue: () => value,
|
|
110
|
+
prepareValue,
|
|
97
111
|
setValue,
|
|
98
|
-
clear: () =>
|
|
112
|
+
clear: () => {
|
|
113
|
+
setValue(min);
|
|
114
|
+
return true;
|
|
115
|
+
},
|
|
99
116
|
focus: () => element.focus(),
|
|
100
117
|
reveal: () => revealControl(element),
|
|
101
118
|
highlight: (durationMs) =>
|
|
102
119
|
highlightControl(element.closest('.slider') ?? element, durationMs),
|
|
103
120
|
validate: () => element.reportValidity(),
|
|
121
|
+
validateValue: (next) => validatesSteppedNumber(next, min, max, step),
|
|
104
122
|
getState: () => ({
|
|
105
|
-
disabled: element.disabled,
|
|
123
|
+
disabled: element.matches(':disabled'),
|
|
106
124
|
valid: element.validity.valid,
|
|
107
125
|
validationMessage: element.validationMessage || undefined,
|
|
108
126
|
}),
|
|
109
127
|
};
|
|
110
128
|
});
|
|
111
129
|
</script>
|
|
112
|
-
<div class="slider {className}" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
130
|
+
<div class="slider {className}" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
131
|
+
data-smrt-subject-type={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.type}
|
|
132
|
+
data-smrt-subject-id={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.id}>
|
|
113
133
|
{#if label}<div class="slider__header"><label for={resolvedId}>{label}</label><output for={resolvedId}>{formatValue(value)}</output></div>{/if}
|
|
114
134
|
<div class="slider__controls">
|
|
115
135
|
<input bind:this={rangeEl} id={resolvedId} type="range" {name} {min} {max} {step} {disabled} value={value}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Slider.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Slider.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Slider.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Slider.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAU1E,MAAM,WAAW,KACf,SAAQ,IAAI,CACV,mBAAmB,EACnB,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,CACpD;IACD,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACxC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;CACjD;AAsHD,QAAA,MAAM,MAAM,gDAAwC,CAAC;AACrD,KAAK,MAAM,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC;AACxC,eAAe,MAAM,CAAC"}
|