@destyler/switch 0.2.11 → 0.2.12

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 CHANGED
@@ -1,10 +1,7 @@
1
- import * as _destyler_anatomy0 from "@destyler/anatomy";
2
- import * as _destyler_xstate0 from "@destyler/xstate";
3
1
  import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
4
2
  import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
5
-
6
3
  //#region src/anatomy.d.ts
7
- declare const anatomy: _destyler_anatomy0.AnatomyInstance<"root" | "label" | "control" | "thumb">;
4
+ export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"root" | "label" | "control" | "thumb">;
8
5
  //#endregion
9
6
  //#region src/types.d.ts
10
7
  interface CheckedChangeDetails {
@@ -140,13 +137,13 @@ interface MachineApi<T extends PropTypes = PropTypes> {
140
137
  }
141
138
  //#endregion
142
139
  //#region src/connect.d.ts
143
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
140
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
144
141
  //#endregion
145
142
  //#region src/machine.d.ts
146
- declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
143
+ export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
147
144
  //#endregion
148
145
  //#region src/props.d.ts
149
- declare const props: ("label" | "id" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "value" | "dir" | "getRootNode")[];
150
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "label" | "id" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "value" | "dir" | "getRootNode">];
146
+ export declare const props: ("label" | "value" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "dir" | "id" | "getRootNode")[];
147
+ export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "label" | "value" | "ids" | "disabled" | "invalid" | "required" | "readOnly" | "onCheckedChange" | "checked" | "defaultChecked" | "name" | "form" | "dir" | "id" | "getRootNode">];
151
148
  //#endregion
152
- export { type MachineApi as Api, type CheckedChangeDetails, type UserDefinedContext as Context, type ElementIds, type Service, anatomy, connect, machine, props, splitProps };
149
+ export type { MachineApi as Api, CheckedChangeDetails, UserDefinedContext as Context, ElementIds, Service };
package/dist/index.mjs CHANGED
@@ -1,14 +1,12 @@
1
1
  import { createAnatomy } from "@destyler/anatomy";
2
- import { createScope, dataAttr, dispatchInputCheckedEvent, getEventTarget, trackFormControl, trackPress, visuallyHiddenStyle } from "@destyler/dom";
2
+ import { createScope, dataAttr, dispatchInputCheckedEvent, getEventTarget, trackCanceledInputClick, trackFormControl, trackPress, visuallyHiddenStyle } from "@destyler/dom";
3
3
  import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
4
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
-
8
7
  //#region src/anatomy.ts
9
8
  const anatomy = createAnatomy("switch").parts("root", "label", "control", "thumb");
10
9
  const parts = anatomy.build();
11
-
12
10
  //#endregion
13
11
  //#region src/dom.ts
