@destyler/switch 0.2.6-beta.1 → 0.2.7
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/dist/index.d.mts +10 -6
- package/dist/index.mjs +45 -33
- package/package.json +7 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
1
|
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
3
2
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
4
3
|
import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
|
|
5
4
|
|
|
6
5
|
//#region src/anatomy.d.ts
|
|
7
|
-
declare const anatomy:
|
|
6
|
+
declare const anatomy: any;
|
|
8
7
|
//#endregion
|
|
9
8
|
//#region src/types.d.ts
|
|
10
9
|
interface CheckedChangeDetails {
|
|
@@ -47,9 +46,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
|
|
|
47
46
|
*/
|
|
48
47
|
onCheckedChange?: ((details: CheckedChangeDetails) => void) | undefined;
|
|
49
48
|
/**
|
|
50
|
-
*
|
|
49
|
+
* The controlled checked state of the switch.
|
|
51
50
|
*/
|
|
52
|
-
checked
|
|
51
|
+
checked?: boolean | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* The initial checked state of the switch when it is not controlled.
|
|
54
|
+
* Prefer this over seeding with `checked` for uncontrolled usage.
|
|
55
|
+
*/
|
|
56
|
+
defaultChecked?: boolean | undefined;
|
|
53
57
|
/**
|
|
54
58
|
* The name of the input field in a switch
|
|
55
59
|
* (Useful for form submission).
|
|
@@ -141,7 +145,7 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
|
|
|
141
145
|
declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
|
|
142
146
|
//#endregion
|
|
143
147
|
//#region src/props.d.ts
|
|
144
|
-
declare const props: ("value" | "ids" | "label" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "dir" | "id" | "getRootNode")[];
|
|
145
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "label" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "name" | "form" | "dir" | "id" | "getRootNode">];
|
|
148
|
+
declare const props: ("value" | "ids" | "label" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "dir" | "id" | "getRootNode")[];
|
|
149
|
+
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "label" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "dir" | "id" | "getRootNode">];
|
|
146
150
|
//#endregion
|
|
147
151
|
export { type MachineApi as Api, type CheckedChangeDetails, type UserDefinedContext as Context, type ElementIds, type Service, anatomy, connect, machine, props, splitProps };
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createAnatomy } from "@destyler/anatomy";
|
|
2
2
|
import { createScope, dataAttr, dispatchInputCheckedEvent, getEventTarget, trackFormControl, trackPress, visuallyHiddenStyle } from "@destyler/dom";
|
|
3
3
|
import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
|
|
4
|
-
import { compact, createSplitProps, isEqual } from "@destyler/utils";
|
|
4
|
+
import { compact, createSplitProps, isControlled, isEqual, isPropUserProvided, resolveControllableProp, withControllableProvided } from "@destyler/utils";
|
|
5
5
|
import { createMachine, guards } from "@destyler/xstate";
|
|
6
6
|
import { createProps } from "@destyler/types";
|
|
7
7
|
|
|
@@ -161,28 +161,39 @@ function connect(state, send, normalize) {
|
|
|
161
161
|
//#endregion
|
|
162
162
|
//#region src/machine.ts
|
|
163
163
|
const { not } = guards;
|
|
164
|
-
const invoke = { change: (ctx) => {
|
|
165
|
-
ctx.onCheckedChange?.({ checked
|
|
164
|
+
const invoke = { change: (ctx, checked) => {
|
|
165
|
+
ctx.onCheckedChange?.({ checked });
|
|
166
166
|
} };
|
|
167
167
|
const set = { checked: (ctx, checked) => {
|
|
168
168
|
if (isEqual(ctx.checked, checked)) return;
|
|
169
|
+
if (isControlled(ctx, "checked")) {
|
|
170
|
+
invoke.change(ctx, checked);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
169
173
|
ctx.checked = checked;
|
|
170
|
-
invoke.change(ctx);
|
|
174
|
+
invoke.change(ctx, checked);
|
|
171
175
|
} };
|
|
172
176
|
function machine(userContext) {
|
|
177
|
+
const ctx = compact(withControllableProvided(userContext, ["checked"]));
|
|
178
|
+
const { initial: initialChecked } = resolveControllableProp({
|
|
179
|
+
value: ctx.checked,
|
|
180
|
+
defaultValue: ctx.defaultChecked,
|
|
181
|
+
valueProvided: isPropUserProvided(ctx, "checked"),
|
|
182
|
+
fallback: false
|
|
183
|
+
});
|
|
173
184
|
return createMachine({
|
|
174
185
|
id: "switch",
|
|
175
186
|
initial: "ready",
|
|
176
187
|
context: {
|
|
177
|
-
checked: false,
|
|
178
188
|
label: "switch",
|
|
179
189
|
value: "on",
|
|
180
190
|
disabled: false,
|
|
181
|
-
...
|
|
191
|
+
...ctx,
|
|
192
|
+
checked: initialChecked,
|
|
182
193
|
fieldsetDisabled: false,
|
|
183
194
|
focusVisible: false
|
|
184
195
|
},
|
|
185
|
-
computed: { isDisabled: (ctx) => ctx.disabled || ctx.fieldsetDisabled },
|
|
196
|
+
computed: { isDisabled: (ctx$1) => !!ctx$1.disabled || ctx$1.fieldsetDisabled },
|
|
186
197
|
watch: {
|
|
187
198
|
disabled: "removeFocusIfNeeded",
|
|
188
199
|
checked: "syncInputElement"
|
|
@@ -207,25 +218,25 @@ function machine(userContext) {
|
|
|
207
218
|
}, {
|
|
208
219
|
guards: { isTrusted: (_ctx, evt) => !!evt.isTrusted },
|
|
209
220
|
activities: {
|
|
210
|
-
trackPressEvent(ctx) {
|
|
211
|
-
if (ctx.isDisabled) return;
|
|
221
|
+
trackPressEvent(ctx$1) {
|
|
222
|
+
if (ctx$1.isDisabled) return;
|
|
212
223
|
return trackPress({
|
|
213
|
-
pointerNode: dom.getRootEl(ctx),
|
|
214
|
-
keyboardNode: dom.getHiddenInputEl(ctx),
|
|
224
|
+
pointerNode: dom.getRootEl(ctx$1),
|
|
225
|
+
keyboardNode: dom.getHiddenInputEl(ctx$1),
|
|
215
226
|
isValidKey: (event) => event.key === " ",
|
|
216
|
-
onPress: () => ctx.active = false,
|
|
217
|
-
onPressStart: () => ctx.active = true,
|
|
218
|
-
onPressEnd: () => ctx.active = false
|
|
227
|
+
onPress: () => ctx$1.active = false,
|
|
228
|
+
onPressStart: () => ctx$1.active = true,
|
|
229
|
+
onPressEnd: () => ctx$1.active = false
|
|
219
230
|
});
|
|
220
231
|
},
|
|
221
|
-
trackFocusVisible(ctx) {
|
|
222
|
-
if (ctx.isDisabled) return;
|
|
223
|
-
return trackFocusVisible({ root: dom.getRootNode(ctx) });
|
|
232
|
+
trackFocusVisible(ctx$1) {
|
|
233
|
+
if (ctx$1.isDisabled) return;
|
|
234
|
+
return trackFocusVisible({ root: dom.getRootNode(ctx$1) });
|
|
224
235
|
},
|
|
225
|
-
trackFormControlState(ctx, _evt, { send, initialContext }) {
|
|
226
|
-
return trackFormControl(dom.getHiddenInputEl(ctx), {
|
|
236
|
+
trackFormControlState(ctx$1, _evt, { send, initialContext }) {
|
|
237
|
+
return trackFormControl(dom.getHiddenInputEl(ctx$1), {
|
|
227
238
|
onFieldsetDisabledChange(disabled) {
|
|
228
|
-
ctx.fieldsetDisabled = disabled;
|
|
239
|
+
ctx$1.fieldsetDisabled = disabled;
|
|
229
240
|
},
|
|
230
241
|
onFormReset() {
|
|
231
242
|
send({
|
|
@@ -238,25 +249,25 @@ function machine(userContext) {
|
|
|
238
249
|
}
|
|
239
250
|
},
|
|
240
251
|
actions: {
|
|
241
|
-
setContext(ctx, evt) {
|
|
242
|
-
Object.assign(ctx, evt.context);
|
|
252
|
+
setContext(ctx$1, evt) {
|
|
253
|
+
Object.assign(ctx$1, evt.context);
|
|
243
254
|
},
|
|
244
|
-
syncInputElement(ctx) {
|
|
245
|
-
const inputEl = dom.getHiddenInputEl(ctx);
|
|
255
|
+
syncInputElement(ctx$1) {
|
|
256
|
+
const inputEl = dom.getHiddenInputEl(ctx$1);
|
|
246
257
|
if (!inputEl) return;
|
|
247
|
-
inputEl.checked = !!ctx.checked;
|
|
258
|
+
inputEl.checked = !!ctx$1.checked;
|
|
248
259
|
},
|
|
249
|
-
removeFocusIfNeeded(ctx) {
|
|
250
|
-
if (ctx.disabled && ctx.focused) ctx.focused = false;
|
|
260
|
+
removeFocusIfNeeded(ctx$1) {
|
|
261
|
+
if (ctx$1.disabled && ctx$1.focused) ctx$1.focused = false;
|
|
251
262
|
},
|
|
252
|
-
setChecked(ctx, evt) {
|
|
253
|
-
set.checked(ctx, evt.checked);
|
|
263
|
+
setChecked(ctx$1, evt) {
|
|
264
|
+
set.checked(ctx$1, evt.checked);
|
|
254
265
|
},
|
|
255
|
-
toggleChecked(ctx, _evt) {
|
|
256
|
-
set.checked(ctx, !ctx.checked);
|
|
266
|
+
toggleChecked(ctx$1, _evt) {
|
|
267
|
+
set.checked(ctx$1, !ctx$1.checked);
|
|
257
268
|
},
|
|
258
|
-
dispatchChangeEvent(ctx) {
|
|
259
|
-
dispatchInputCheckedEvent(dom.getHiddenInputEl(ctx), { checked: ctx.checked });
|
|
269
|
+
dispatchChangeEvent(ctx$1) {
|
|
270
|
+
dispatchInputCheckedEvent(dom.getHiddenInputEl(ctx$1), { checked: !!ctx$1.checked });
|
|
260
271
|
}
|
|
261
272
|
}
|
|
262
273
|
});
|
|
@@ -266,6 +277,7 @@ function machine(userContext) {
|
|
|
266
277
|
//#region src/props.ts
|
|
267
278
|
const props = createProps()([
|
|
268
279
|
"checked",
|
|
280
|
+
"defaultChecked",
|
|
269
281
|
"dir",
|
|
270
282
|
"disabled",
|
|
271
283
|
"form",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@destyler/switch",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.7",
|
|
5
5
|
"description": "A control that allows the user to toggle between checked and not checked.",
|
|
6
6
|
"author": "Elone Hoo <elonehoo@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@destyler/anatomy": "0.2.
|
|
26
|
-
"@destyler/
|
|
27
|
-
"@destyler/
|
|
28
|
-
"@destyler/
|
|
29
|
-
"@destyler/
|
|
30
|
-
"@destyler/xstate": "0.2.
|
|
25
|
+
"@destyler/anatomy": "0.2.7",
|
|
26
|
+
"@destyler/dom": "0.2.7",
|
|
27
|
+
"@destyler/focus-visible": "0.2.7",
|
|
28
|
+
"@destyler/utils": "0.2.7",
|
|
29
|
+
"@destyler/types": "0.2.7",
|
|
30
|
+
"@destyler/xstate": "0.2.7"
|
|
31
31
|
},
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|