@destyler/radio 0.2.6 → 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" | "label" | "indicator" | "item" | "itemControl" | "itemText">;
6
+ declare const anatomy: any;
8
7
  //#endregion
9
8
  //#region src/types.d.ts
10
9
  interface ValueChangeDetails {
@@ -25,9 +24,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
25
24
  */
26
25
  ids?: ElementIds | undefined;
27
26
  /**
28
- * The value of the checked radio
27
+ * The controlled value of the checked radio
29
28
  */
30
- value: string | null;
29
+ value?: string | null | undefined;
30
+ /**
31
+ * The initial value of the radio group when it is not controlled.
32
+ * Prefer this over seeding with `value` for uncontrolled usage.
33
+ */
34
+ defaultValue?: string | null | undefined;
31
35
  /**
32
36
  * The name of the input fields in the radio
33
37
  * (Useful for form submission).
@@ -190,8 +194,8 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
190
194
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
191
195
  //#endregion
192
196
  //#region src/props.d.ts
193
- declare const props: ("value" | "ids" | "name" | "form" | "disabled" | "readOnly" | "onValueChange" | "orientation" | "dir" | "id" | "getRootNode")[];
194
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "name" | "form" | "disabled" | "readOnly" | "onValueChange" | "orientation" | "dir" | "id" | "getRootNode">];
197
+ declare const props: ("value" | "ids" | "defaultValue" | "name" | "form" | "disabled" | "readOnly" | "onValueChange" | "orientation" | "dir" | "id" | "getRootNode")[];
198
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "defaultValue" | "name" | "form" | "disabled" | "readOnly" | "onValueChange" | "orientation" | "dir" | "id" | "getRootNode">];
195
199
  declare const itemProps: (keyof ItemProps)[];
196
200
  declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
197
201
  //#endregion
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ import { createAnatomy } from "@destyler/anatomy";
2
2
  import { createScope, dataAttr, dispatchInputCheckedEvent, nextTick, queryAll, trackFormControl, visuallyHiddenStyle } from "@destyler/dom";
3
3
  import { isFocusVisible, trackFocusVisible } from "@destyler/focus-visible";
4
4
  import { trackElementRect } from "@destyler/element-rect";
5
- import { compact, createSplitProps, isEqual, isString } from "@destyler/utils";
5
+ import { compact, createSplitProps, isControlled, isEqual, isPropUserProvided, isString, resolveControllableProp, withControllableProvided } from "@destyler/utils";
6
6
  import { createMachine, guards } from "@destyler/xstate";
7
7
  import { createProps } from "@destyler/types";
8
8
 
@@ -277,34 +277,45 @@ function connect(state, send, normalize) {
277
277
  //#endregion
278
278
  //#region src/machine.ts
279
279
  const { not } = guards;
280
- const invoke = { change: (ctx) => {
281
- if (ctx.value == null) return;
282
- ctx.onValueChange?.({ value: ctx.value });
280
+ const invoke = { change: (ctx, value) => {
281
+ if (value == null) return;
282
+ ctx.onValueChange?.({ value });
283
283
  } };
284
284
  const set = { value: (ctx, value) => {
285
285
  if (isEqual(ctx.value, value)) return;
286
+ if (isControlled(ctx, "value")) {
287
+ invoke.change(ctx, value);
288
+ return;
289
+ }
286
290
  ctx.value = value;
287
- invoke.change(ctx);
291
+ invoke.change(ctx, value);
288
292
  } };
289
293
  function machine(userContext) {
294
+ const ctx = compact(withControllableProvided(userContext, ["value"]));
295
+ const { initial: initialValue } = resolveControllableProp({
296
+ value: ctx.value,
297
+ defaultValue: ctx.defaultValue,
298
+ valueProvided: isPropUserProvided(ctx, "value"),
299
+ fallback: null
300
+ });
290
301
  return createMachine({
291
302
  id: "radio",
292
303
  initial: "idle",
293
304
  context: {
294
- value: null,
295
305
  activeValue: null,
296
306
  focusedValue: null,
297
307
  hoveredValue: null,
298
308
  disabled: false,
299
309
  orientation: "vertical",
300
- ...compact(userContext),
310
+ ...ctx,
311
+ value: initialValue,
301
312
  indicatorRect: {},
302
313
  canIndicatorTransition: false,
303
314
  fieldsetDisabled: false,
304
315
  focusVisible: false,
305
316
  ssr: true
306
317
  },
307
- computed: { isDisabled: (ctx) => !!ctx.disabled || ctx.fieldsetDisabled },
318
+ computed: { isDisabled: (ctx$1) => !!ctx$1.disabled || ctx$1.fieldsetDisabled },
308
319
  entry: ["syncIndicatorRect", "syncSsr"],
309
320
  exit: ["cleanupObserver"],
310
321
  activities: ["trackFormControlState", "trackFocusVisible"],
@@ -326,10 +337,10 @@ function machine(userContext) {
326
337
  }, {
327
338
  guards: { isTrusted: (_ctx, evt) => !!evt.isTrusted },
328
339
  activities: {
329
- trackFormControlState(ctx, _evt, { send, initialContext }) {
330
- return trackFormControl(dom.getRootEl(ctx), {
340
+ trackFormControlState(ctx$1, _evt, { send, initialContext }) {
341
+ return trackFormControl(dom.getRootEl(ctx$1), {
331
342
  onFieldsetDisabledChange(disabled) {
332
- ctx.fieldsetDisabled = disabled;
343
+ ctx$1.fieldsetDisabled = disabled;
333
344
  },
334
345
  onFormReset() {
335
346
  send({
@@ -339,62 +350,62 @@ function machine(userContext) {
339
350
  }
340
351
  });
341
352
  },
342
- trackFocusVisible(ctx) {
343
- return trackFocusVisible({ root: dom.getRootNode(ctx) });
353
+ trackFocusVisible(ctx$1) {
354
+ return trackFocusVisible({ root: dom.getRootNode(ctx$1) });
344
355
  }
345
356
  },
346
357
  actions: {
347
- setValue(ctx, evt) {
348
- set.value(ctx, evt.value);
358
+ setValue(ctx$1, evt) {
359
+ set.value(ctx$1, evt.value);
349
360
  },
350
- setHovered(ctx, evt) {
351
- ctx.hoveredValue = evt.value;
361
+ setHovered(ctx$1, evt) {
362
+ ctx$1.hoveredValue = evt.value;
352
363
  },
353
- setActive(ctx, evt) {
354
- ctx.activeValue = evt.value;
364
+ setActive(ctx$1, evt) {
365
+ ctx$1.activeValue = evt.value;
355
366
  },
356
- setFocused(ctx, evt) {
357
- ctx.focusedValue = evt.value;
358
- ctx.focusVisible = evt.focusVisible;
367
+ setFocused(ctx$1, evt) {
368
+ ctx$1.focusedValue = evt.value;
369
+ ctx$1.focusVisible = evt.focusVisible;
359
370
  },
360
- syncInputElements(ctx) {
361
- dom.getInputEls(ctx).forEach((input) => {
362
- input.checked = input.value === ctx.value;
371
+ syncInputElements(ctx$1) {
372
+ dom.getInputEls(ctx$1).forEach((input) => {
373
+ input.checked = input.value === ctx$1.value;
363
374
  });
364
375
  },
365
- setIndicatorTransition(ctx) {
366
- ctx.canIndicatorTransition = isString(ctx.value);
376
+ setIndicatorTransition(ctx$1) {
377
+ ctx$1.canIndicatorTransition = isString(ctx$1.value);
367
378
  },
368
- cleanupObserver(ctx) {
369
- ctx.indicatorCleanup?.();
379
+ cleanupObserver(ctx$1) {
380
+ ctx$1.indicatorCleanup?.();
370
381
  },
371
- syncSsr(ctx) {
372
- ctx.ssr = false;
382
+ syncSsr(ctx$1) {
383
+ ctx$1.ssr = false;
373
384
  },
374
- syncIndicatorRect(ctx) {
375
- ctx.indicatorCleanup?.();
376
- if (!dom.getIndicatorEl(ctx)) return;
377
- const value = ctx.value;
378
- const radioEl = dom.getActiveRadioEl(ctx);
385
+ syncIndicatorRect(ctx$1) {
386
+ ctx$1.indicatorCleanup?.();
387
+ if (!dom.getIndicatorEl(ctx$1)) return;
388
+ const value = ctx$1.value;
389
+ const radioEl = dom.getActiveRadioEl(ctx$1);
379
390
  if (value == null || !radioEl) {
380
- ctx.indicatorRect = {};
391
+ ctx$1.indicatorRect = {};
381
392
  return;
382
393
  }
383
- ctx.indicatorCleanup = trackElementRect(radioEl, {
394
+ ctx$1.indicatorCleanup = trackElementRect(radioEl, {
384
395
  getRect(el) {
385
396
  return dom.getOffsetRect(el);
386
397
  },
387
398
  onChange(rect) {
388
- ctx.indicatorRect = dom.resolveRect(rect);
399
+ ctx$1.indicatorRect = dom.resolveRect(rect);
389
400
  nextTick(() => {
390
- ctx.canIndicatorTransition = false;
401
+ ctx$1.canIndicatorTransition = false;
391
402
  });
392
403
  }
393
404
  });
394
405
  },
395
- dispatchChangeEvent(ctx) {
396
- dom.getInputEls(ctx).forEach((inputEl) => {
397
- const checked = inputEl.value === ctx.value;
406
+ dispatchChangeEvent(ctx$1) {
407
+ dom.getInputEls(ctx$1).forEach((inputEl) => {
408
+ const checked = inputEl.value === ctx$1.value;
398
409
  if (checked === inputEl.checked) return;
399
410
  dispatchInputCheckedEvent(inputEl, { checked });
400
411
  });
@@ -406,6 +417,7 @@ function machine(userContext) {
406
417
  //#endregion
407
418
  //#region src/props.ts
408
419
  const props = createProps()([
420
+ "defaultValue",
409
421
  "dir",
410
422
  "disabled",
411
423
  "form",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/radio",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.7",
5
5
  "description": "A set of checkable buttons—known as radio buttons—where no more than one of the buttons can be checked at a time.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
@@ -22,13 +22,13 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@destyler/anatomy": "0.2.6",
26
- "@destyler/dom": "0.2.6",
27
- "@destyler/element-rect": "0.2.6",
28
- "@destyler/focus-visible": "0.2.6",
29
- "@destyler/types": "0.2.6",
30
- "@destyler/utils": "0.2.6",
31
- "@destyler/xstate": "0.2.6"
25
+ "@destyler/dom": "0.2.7",
26
+ "@destyler/anatomy": "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
+ "@destyler/element-rect": "0.2.7"
32
32
  },
33
33
  "publishConfig": {
34
34
  "access": "public"