14
12
  const dom = createScope({
@@ -20,7 +18,6 @@ const dom = createScope({
20
18
  getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
21
19
  getHiddenInputEl: (ctx) => dom.getById(ctx, dom.getHiddenInputId(ctx))
22
20
  });
23
-
24
21
  //#endregion
25
22
  //#region src/connect.ts
26
23
  function connect(state, send, normalize) {
@@ -43,10 +40,10 @@ function connect(state, send, normalize) {
43
40
  checked,
44
41
  disabled,
45
42
  focused,
46
- setChecked(checked$1) {
43
+ setChecked(checked) {
47
44
  send({
48
45
  type: "CHECKED.SET",
49
- checked: checked$1,
46
+ checked,
50
47
  isTrusted: false
51
48
  });
52
49
  },
@@ -146,10 +143,10 @@ function connect(state, send, normalize) {
146
143
  event.preventDefault();
147
144
  return;
148
145
  }
149
- const checked$1 = event.currentTarget.checked;
146
+ const checked = event.currentTarget.checked;
150
147
  send({
151
148
  type: "CHECKED.SET",
152
- checked: checked$1,
149
+ checked,
153
150
  isTrusted: true
154
151
  });
155
152
  }
@@ -157,7 +154,6 @@ function connect(state, send, normalize) {
157
154
  }
158
155
  };
159
156
  }
160
-
161
157
  //#endregion
162
158
  //#region src/machine.ts
163
159
  const { not } = guards;
@@ -193,12 +189,13 @@ function machine(userContext) {
193
189
  fieldsetDisabled: false,
194
190
  focusVisible: false
195
191
  },
196
- computed: { isDisabled: (ctx$1) => !!ctx$1.disabled || ctx$1.fieldsetDisabled },
192
+ computed: { isDisabled: (ctx) => !!ctx.disabled || ctx.fieldsetDisabled },
197
193
  watch: {
198
194
  disabled: "removeFocusIfNeeded",
199
195
  checked: "syncInputElement"
200
196
  },
201
197
  activities: [
198
+ "trackCanceledInputClick",
202
199
  "trackFormControlState",
203
200
  "trackPressEvent",
204
201
  "trackFocusVisible"
@@ -211,32 +208,38 @@ function machine(userContext) {
211
208
  "CHECKED.SET": [{
212
209
  guard: not("isTrusted"),
213
210
  actions: ["setChecked", "dispatchChangeEvent"]
214
- }, { actions: ["setChecked"] }],
211
+ }, { actions: ["setChecked", "syncInputElement"] }],
215
212
  "CONTEXT.SET": { actions: ["setContext"] }
216
213
  },
217
214
  states: { ready: {} }
218
215
  }, {
219
216
  guards: { isTrusted: (_ctx, evt) => !!evt.isTrusted },
220
217
  activities: {
221
- trackPressEvent(ctx$1) {
222
- if (ctx$1.isDisabled) return;
218
+ trackCanceledInputClick(ctx) {
219
+ const input = dom.getHiddenInputEl(ctx);
220
+ return trackCanceledInputClick(input, (target) => {
221
+ if (target === input) target.checked = !!ctx.checked;
222
+ });
223
+ },
224
+ trackPressEvent(ctx) {
225
+ if (ctx.isDisabled) return;
223
226
  return trackPress({
224
- pointerNode: dom.getRootEl(ctx$1),
225
- keyboardNode: dom.getHiddenInputEl(ctx$1),
227
+ pointerNode: dom.getRootEl(ctx),
228
+ keyboardNode: dom.getHiddenInputEl(ctx),
226
229
  isValidKey: (event) => event.key === " ",
227
- onPress: () => ctx$1.active = false,
228
- onPressStart: () => ctx$1.active = true,
229
- onPressEnd: () => ctx$1.active = false
230
+ onPress: () => ctx.active = false,
231
+ onPressStart: () => ctx.active = true,
232
+ onPressEnd: () => ctx.active = false
230
233
  });
231
234
  },
232
- trackFocusVisible(ctx$1) {
233
- if (ctx$1.isDisabled) return;
234
- return trackFocusVisible({ root: dom.getRootNode(ctx$1) });
235
+ trackFocusVisible(ctx) {
236
+ if (ctx.isDisabled) return;
237
+ return trackFocusVisible({ root: dom.getRootNode(ctx) });
235
238
  },
236
- trackFormControlState(ctx$1, _evt, { send, initialContext }) {
237
- return trackFormControl(dom.getHiddenInputEl(ctx$1), {
239
+ trackFormControlState(ctx, _evt, { send, initialContext }) {
240
+ return trackFormControl(dom.getHiddenInputEl(ctx), {
238
241
  onFieldsetDisabledChange(disabled) {
239
- ctx$1.fieldsetDisabled = disabled;
242
+ ctx.fieldsetDisabled = disabled;
240
243
  },
241
244
  onFormReset() {
242
245
  send({
@@ -249,30 +252,30 @@ function machine(userContext) {
249
252
  }
250
253
  },
251
254
  actions: {
252
- setContext(ctx$1, evt) {
253
- Object.assign(ctx$1, evt.context);
255
+ setContext(ctx, evt) {
256
+ Object.assign(ctx, evt.context);
254
257
  },
255
- syncInputElement(ctx$1) {
256
- const inputEl = dom.getHiddenInputEl(ctx$1);
258
+ syncInputElement(ctx) {
259
+ const inputEl = dom.getHiddenInputEl(ctx);
257
260
  if (!inputEl) return;
258
- inputEl.checked = !!ctx$1.checked;
261
+ inputEl.checked = !!ctx.checked;
259
262
  },
260
- removeFocusIfNeeded(ctx$1) {
261
- if (ctx$1.disabled && ctx$1.focused) ctx$1.focused = false;
263
+ removeFocusIfNeeded(ctx) {
264
+ if (ctx.disabled && ctx.focused) ctx.focused = false;
262
265
  },
263
- setChecked(ctx$1, evt) {
264
- set.checked(ctx$1, evt.checked);
266
+ setChecked(ctx, evt) {
267
+ set.checked(ctx, evt.checked);
265
268
  },
266
- toggleChecked(ctx$1, _evt) {
267
- set.checked(ctx$1, !ctx$1.checked);
269
+ toggleChecked(ctx, _evt) {
270
+ set.checked(ctx, !ctx.checked);
268
271
  },
269
- dispatchChangeEvent(ctx$1) {
270
- dispatchInputCheckedEvent(dom.getHiddenInputEl(ctx$1), { checked: !!ctx$1.checked });
272
+ dispatchChangeEvent(ctx) {
273
+ const inputEl = dom.getHiddenInputEl(ctx);
274
+ dispatchInputCheckedEvent(inputEl, { checked: !!ctx.checked });
271
275
  }
272
276
  }
273
277
  });
274
278
  }
275
-
276
279
  //#endregion
277
280
  //#region src/props.ts
278
281
  const props = createProps()([
@@ -293,6 +296,5 @@ const props = createProps()([
293
296
  "value"
294
297
  ]);
295
298
  const splitProps = createSplitProps(props);
296
-
297
299
  //#endregion
298
- export { anatomy, connect, machine, props, splitProps };
300
+ export { anatomy, connect, machine, props, splitProps };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/switch",
3
3
  "type": "module",
4
- "version": "0.2.11",
4
+ "version": "0.2.12",
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.11",
26
- "@destyler/focus-visible": "0.2.11",
27
- "@destyler/types": "0.2.11",
28
- "@destyler/dom": "0.2.11",
29
- "@destyler/utils": "0.2.11",
30
- "@destyler/xstate": "0.2.11"
25
+ "@destyler/anatomy": "0.2.12",
26
+ "@destyler/dom": "0.2.12",
27
+ "@destyler/focus-visible": "0.2.12",
28
+ "@destyler/types": "0.2.12",
29
+ "@destyler/utils": "0.2.12",
30
+ "@destyler/xstate": "0.2.12"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"