@destyler/edit 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,11 +1,10 @@
1
- import * as _destyler_anatomy0 from "@destyler/anatomy";
2
1
  import { FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, PointerDownOutsideEvent } from "@destyler/interact-outside";
3
2
  import * as _destyler_xstate0 from "@destyler/xstate";
4
3
  import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
5
4
  import { CommonProperties, DirectionProperty, NormalizeProps, PropTypes, RequiredBy } from "@destyler/types";
6
5
 
7
6
  //#region src/anatomy.d.ts
8
- declare const anatomy: _destyler_anatomy0.AnatomyInstance<"root" | "area" | "label" | "preview" | "input" | "editTrigger" | "submitTrigger" | "cancelTrigger" | "control">;
7
+ declare const anatomy: any;
9
8
  //#endregion
10
9
  //#region src/types.d.ts
11
10
  interface ValueChangeDetails {
@@ -37,23 +36,23 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
37
36
  /**
38
37
  * The ids of the elements in the editable. Useful for composition.
39
38
  */
40
- 'ids'?: ElementIds | undefined;
39
+ ids?: ElementIds | undefined;
41
40
  /**
42
41
  * Whether the input's value is invalid.
43
42
  */
44
- 'invalid'?: boolean | undefined;
43
+ invalid?: boolean | undefined;
45
44
  /**
46
45
  * The name attribute of the editable component. Used for form submission.
47
46
  */
48
- 'name'?: string | undefined;
47
+ name?: string | undefined;
49
48
  /**
50
49
  * The associate form of the underlying input.
51
50
  */
52
- 'form'?: string | undefined;
51
+ form?: string | undefined;
53
52
  /**
54
53
  * Whether the editable should auto-resize to fit the content.
55
54
  */
56
- 'autoResize'?: boolean | undefined;
55
+ autoResize?: boolean | undefined;
57
56
  /**
58
57
  * The activation mode for the preview element.
59
58
  *
@@ -63,7 +62,7 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
63
62
  *
64
63
  * @default "focus"
65
64
  */
66
- 'activationMode': ActivationMode;
65
+ activationMode: ActivationMode;
67
66
  /**
68
67
  * The action that triggers submit in the edit mode:
69
68
  *
@@ -74,72 +73,75 @@ interface PublicContext extends DirectionProperty, CommonProperties, InteractOut
74
73
  *
75
74
  * @default "both"
76
75
  */
77
- 'submitMode': SubmitMode;
76
+ submitMode: SubmitMode;
78
77
  /**
79
- * Whether the editable is in edit mode.
78
+ * Whether the editable is in edit mode (controlled sync field / legacy seed).
79
+ * Phase 3: presence of `edit` is presence-controlled.
80
+ * There is no `defaultEdit` — omit `edit` and enter via gestures for uncontrolled usage.
80
81
  */
81
- 'edit'?: boolean | undefined;
82
- /**
83
- * Whether the editable is controlled
84
- */
85
- 'edit.controlled'?: boolean | undefined;
82
+ edit?: boolean | undefined;
86
83
  /**
87
84
  * Whether to select the text in the input when it is focused.
88
85
  * @default true
89
86
  */
90
- 'selectOnFocus'?: boolean | undefined;
87
+ selectOnFocus?: boolean | undefined;
88
+ /**
89
+ * The controlled value of the editable in both edit and preview mode
90
+ */
91
+ value?: string | undefined;
91
92
  /**
92
- * The value of the editable in both edit and preview mode
93
+ * The initial value when it is not controlled.
94
+ * Prefer this over seeding with `value` for uncontrolled usage.
93
95
  */
94
- 'value': string;
96
+ defaultValue?: string | undefined;
95
97
  /**
96
98
  * The maximum number of characters allowed in the editable
97
99
  */
98
- 'maxLength'?: number | undefined;
100
+ maxLength?: number | undefined;
99
101
  /**
100
102
  * Whether the editable is disabled
101
103
  */
102
- 'disabled'?: boolean | undefined;
104
+ disabled?: boolean | undefined;
103
105
  /**
104
106
  * Whether the editable is readonly
105
107
  */
106
- 'readOnly'?: boolean | undefined;
108
+ readOnly?: boolean | undefined;
107
109
  /**
108
110
  * Whether the editable is required
109
111
  */
110
- 'required'?: boolean | undefined;
112
+ required?: boolean | undefined;
111
113
  /**
112
114
  * The callback that is called when the editable's value is changed
113
115
  */
114
- 'onValueChange'?: ((details: ValueChangeDetails) => void) | undefined;
116
+ onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
115
117
  /**
116
118
  * The callback that is called when the esc key is pressed or the cancel button is clicked
117
119
  */
118
- 'onValueRevert'?: ((details: ValueChangeDetails) => void) | undefined;
120
+ onValueRevert?: ((details: ValueChangeDetails) => void) | undefined;
119
121
  /**
120
122
  * The callback that is called when the editable's value is submitted.
121
123
  */
122
- 'onValueCommit'?: ((details: ValueChangeDetails) => void) | undefined;
124
+ onValueCommit?: ((details: ValueChangeDetails) => void) | undefined;
123
125
  /**
124
126
  * The callback that is called when the edit mode is changed
125
127
  */
126
- 'onEditChange'?: ((details: EditChangeDetails) => void) | undefined;
128
+ onEditChange?: ((details: EditChangeDetails) => void) | undefined;
127
129
  /**
128
130
  * The placeholder value to show when the `value` is empty
129
131
  */
130
- 'placeholder'?: string | {
132
+ placeholder?: string | {
131
133
  edit: string;
132
134
  preview: string;
133
135
  } | undefined;
134
136
  /**
135
137
  * Specifies the localized strings that identifies the accessibility elements and their states
136
138
  */
137
- 'translations': IntlTranslations;
139
+ translations: IntlTranslations;
138
140
  /**
139
141
  * The element that should receive focus when the editable is closed.
140
142
  * By default, it will focus on the trigger element.
141
143
  */
142
- 'finalFocusEl'?: (() => HTMLElement | null) | undefined;
144
+ finalFocusEl?: (() => HTMLElement | null) | undefined;
143
145
  }
144
146
  type UserDefinedContext = RequiredBy<PublicContext, 'id'>;
145
147
  type ComputedContext = Readonly<{
@@ -228,7 +230,7 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
228
230
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
229
231
  //#endregion
230
232
  //#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">];
233
+ 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")[];
234
+ 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
235
  //#endregion
234
236
  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.7",
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.7",
26
+ "@destyler/dom": "0.2.7",
27
+ "@destyler/interact-outside": "0.2.7",
28
+ "@destyler/utils": "0.2.7",
29
+ "@destyler/xstate": "0.2.7",
30
+ "@destyler/types": "0.2.7"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public"