@destyler/edit 0.2.6 → 0.2.9

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
@@ -37,23 +37,23 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
37
37
  /**
38
38
  * The ids of the elements in the editable. Useful for composition.
39
39
  */
40
- 'ids'?: ElementIds | undefined;
40
+ ids?: ElementIds | undefined;
41
41
  /**
42
42
  * Whether the input's value is invalid.
43
43
  */
44
- 'invalid'?: boolean | undefined;
44
+ invalid?: boolean | undefined;
45
45
  /**
46
46
  * The name attribute of the editable component. Used for form submission.
47
47
  */
48
- 'name'?: string | undefined;
48
+ name?: string | undefined;
49
49
  /**
50
50
  * The associate form of the underlying input.
51
51
  */
52
- 'form'?: string | undefined;
52
+ form?: string | undefined;
53
53
  /**
54
54
  * Whether the editable should auto-resize to fit the content.
55
55
  */
56
- 'autoResize'?: boolean | undefined;
56
+ autoResize?: boolean | undefined;
57
57
  /**
58
58
  * The activation mode for the preview element.
59
59
  *
@@ -63,7 +63,7 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
63
63
  *
64
64
  * @default "focus"
65
65
  */
66
- 'activationMode': ActivationMode;
66
+ activationMode: ActivationMode;
67
67
  /**
68
68
  * The action that triggers submit in the edit mode:
69
69
  *
@@ -74,72 +74,75 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
74
74
  *
75
75
  * @default "both"
76
76
  */
77
- 'submitMode': SubmitMode;
77
+ submitMode: SubmitMode;
78
78
  /**
79
- * Whether the editable is in edit mode.
79
+ * Whether the editable is in edit mode (controlled sync field / legacy seed).
80
+ * Phase 3: presence of `edit` is presence-controlled.
81
+ * There is no `defaultEdit` — omit `edit` and enter via gestures for uncontrolled usage.
80
82
  */
81
- 'edit'?: boolean | undefined;
82
- /**
83
- * Whether the editable is controlled
84
- */
85
- 'edit.controlled'?: boolean | undefined;
83
+ edit?: boolean | undefined;
86
84
  /**
87
85
  * Whether to select the text in the input when it is focused.
88
86
  * @default true
89
87
  */
90
- 'selectOnFocus'?: boolean | undefined;
88
+ selectOnFocus?: boolean | undefined;
89
+ /**
90
+ * The controlled value of the editable in both edit and preview mode
91
+ */
92
+ value?: string | undefined;
91
93
  /**
92
- * The value of the editable in both edit and preview mode
94
+ * The initial value when it is not controlled.
95
+ * Prefer this over seeding with `value` for uncontrolled usage.
93
96
  */
94
- 'value': string;
97
+ defaultValue?: string | undefined;
95
98
  /**
96
99
  * The maximum number of characters allowed in the editable
97
100
  */
98
- 'maxLength'?: number | undefined;
101
+ maxLength?: number | undefined;
99
102
  /**
100
103
  * Whether the editable is disabled
101
104
  */
102
- 'disabled'?: boolean | undefined;
105
+ disabled?: boolean | undefined;
103
106
  /**
104
107
  * Whether the editable is readonly
105
108
  */
106
- 'readOnly'?: boolean | undefined;
109
+ readOnly?: boolean | undefined;
107
110
  /**
108
111
  * Whether the editable is required
109
112
  */
110
- 'required'?: boolean | undefined;
113
+ required?: boolean | undefined;
111
114
  /**
112
115
  * The callback that is called when the editable's value is changed
113
116
  */
114
- 'onValueChange'?: ((details: ValueChangeDetails) => void) | undefined;
117
+ onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
115
118
  /**
116
119
  * The callback that is called when the esc key is pressed or the cancel button is clicked
117
120
  */
118
- 'onValueRevert'?: ((details: ValueChangeDetails) => void) | undefined;
121
+ onValueRevert?: ((details: ValueChangeDetails) => void) | undefined;
119
122
  /**
120
123
  * The callback that is called when the editable's value is submitted.
121
124
  */
