@destyler/checkbox 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 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: _destyler_anatomy0.AnatomyInstance<"root" | "control" | "label" | "indicator">;
6
+ declare const anatomy: any;
8
7
  //#endregion
9
8
  //#region src/types.d.ts
10
9
  type CheckedState = boolean | 'indeterminate';
@@ -35,9 +34,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
35
34
  */
36
35
  required?: boolean | undefined;
37
36
  /**
38
- * The checked state of the checkbox
37
+ * The controlled checked state of the checkbox
39
38
  */
40
- checked: CheckedState;
39
+ checked?: CheckedState | undefined;
40
+ /**
41
+ * The initial checked state of the checkbox when it is not controlled.
42
+ * Prefer this over seeding with `checked` for uncontrolled usage.
43
+ */
44
+ defaultChecked?: CheckedState | undefined;
41
45
  /**
42
46
  * Whether the checkbox is read-only
43
47
  */
@@ -153,7 +157,7 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
153
157
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
154
158
  //#endregion
155
159
  //#region src/props.d.ts
156
- declare const props: ("value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode")[];
157
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode">];
160
+ declare const props: ("value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode")[];
161
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "invalid" | "required" | "checked" | "defaultChecked" | "readOnly" | "onCheckedChange" | "name" | "form" | "dir" | "id" | "getRootNode">];
158
162
  //#endregion
