@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
|
@@ -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
|
+
booleanControlValue,
|
|
12
|
+
prepareBooleanControlValue,
|
|
13
|
+
validateNativeCheckedValue,
|
|
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
|
|
|
@@ -57,10 +62,7 @@ const controlId = $derived(
|
|
|
57
62
|
: (resolvedInteraction.id ?? name ?? resolvedId),
|
|
58
63
|
);
|
|
59
64
|
function setChecked(next: unknown) {
|
|
60
|
-
checked =
|
|
61
|
-
typeof next === 'string'
|
|
62
|
-
? ['true', '1', 'yes', 'on'].includes(next.toLowerCase())
|
|
63
|
-
: Boolean(next);
|
|
65
|
+
checked = booleanControlValue(next);
|
|
64
66
|
if (inputEl) emitControlChange(inputEl);
|
|
65
67
|
}
|
|
66
68
|
function handleChange(event: Event & { currentTarget: HTMLInputElement }) {
|
|
@@ -84,15 +86,20 @@ useControlRegistration(() => {
|
|
|
84
86
|
constraints: { required: required === true },
|
|
85
87
|
},
|
|
86
88
|
getValue: () => checked,
|
|
89
|
+
prepareValue: prepareBooleanControlValue,
|
|
87
90
|
setValue: setChecked,
|
|
88
|
-
clear: () =>
|
|
91
|
+
clear: () => {
|
|
92
|
+
setChecked(false);
|
|
93
|
+
return true;
|
|
94
|
+
},
|
|
89
95
|
focus: () => element.focus(),
|
|
90
96
|
reveal: () => revealControl(element),
|
|
91
97
|
highlight: (durationMs) =>
|
|
92
98
|
highlightControl(element.closest('label') ?? element, durationMs),
|
|
93
99
|
validate: () => element.reportValidity(),
|
|
100
|
+
validateValue: (next) => validateNativeCheckedValue(element, next),
|
|
94
101
|
getState: () => ({
|
|
95
|
-
disabled: element.disabled,
|
|
102
|
+
disabled: element.matches(':disabled'),
|
|
96
103
|
valid: element.validity.valid,
|
|
97
104
|
validationMessage: element.validationMessage || undefined,
|
|
98
105
|
}),
|
|
@@ -100,7 +107,9 @@ useControlRegistration(() => {
|
|
|
100
107
|
});
|
|
101
108
|
</script>
|
|
102
109
|
|
|
103
|
-
<label class="switch switch--{size} {className}" class:switch--disabled={disabled} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
110
|
+
<label class="switch switch--{size} {className}" class:switch--disabled={disabled} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
111
|
+
data-smrt-subject-type={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.type}
|
|
112
|
+
data-smrt-subject-id={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.id}>
|
|
104
113
|
{#if label && labelPosition === 'left'}<span>{label}</span>{/if}
|
|
105
114
|
<span class="switch__control">
|
|
106
115
|
<input bind:this={inputEl} id={resolvedId} type="checkbox" role="switch" {name} {value} {disabled} {required} checked={checked}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Switch.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Switch.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":"Switch.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Switch.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAM3D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAW1E,MAAM,WAAW,KACf,SAAQ,IAAI,CACV,mBAAmB,EACnB,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,CAC/C;IACD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;IACjC,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC;IAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;CACjD;AAuGD,QAAA,MAAM,MAAM,kDAAwC,CAAC;AACrD,KAAK,MAAM,GAAG,UAAU,CAAC,OAAO,MAAM,CAAC,CAAC;AACxC,eAAe,MAAM,CAAC"}
|
|
@@ -1,7 +1,15 @@
|
|
|
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 { validatesStringArray } from './control-value-validation.js';
|
|
5
13
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
6
14
|
export interface Props {
|
|
7
15
|
values?: string[];
|
|
@@ -48,17 +56,30 @@ function commit(raw: string) {
|
|
|
48
56
|
values = [...values, tag];
|
|
49
57
|
draft = '';
|
|
50
58
|
onvalueschange?.(values);
|
|
59
|
+
recordControlUserEdit(
|
|
60
|
+
interactionContext,
|
|
61
|
+
controlId,
|
|
62
|
+
interaction === false ? undefined : interaction?.subject,
|
|
63
|
+
);
|
|
64
|
+
if (rootEl) emitControlChange(rootEl);
|
|
51
65
|
}
|
|
52
66
|
function remove(index: number) {
|
|
67
|
+
if (index < 0 || index >= values.length) return;
|
|
53
68
|
values = values.filter((_, candidate) => candidate !== index);
|
|
54
69
|
onvalueschange?.(values);
|
|
70
|
+
recordControlUserEdit(
|
|
71
|
+
interactionContext,
|
|
72
|
+
controlId,
|
|
73
|
+
interaction === false ? undefined : interaction?.subject,
|
|
74
|
+
);
|
|
75
|
+
if (rootEl) emitControlChange(rootEl);
|
|
55
76
|
}
|
|
56
77
|
function setValues(next: unknown) {
|
|
57
78
|
if (!Array.isArray(next)) return;
|
|
58
|
-
|
|
79
|
+
const normalized = next
|
|
59
80
|
.map(String)
|
|
60
|
-
.filter((tag, index, all) => allowDuplicates || all.indexOf(tag) === index)
|
|
61
|
-
|
|
81
|
+
.filter((tag, index, all) => allowDuplicates || all.indexOf(tag) === index);
|
|
82
|
+
values = maxTags === undefined ? normalized : normalized.slice(0, maxTags);
|
|
62
83
|
onvalueschange?.(values);
|
|
63
84
|
}
|
|
64
85
|
function handleKeydown(event: KeyboardEvent) {
|
|
@@ -85,15 +106,22 @@ useControlRegistration(() => {
|
|
|
85
106
|
},
|
|
86
107
|
getValue: () => [...values],
|
|
87
108
|
setValue: setValues,
|
|
88
|
-
clear: () =>
|
|
109
|
+
clear: () => {
|
|
110
|
+
setValues([]);
|
|
111
|
+
return true;
|
|
112
|
+
},
|
|
89
113
|
focus: () => input.focus(),
|
|
90
114
|
reveal: () => revealControl(root),
|
|
91
115
|
highlight: (durationMs) => highlightControl(root, durationMs),
|
|
92
|
-
|
|
116
|
+
validateValue: (next) =>
|
|
117
|
+
validatesStringArray(next, maxTags, allowDuplicates),
|
|
118
|
+
getState: () => ({ disabled: input.matches(':disabled') }),
|
|
93
119
|
};
|
|
94
120
|
});
|
|
95
121
|
</script>
|
|
96
|
-
<div class="tags-field {className}"><label for={inputId}>{label}</label><div bind:this={rootEl} class="tags" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
122
|
+
<div class="tags-field {className}"><label for={inputId}>{label}</label><div bind:this={rootEl} class="tags" data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
123
|
+
data-smrt-subject-type={interaction === false ? undefined : interaction?.subject?.type}
|
|
124
|
+
data-smrt-subject-id={interaction === false ? undefined : interaction?.subject?.id}>
|
|
97
125
|
{#each values as tag, index}<span class="tag">{tag}<button type="button" disabled={disabled} aria-label={`Remove ${tag}`} onclick={() => remove(index)}>×</button><input type="hidden" name={name ? `${name}[]` : undefined} value={tag} /></span>{/each}
|
|
98
126
|
<input bind:this={inputEl} id={inputId} {placeholder} {disabled} bind:value={draft} onkeydown={handleKeydown} onblur={() => commit(draft)} />
|
|
99
127
|
</div></div>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TagsInput.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/TagsInput.svelte.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TagsInput.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/TagsInput.svelte.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAQ1E,MAAM,WAAW,KAAK;IACpB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,IAAI,CAAC;IAC5C,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAgHD,QAAA,MAAM,SAAS,iDAAwC,CAAC;AACxD,KAAK,SAAS,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC;AAC9C,eAAe,SAAS,CAAC"}
|
|
@@ -7,7 +7,12 @@ 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
|
+
prepareTextControlValue,
|
|
12
|
+
validateNativeTextValue,
|
|
13
|
+
} from './control-value-validation.js';
|
|
10
14
|
import { tryGetFormGroupContext } from './form-group-context.js';
|
|
15
|
+
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
11
16
|
|
|
12
17
|
export interface Props extends Omit<HTMLTextareaAttributes, 'class' | 'value'> {
|
|
13
18
|
value?: string;
|
|
@@ -60,14 +65,14 @@ function setControlValue(next: unknown) {
|
|
|
60
65
|
if (textareaEl) emitControlChange(textareaEl);
|
|
61
66
|
}
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
const context = controlInteraction;
|
|
68
|
+
useControlRegistration(() => {
|
|
65
69
|
const element = textareaEl;
|
|
66
70
|
const controlId = resolvedControlId;
|
|
67
71
|
const options = resolvedInteraction;
|
|
68
|
-
if (!
|
|
69
|
-
return
|
|
70
|
-
|
|
72
|
+
if (!element || !controlId || options === false) return false;
|
|
73
|
+
return {
|
|
74
|
+
controlId,
|
|
75
|
+
subject: options.subject,
|
|
71
76
|
metadata: {
|
|
72
77
|
kind: 'textarea',
|
|
73
78
|
label: formGroup?.().label ?? ariaLabel ?? undefined,
|
|
@@ -82,19 +87,24 @@ $effect(() => {
|
|
|
82
87
|
},
|
|
83
88
|
},
|
|
84
89
|
getValue: () => value,
|
|
90
|
+
prepareValue: prepareTextControlValue,
|
|
85
91
|
setValue: setControlValue,
|
|
86
|
-
clear: () =>
|
|
92
|
+
clear: () => {
|
|
93
|
+
setControlValue('');
|
|
94
|
+
return true;
|
|
95
|
+
},
|
|
87
96
|
focus: () => element.focus(),
|
|
88
97
|
reveal: () => revealControl(element),
|
|
89
98
|
highlight: (durationMs) => highlightControl(element, durationMs),
|
|
90
99
|
validate: () => element.reportValidity(),
|
|
100
|
+
validateValue: (next) => validateNativeTextValue(element, next),
|
|
91
101
|
getState: () => ({
|
|
92
|
-
disabled: element.disabled,
|
|
102
|
+
disabled: element.matches(':disabled'),
|
|
93
103
|
readonly: element.readOnly,
|
|
94
104
|
valid: element.validity.valid,
|
|
95
105
|
validationMessage: element.validationMessage || undefined,
|
|
96
106
|
}),
|
|
97
|
-
}
|
|
107
|
+
};
|
|
98
108
|
});
|
|
99
109
|
|
|
100
110
|
export function focus(): void {
|
|
@@ -122,6 +132,8 @@ export function getElement(): HTMLTextAreaElement | null {
|
|
|
122
132
|
class="textarea {className}"
|
|
123
133
|
data-smrt-control={resolvedControlId}
|
|
124
134
|
data-smrt-form={controlInteraction?.formId}
|
|
135
|
+
data-smrt-subject-type={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.type}
|
|
136
|
+
data-smrt-subject-id={resolvedInteraction === false ? undefined : resolvedInteraction.subject?.id}
|
|
125
137
|
{...rest}
|
|
126
138
|
></textarea>
|
|
127
139
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Textarea.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Textarea.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAM9D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"Textarea.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/Textarea.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAM9D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAC;AAU1E,MAAM,WAAW,KAAM,SAAQ,IAAI,CAAC,sBAAsB,EAAE,OAAO,GAAG,OAAO,CAAC;IAC5E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;CACjD;AAgHD,QAAA,MAAM,QAAQ;iBAfK,IAAI;sBAIC,mBAAmB,GAAG,IAAI;WAWI,CAAC;AACvD,KAAK,QAAQ,GAAG,UAAU,CAAC,OAAO,QAAQ,CAAC,CAAC;AAC5C,eAAe,QAAQ,CAAC"}
|
|
@@ -7,7 +7,11 @@ import {
|
|
|
7
7
|
revealControl,
|
|
8
8
|
} from './control-dom.js';
|
|
9
9
|
import type { ControlInteractionOptions } from './control-interaction.js';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
recordControlUserEdit,
|
|
12
|
+
tryGetControlInteractionContext,
|
|
13
|
+
} from './control-interaction-context.js';
|
|
14
|
+
import { prepareBooleanControlValue } from './control-value-validation.js';
|
|
11
15
|
import { useControlRegistration } from './use-control-registration.svelte.js';
|
|
12
16
|
|
|
13
17
|
export interface Props extends Omit<HTMLButtonAttributes, 'class'> {
|
|
@@ -41,6 +45,12 @@ function setPressed(next: unknown) {
|
|
|
41
45
|
}
|
|
42
46
|
function handleClick(event: MouseEvent & { currentTarget: HTMLButtonElement }) {
|
|
43
47
|
pressed = !pressed;
|
|
48
|
+
recordControlUserEdit(
|
|
49
|
+
interactionContext,
|
|
50
|
+
controlId,
|
|
51
|
+
interaction === false ? undefined : interaction?.subject,
|
|
52
|
+
);
|
|
53
|
+
if (buttonEl) emitControlChange(buttonEl);
|
|
44
54
|
onclick?.(event);
|
|
45
55
|
}
|
|
46
56
|
useControlRegistration(() => {
|
|
@@ -58,17 +68,24 @@ useControlRegistration(() => {
|
|
|
58
68
|
writable: interaction?.writable,
|
|
59
69
|
},
|
|
60
70
|
getValue: () => pressed,
|
|
71
|
+
prepareValue: prepareBooleanControlValue,
|
|
61
72
|
setValue: setPressed,
|
|
62
|
-
clear: () =>
|
|
73
|
+
clear: () => {
|
|
74
|
+
setPressed(false);
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
63
77
|
focus: () => element.focus(),
|
|
64
78
|
reveal: () => revealControl(element),
|
|
65
79
|
highlight: (durationMs) => highlightControl(element, durationMs),
|
|
66
|
-
|
|
80
|
+
validateValue: (next) => typeof next === 'boolean',
|
|
81
|
+
getState: () => ({ disabled: element.matches(':disabled') }),
|
|
67
82
|
};
|
|
68
83
|
});
|
|
69
84
|
</script>
|
|
70
85
|
<button bind:this={buttonEl} id={resolvedId} type="button" {name} {disabled} class="toggle-button {className}" class:pressed
|
|
71
86
|
aria-pressed={pressed} aria-label={ariaLabel} data-smrt-control={controlId} data-smrt-form={interactionContext?.formId}
|
|
87
|
+
data-smrt-subject-type={interaction === false ? undefined : interaction?.subject?.type}
|
|
88
|
+
data-smrt-subject-id={interaction === false ? undefined : interaction?.subject?.id}
|
|
72
89
|
onclick={handleClick} {...rest}>{@render children?.()}</button>
|
|
73
90
|
<style>
|
|
74
91
|
.toggle-button { display: inline-flex; align-items: center; justify-content: center; min-height: 2.5rem; padding: 0 var(--smrt-spacing-4); border: 1px solid var(--smrt-color-outline); border-radius: var(--smrt-radius-full); background: var(--smrt-color-surface); color: var(--smrt-color-on-surface); cursor: pointer; }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToggleButton.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/ToggleButton.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":"ToggleButton.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/forms/ToggleButton.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;AAS1E,MAAM,WAAW,KAAM,SAAQ,IAAI,CAAC,oBAAoB,EAAE,OAAO,CAAC;IAChE,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,yBAAyB,GAAG,KAAK,CAAC;IAChD,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AA6ED,QAAA,MAAM,YAAY,kDAAwC,CAAC;AAC3D,KAAK,YAAY,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC;AACpD,eAAe,YAAY,CAAC"}
|
|
@@ -10,10 +10,36 @@ import userEvent from '@testing-library/user-event';
|
|
|
10
10
|
import { createRawSnippet } from 'svelte';
|
|
11
11
|
import { describe, expect, it, vi } from 'vitest';
|
|
12
12
|
import { expectNoA11yViolations } from '../../../test-support/a11y';
|
|
13
|
+
import { createControlInteractionRegistry, executeLocalControlCommand, } from '../control-interaction.js';
|
|
13
14
|
import Form from '../Form.svelte';
|
|
14
15
|
const children = createRawSnippet(() => ({
|
|
15
16
|
render: () => '<button type="submit">Save</button>',
|
|
16
17
|
}));
|
|
18
|
+
const controlledInput = createRawSnippet(() => ({
|
|
19
|
+
render: () => '<input data-smrt-control="display-name" />',
|
|
20
|
+
}));
|
|
21
|
+
function dispatchTrusted(target, type) {
|
|
22
|
+
const event = new Event(type, { bubbles: true });
|
|
23
|
+
const implementationSymbol = Object.getOwnPropertySymbols(event).find((symbol) => symbol.description === 'impl');
|
|
24
|
+
if (!implementationSymbol)
|
|
25
|
+
throw new Error('missing JSDOM event internals');
|
|
26
|
+
target.addEventListener(type, (dispatched) => {
|
|
27
|
+
const implementation = dispatched[implementationSymbol];
|
|
28
|
+
implementation.isTrusted = true;
|
|
29
|
+
}, { capture: true, once: true });
|
|
30
|
+
target.dispatchEvent(event);
|
|
31
|
+
}
|
|
32
|
+
function dispatchLocalGesture(execute) {
|
|
33
|
+
const target = new EventTarget();
|
|
34
|
+
let pending;
|
|
35
|
+
target.addEventListener('click', (event) => {
|
|
36
|
+
pending = execute(event);
|
|
37
|
+
}, { once: true });
|
|
38
|
+
target.dispatchEvent(new Event('click'));
|
|
39
|
+
if (!pending)
|
|
40
|
+
throw new Error('local gesture handler did not run');
|
|
41
|
+
return pending;
|
|
42
|
+
}
|
|
17
43
|
describe('Form', () => {
|
|
18
44
|
it('renders a <form> with forwarded attributes and its children', () => {
|
|
19
45
|
const { container } = render(Form, {
|
|
@@ -40,6 +66,86 @@ describe('Form', () => {
|
|
|
40
66
|
expect(onsubmit).toHaveBeenCalledTimes(1);
|
|
41
67
|
expect(onsubmit.mock.calls[0][0].defaultPrevented).toBe(false);
|
|
42
68
|
});
|
|
69
|
+
it('records trusted value edits but not focus-only clicks', () => {
|
|
70
|
+
const registry = createControlInteractionRegistry();
|
|
71
|
+
const recordUserEdit = vi.spyOn(registry, 'recordUserEdit');
|
|
72
|
+
const { container } = render(Form, {
|
|
73
|
+
props: {
|
|
74
|
+
formId: 'profile',
|
|
75
|
+
interactionRegistry: registry,
|
|
76
|
+
children: controlledInput,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const input = container.querySelector('input');
|
|
80
|
+
if (!input)
|
|
81
|
+
throw new Error('Expected controlled input');
|
|
82
|
+
// A click can focus a field without changing it. Direct-edit tracking is
|
|
83
|
+
// deliberately limited to input/change, while composite controls record
|
|
84
|
+
// their successful mutations explicitly.
|
|
85
|
+
dispatchTrusted(input, 'click');
|
|
86
|
+
expect(recordUserEdit).not.toHaveBeenCalled();
|
|
87
|
+
dispatchTrusted(input, 'input');
|
|
88
|
+
expect(recordUserEdit).toHaveBeenCalledOnce();
|
|
89
|
+
expect(recordUserEdit).toHaveBeenCalledWith({
|
|
90
|
+
formId: 'profile',
|
|
91
|
+
controlId: 'display-name',
|
|
92
|
+
subject: undefined,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
it('preserves a consumer-normalized human edit while an async setter rolls back', async () => {
|
|
96
|
+
let releaseSetter;
|
|
97
|
+
let setterStarted;
|
|
98
|
+
const setterBlocked = new Promise((resolve) => {
|
|
99
|
+
releaseSetter = resolve;
|
|
100
|
+
});
|
|
101
|
+
const setterStartedPromise = new Promise((resolve) => {
|
|
102
|
+
setterStarted = resolve;
|
|
103
|
+
});
|
|
104
|
+
const registry = createControlInteractionRegistry({
|
|
105
|
+
isLocalGesture: () => true,
|
|
106
|
+
});
|
|
107
|
+
const { container } = render(Form, {
|
|
108
|
+
props: {
|
|
109
|
+
formId: 'profile',
|
|
110
|
+
interactionRegistry: registry,
|
|
111
|
+
children: controlledInput,
|
|
112
|
+
oninput: (event) => {
|
|
113
|
+
const input = event.target;
|
|
114
|
+
input.value = input.value.replace(/^./, (character) => character.toUpperCase());
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
const input = container.querySelector('input');
|
|
119
|
+
if (!input)
|
|
120
|
+
throw new Error('Expected controlled input');
|
|
121
|
+
const identity = { formId: 'profile', controlId: 'display-name' };
|
|
122
|
+
registry.register({
|
|
123
|
+
identity,
|
|
124
|
+
metadata: { kind: 'text' },
|
|
125
|
+
getValue: () => input.value,
|
|
126
|
+
setValue: async (next) => {
|
|
127
|
+
input.value = String(next);
|
|
128
|
+
setterStarted?.();
|
|
129
|
+
await setterBlocked;
|
|
130
|
+
throw new Error('setter_failed');
|
|
131
|
+
},
|
|
132
|
+
restoreValue: (next) => {
|
|
133
|
+
input.value = String(next);
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
await registry.execute({ action: 'stage', identity, value: 'Grace' }, { source: 'agent' });
|
|
137
|
+
const applying = dispatchLocalGesture((event) => executeLocalControlCommand(registry, { action: 'apply', identity, revision: 1 }, event));
|
|
138
|
+
await setterStartedPromise;
|
|
139
|
+
input.value = 'katherine';
|
|
140
|
+
dispatchTrusted(input, 'input');
|
|
141
|
+
expect(input.value).toBe('Katherine');
|
|
142
|
+
releaseSetter?.();
|
|
143
|
+
expect(await applying).toMatchObject({
|
|
144
|
+
ok: false,
|
|
145
|
+
reason: 'setter_failed',
|
|
146
|
+
});
|
|
147
|
+
expect(input.value).toBe('Katherine');
|
|
148
|
+
});
|
|
43
149
|
it('is axe-clean', async () => {
|
|
44
150
|
const { container } = render(Form, {
|
|
45
151
|
props: { 'aria-label': 'Profile form', children },
|