122
- 'onValueCommit'?: ((details: ValueChangeDetails) => void) | undefined;
125
+ onValueCommit?: ((details: ValueChangeDetails) => void) | undefined;
123
126
  /**
124
127
  * The callback that is called when the edit mode is changed
125
128
  */
126
- 'onEditChange'?: ((details: EditChangeDetails) => void) | undefined;
129
+ onEditChange?: ((details: EditChangeDetails) => void) | undefined;
127
130
  /**
128
131
  * The placeholder value to show when the `value` is empty
129
132
  */
130
- 'placeholder'?: string | {
133
+ placeholder?: string | {
131
134
  edit: string;
132
135
  preview: string;
133
136
  } | undefined;
134
137
  /**
135
138
  * Specifies the localized strings that identifies the accessibility elements and their states
136
139
  */
137
- 'translations': IntlTranslations;
140
+ translations: IntlTranslations;
138
141
  /**
139
142
  * The element that should receive focus when the editable is closed.
140
143
  * By default, it will focus on the trigger element.
141
144
  */
142
- 'finalFocusEl'?: (() => HTMLElement | null) | undefined;
145
+ finalFocusEl?: (() => HTMLElement | null) | undefined;
143
146
  }
144
147
  type UserDefinedContext = RequiredBy<PublicContext, 'id'>;