159
163
  export { type MachineApi as Api, type CheckedChangeDetails, type CheckedState, type UserDefinedContext as Context, type ElementIds, type MachineState, type Service, type State, 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, setElementChecked, 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
 
@@ -167,23 +167,34 @@ function isIndeterminate(checked) {
167
167
  function isChecked(checked) {
168
168
  return isIndeterminate(checked) ? false : !!checked;
169
169
  }
170
- const invoke = { change: (ctx) => {
171
- ctx.onCheckedChange?.({ checked: ctx.checked });
170
+ const invoke = { change: (ctx, checked) => {
171
+ ctx.onCheckedChange?.({ checked });
172
172
  } };
173
173
  const set = { checked: (ctx, checked) => {
174
174
  if (isEqual(ctx.checked, checked)) return;
175
+ if (isControlled(ctx, "checked")) {
176
+ invoke.change(ctx, checked);
177
+ return;
178
+ }
175
179
  ctx.checked = checked;
176
- invoke.change(ctx);
180
+ invoke.change(ctx, checked);
177
181
  } };
178
182
  function machine(userContext) {
183
+ const ctx = compact(withControllableProvided(userContext, ["checked"]));
184
+ const { initial: initialChecked } = resolveControllableProp({
185
+ value: ctx.checked,
186
+ defaultValue: ctx.defaultChecked,
187
+ valueProvided: isPropUserProvided(ctx, "checked"),
188
+ fallback: false
189
+ });
179
190
  return createMachine({
180
191
  id: "checkbox",
181
192
  initial: "ready",
182
193
  context: {
183
- checked: false,
184
194
  value: "on",
185
195
  disabled: false,
186
- ...compact(userContext),
196
+ ...ctx,
197
+ checked: initialChecked,
187
198
  fieldsetDisabled: false,
188
199
  focusVisible: false
189
200
  },
@@ -208,33 +219,33 @@ function machine(userContext) {
208
219
  "CONTEXT.SET": { actions: ["setContext"] }
209
220
  },
210
221
  computed: {
211
- isIndeterminate: (ctx) => isIndeterminate(ctx.checked),
212
- isChecked: (ctx) => isChecked(ctx.checked),
213
- isDisabled: (ctx) => !!ctx.disabled || ctx.fieldsetDisabled
222
+ isIndeterminate: (ctx$1) => isIndeterminate(ctx$1.checked),
223
+ isChecked: (ctx$1) => isChecked(ctx$1.checked),
224
+ isDisabled: (ctx$1) => !!ctx$1.disabled || ctx$1.fieldsetDisabled
214
225
  },
215
226
  states: { ready: {} }
216
227
  }, {
217
228
  guards: { isTrusted: (_ctx, evt) => !!evt.isTrusted },
218
229
  activities: {
219
- trackPressEvent(ctx) {
220
- if (ctx.isDisabled) return;
230
+ trackPressEvent(ctx$1) {
231
+ if (ctx$1.isDisabled) return;
221
232
  return trackPress({
222
- pointerNode: dom.getRootEl(ctx),
223
- keyboardNode: dom.getHiddenInputEl(ctx),
233
+ pointerNode: dom.getRootEl(ctx$1),
234
+ keyboardNode: dom.getHiddenInputEl(ctx$1),
224
235
  isValidKey: (event) => event.key === " ",
225
- onPress: () => ctx.active = false,
226
- onPressStart: () => ctx.active = true,
227
- onPressEnd: () => ctx.active = false
236
+ onPress: () => ctx$1.active = false,
237
+ onPressStart: () => ctx$1.active = true,
238
+ onPressEnd: () => ctx$1.active = false
228
239
  });
229
240
  },
230
- trackFocusVisible(ctx) {
231
- if (ctx.isDisabled) return;
232
- return trackFocusVisible({ root: dom.getRootNode(ctx) });
241
+ trackFocusVisible(ctx$1) {
242
+ if (ctx$1.isDisabled) return;
243
+ return trackFocusVisible({ root: dom.getRootNode(ctx$1) });
233
244
  },
234
- trackFormControlState(ctx, _evt, { send, initialContext }) {
235
- return trackFormControl(dom.getHiddenInputEl(ctx), {
245
+ trackFormControlState(ctx$1, _evt, { send, initialContext }) {
246
+ return trackFormControl(dom.getHiddenInputEl(ctx$1), {
236
247
  onFieldsetDisabledChange(disabled) {
237
- ctx.fieldsetDisabled = disabled;
248
+ ctx$1.fieldsetDisabled = disabled;
238
249
  },
239
250
  onFormReset() {
240
251
  send({
@@ -246,30 +257,30 @@ function machine(userContext) {
246
257
  }
247
258
  },
248
259
  actions: {
249
- setContext(ctx, evt) {
250
- Object.assign(ctx, evt.context);
260
+ setContext(ctx$1, evt) {
261
+ Object.assign(ctx$1, evt.context);
251
262
  },
252
- syncInputElement(ctx) {
253
- const inputEl = dom.getHiddenInputEl(ctx);
263
+ syncInputElement(ctx$1) {
264
+ const inputEl = dom.getHiddenInputEl(ctx$1);
254
265
  if (!inputEl) return;
255
- setElementChecked(inputEl, ctx.isChecked);
256
- inputEl.indeterminate = ctx.isIndeterminate;
266
+ setElementChecked(inputEl, ctx$1.isChecked);
267
+ inputEl.indeterminate = ctx$1.isIndeterminate;
257
268
  },
258
- removeFocusIfNeeded(ctx) {
259
- if (ctx.disabled && ctx.focused) {
260
- ctx.focused = false;
261
- ctx.focusVisible = false;
269
+ removeFocusIfNeeded(ctx$1) {
270
+ if (ctx$1.disabled && ctx$1.focused) {
271
+ ctx$1.focused = false;
272
+ ctx$1.focusVisible = false;
262
273
  }
263
274
  },
264
- setChecked(ctx, evt) {
265
- set.checked(ctx, evt.checked);
275
+ setChecked(ctx$1, evt) {
276
+ set.checked(ctx$1, evt.checked);
266
277
  },
267
- toggleChecked(ctx) {
268
- const checked = isIndeterminate(ctx.checked) ? true : !ctx.checked;
269
- set.checked(ctx, checked);
278
+ toggleChecked(ctx$1) {
279
+ const checked = isIndeterminate(ctx$1.checked) ? true : !ctx$1.checked;
280
+ set.checked(ctx$1, checked);
270
281
  },
271
- dispatchChangeEvent(ctx) {
272
- dispatchInputCheckedEvent(dom.getHiddenInputEl(ctx), { checked: isChecked(ctx.checked) });
282
+ dispatchChangeEvent(ctx$1) {
283
+ dispatchInputCheckedEvent(dom.getHiddenInputEl(ctx$1), { checked: isChecked(ctx$1.checked) });
273
284
  }
274
285
  }
275
286
  });
@@ -279,6 +290,7 @@ function machine(userContext) {
279
290
  //#region src/props.ts
280
291
  const props = createProps()([
281
292
  "checked",
293
+ "defaultChecked",
282
294
  "dir",
283
295
  "disabled",
284
296
  "form",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/checkbox",
3
3
  "type": "module",
4
- "version": "0.2.6-beta.1",
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.6-beta.1",
26
- "@destyler/dom": "0.2.6-beta.1",
27
- "@destyler/types": "0.2.6-beta.1",
28
- "@destyler/focus-visible": "0.2.6-beta.1",
29
- "@destyler/utils": "0.2.6-beta.1",
30
- "@destyler/xstate": "0.2.6-beta.1"
25
+ "@destyler/anatomy": "0.2.7",
26
+ "@destyler/dom": "0.2.7",
27
+ "@destyler/focus-visible": "0.2.7",
28
+ "@destyler/types": "0.2.7",
29
+ "@destyler/xstate": "0.2.7",
30
+ "@destyler/utils": "0.2.7"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"