145
148
  type ComputedContext = Readonly<{
@@ -228,7 +231,7 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
228
231
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
229
232
  //#endregion
230
233
  //#region src/props.d.ts
231
- declare const props: ("edit" | "id" | "ids" | "invalid" | "name" | "form" | "autoResize" | "activationMode" | "submitMode" | "edit.controlled" | "selectOnFocus" | "value" | "maxLength" | "disabled" | "readOnly" | "required" | "onValueChange" | "onValueRevert" | "onValueCommit" | "onEditChange" | "placeholder" | "translations" | "finalFocusEl" | "dir" | "getRootNode" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside")[];
232
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "edit" | "id" | "ids" | "invalid" | "name" | "form" | "autoResize" | "activationMode" | "submitMode" | "edit.controlled" | "selectOnFocus" | "value" | "maxLength" | "disabled" | "readOnly" | "required" | "onValueChange" | "onValueRevert" | "onValueCommit" | "onEditChange" | "placeholder" | "translations" | "finalFocusEl" | "dir" | "getRootNode" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside">];
234
+ declare const props: ("id" | "ids" | "invalid" | "name" | "form" | "autoResize" | "activationMode" | "submitMode" | "edit" | "selectOnFocus" | "value" | "defaultValue" | "maxLength" | "disabled" | "readOnly" | "required" | "onValueChange" | "onValueRevert" | "onValueCommit" | "onEditChange" | "placeholder" | "translations" | "finalFocusEl" | "dir" | "getRootNode" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside")[];
235
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "id" | "ids" | "invalid" | "name" | "form" | "autoResize" | "activationMode" | "submitMode" | "edit" | "selectOnFocus" | "value" | "defaultValue" | "maxLength" | "disabled" | "readOnly" | "required" | "onValueChange" | "onValueRevert" | "onValueCommit" | "onEditChange" | "placeholder" | "translations" | "finalFocusEl" | "dir" | "getRootNode" | "onPointerDownOutside" | "onFocusOutside" | "onInteractOutside">];
233
236
  //#endregion
234
237
  export { type ActivationMode, type MachineApi as Api, type UserDefinedContext as Context, type EditChangeDetails, type ElementIds, type FocusOutsideEvent, type InteractOutsideEvent, type IntlTranslations, type PointerDownOutsideEvent, type Service, type SubmitMode, type ValueChangeDetails, anatomy, connect, machine, props, splitProps };
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createAnatomy } from "@destyler/anatomy";
2
2
  import { ariaAttr, contains, createScope, dataAttr, isApple, isComposingEvent, raf } from "@destyler/dom";
3
3
  import { trackInteractOutside } from "@destyler/interact-outside";
4
- import { compact, createSplitProps, isEqual } from "@destyler/utils";
4
+ import { compact, createSplitProps, isControlled, isEqual, isPropUserProvided, resolveControllableProp, withControllableProvided } from "@destyler/utils";
5
5
  import { createMachine } from "@destyler/xstate";
6
6
  import { createProps } from "@destyler/types";
7
7
 
@@ -306,15 +306,30 @@ const invoke = { change(ctx) {
306
306
  } };
307
307
  const set = { value(ctx, value) {
308
308
  if (isEqual(ctx.value, value)) return;
309
+ if (isControlled(ctx, "value")) {
310
+ ctx.onValueChange?.({ value });
311
+ return;
312
+ }
309
313
  ctx.value = value;
310
314
  invoke.change(ctx);
311
315
  } };
312
316
  function machine(userContext) {
313
- const ctx = compact(userContext);
317
+ const ctx = compact(withControllableProvided(userContext, ["value", "edit"]));
318
+ const { initial: initialValue } = resolveControllableProp({
319
+ value: ctx.value,
320
+ defaultValue: ctx.defaultValue,
321
+ valueProvided: isPropUserProvided(ctx, "value"),
322
+ fallback: ""
323
+ });
324
+ const { initial: initialEdit } = resolveControllableProp({
325
+ value: ctx.edit,
326
+ valueProvided: isPropUserProvided(ctx, "edit"),
327
+ fallback: false
328
+ });
314
329
  return createMachine({
315
330
  id: "editable",
316
- initial: ctx.edit ? "edit" : "preview",
317
- entry: ctx.edit ? ["focusInput"] : void 0,
331
+ initial: initialEdit ? "edit" : "preview",
332
+ entry: initialEdit ? ["focusInput"] : void 0,
318
333
  context: {
319
334
  activationMode: "focus",
320
335
  submitMode: "both",
@@ -324,6 +339,8 @@ function machine(userContext) {
324
339
  disabled: false,
325
340
  readOnly: false,
326
341
  ...ctx,
342
+ value: initialValue,
343
+ previousValue: initialValue,
327
344
  translations: {
328
345
  input: "editable input",
329
346
  edit: "edit",
@@ -411,7 +428,7 @@ function machine(userContext) {
411
428
  }
412
429
  }, {
413
430
  guards: {
414
- isEditControlled: (ctx$1) => !!ctx$1["edit.controlled"],
431
+ isEditControlled: (ctx$1) => isControlled(ctx$1, "edit"),
415
432
  isSubmitEvent: (_ctx, evt) => evt.previousEvent?.type === "SUBMIT"
416
433
  },
417
434
  activities: { trackInteractOutside(ctx$1, _evt, { send }) {
@@ -492,6 +509,7 @@ const props = createProps()([
492
509
  "activationMode",
493
510
  "autoResize",
494
511
  "dir",
512
+ "defaultValue",
495
513
  "disabled",
496
514
  "finalFocusEl",
497
515
  "form",
@@ -513,7 +531,6 @@ const props = createProps()([
513
531
  "required",
514
532
  "selectOnFocus",
515
533
  "edit",
516
- "edit.controlled",
517
534
  "submitMode",
518
535
  "translations",
519
536
  "value"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/edit",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.9",
5
5
  "description": "a component that allows users to edit text in place.",
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",
26
- "@destyler/dom": "0.2.6",
27
- "@destyler/types": "0.2.6",
28
- "@destyler/interact-outside": "0.2.6",
29
- "@destyler/xstate": "0.2.6",
30
- "@destyler/utils": "0.2.6"
25
+ "@destyler/anatomy": "0.2.9",
26
+ "@destyler/dom": "0.2.9",
27
+ "@destyler/types": "0.2.9",
28
+ "@destyler/xstate": "0.2.9",
29
+ "@destyler/interact-outside": "0.2.9",
30
+ "@destyler/utils": "0.2.9"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"