@yamada-ui/editable 0.1.0

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.
@@ -0,0 +1,450 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/editable.tsx
21
+ var editable_exports = {};
22
+ __export(editable_exports, {
23
+ Editable: () => Editable,
24
+ EditableInput: () => EditableInput,
25
+ EditablePreview: () => EditablePreview,
26
+ EditableTextarea: () => EditableTextarea,
27
+ useEditable: () => useEditable,
28
+ useEditableControl: () => useEditableControl
29
+ });
30
+ module.exports = __toCommonJS(editable_exports);
31
+ var import_core = require("@yamada-ui/core");
32
+ var import_form_control = require("@yamada-ui/form-control");
33
+ var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
34
+ var import_use_focus = require("@yamada-ui/use-focus");
35
+ var import_utils = require("@yamada-ui/utils");
36
+ var import_react = require("react");
37
+ var import_jsx_runtime = require("react/jsx-runtime");
38
+ var useEditable = (props) => {
39
+ const {
40
+ id,
41
+ placeholder,
42
+ defaultValue,
43
+ required,
44
+ disabled,
45
+ readOnly,
46
+ startWithEditView,
47
+ isPreviewFocusable = true,
48
+ submitOnBlur = true,
49
+ selectAllOnFocus = true,
50
+ ...rest
51
+ } = (0, import_form_control.useFormControlProps)(props);
52
+ rest.onEdit = (0, import_utils.useCallbackRef)(rest.onEdit);
53
+ const [isEditing, setIsEditing] = (0, import_react.useState)(!!startWithEditView && !disabled);
54
+ const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
55
+ defaultValue: defaultValue || "",
56
+ value: rest.value,
57
+ onChange: rest.onChange
58
+ });
59
+ const isInteractive = !isEditing && !disabled;
60
+ const isValueEmpty = value.length === 0;
61
+ const [prevValue, setPrevValue] = (0, import_react.useState)(value);
62
+ const inputRef = (0, import_react.useRef)(null);
63
+ const previewRef = (0, import_react.useRef)(null);
64
+ const editRef = (0, import_react.useRef)(null);
65
+ const cancelRef = (0, import_react.useRef)(null);
66
+ const submitRef = (0, import_react.useRef)(null);
67
+ (0, import_use_focus.useFocusOnPointerDown)({
68
+ ref: inputRef,
69
+ enabled: isEditing,
70
+ elements: [cancelRef, submitRef]
71
+ });
72
+ (0, import_utils.useSafeLayoutEffect)(() => {
73
+ var _a, _b;
74
+ if (!isEditing)
75
+ return;
76
+ (_a = inputRef.current) == null ? void 0 : _a.focus();
77
+ if (selectAllOnFocus)
78
+ (_b = inputRef.current) == null ? void 0 : _b.select();
79
+ }, []);
80
+ (0, import_utils.useUpdateEffect)(() => {
81
+ var _a, _b, _c, _d;
82
+ if (!isEditing) {
83
+ (_a = editRef.current) == null ? void 0 : _a.focus();
84
+ return;
85
+ }
86
+ (_b = inputRef.current) == null ? void 0 : _b.focus();
87
+ if (selectAllOnFocus)
88
+ (_c = inputRef.current) == null ? void 0 : _c.select();
89
+ (_d = rest.onEdit) == null ? void 0 : _d.call(rest);
90
+ }, [isEditing, rest.onEdit, selectAllOnFocus]);
91
+ (0, import_react.useEffect)(() => {
92
+ if (isEditing)
93
+ return;
94
+ const el = inputRef.current;
95
+ const activeEl = el == null ? void 0 : el.ownerDocument.activeElement;
96
+ if (activeEl === el)
97
+ el == null ? void 0 : el.blur();
98
+ }, [isEditing]);
99
+ const onChange = (0, import_react.useCallback)(
100
+ (ev) => setValue(ev.currentTarget.value),
101
+ [setValue]
102
+ );
103
+ const onUpdatePrevValue = (0, import_react.useCallback)(() => setPrevValue(value), [value]);
104
+ const onEdit = (0, import_react.useCallback)(() => {
105
+ if (isInteractive)
106
+ setIsEditing(true);
107
+ }, [isInteractive]);
108
+ const onCancel = (0, import_react.useCallback)(() => {
109
+ var _a;
110
+ setIsEditing(false);
111
+ setValue(prevValue);
112
+ (_a = rest.onCancel) == null ? void 0 : _a.call(rest, prevValue);
113
+ }, [prevValue, rest, setValue]);
114
+ const onSubmit = (0, import_react.useCallback)(() => {
115
+ var _a;
116
+ setIsEditing(false);
117
+ setPrevValue(value);
118
+ (_a = rest.onSubmit) == null ? void 0 : _a.call(rest, value);
119
+ }, [rest, value]);
120
+ const onKeyDown = (0, import_react.useCallback)(
121
+ (ev) => {
122
+ if (ev.key !== "Escape" && ev.key !== "Enter")
123
+ return;
124
+ ev.preventDefault();
125
+ if (ev.key === "Escape") {
126
+ onCancel();
127
+ } else {
128
+ const { shiftKey, metaKey } = ev;
129
+ if (!shiftKey && !metaKey)
130
+ onSubmit();
131
+ }
132
+ },
133
+ [onCancel, onSubmit]
134
+ );
135
+ const onKeyDownWithoutSubmit = (0, import_react.useCallback)(
136
+ (ev) => {
137
+ if (ev.key !== "Escape")
138
+ return;
139
+ ev.preventDefault();
140
+ onCancel();
141
+ },
142
+ [onCancel]
143
+ );
144
+ const onBlur = (0, import_react.useCallback)(
145
+ (ev) => {
146
+ var _a;
147
+ if (!isEditing)
148
+ return;
149
+ const ownerDocument = ev.currentTarget.ownerDocument;
150
+ const relatedTarget = (_a = ev.relatedTarget) != null ? _a : ownerDocument.activeElement;
151
+ const targetIsCancel = (0, import_utils.isContains)(cancelRef.current, relatedTarget);
152
+ const targetIsSubmit = (0, import_utils.isContains)(submitRef.current, relatedTarget);
153
+ const isValidBlur = !targetIsCancel && !targetIsSubmit;
154
+ if (!isValidBlur)
155
+ return;
156
+ if (submitOnBlur) {
157
+ onSubmit();
158
+ } else {
159
+ onCancel();
160
+ }
161
+ },
162
+ [isEditing, submitOnBlur, onSubmit, onCancel]
163
+ );
164
+ const getPreviewProps = (0, import_react.useCallback)(
165
+ (props2 = {}, ref = null) => ({
166
+ ...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
167
+ ...props2,
168
+ ref: (0, import_utils.mergeRefs)(ref, previewRef),
169
+ hidden: isEditing,
170
+ tabIndex: isInteractive && isPreviewFocusable ? 0 : void 0,
171
+ children: isValueEmpty ? placeholder : value,
172
+ onFocus: (0, import_utils.handlerAll)(props2.onFocus, onEdit, onUpdatePrevValue)
173
+ }),
174
+ [
175
+ isEditing,
176
+ isInteractive,
177
+ isPreviewFocusable,
178
+ isValueEmpty,
179
+ onEdit,
180
+ onUpdatePrevValue,
181
+ placeholder,
182
+ rest,
183
+ value
184
+ ]
185
+ );
186
+ const getInputProps = (0, import_react.useCallback)(
187
+ (props2 = {}, ref = null) => ({
188
+ ...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
189
+ ...props2,
190
+ ref: (0, import_utils.mergeRefs)(ref, inputRef),
191
+ id,
192
+ placeholder,
193
+ hidden: !isEditing,
194
+ value,
195
+ required,
196
+ disabled,
197
+ readOnly,
198
+ onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
199
+ onChange: (0, import_utils.handlerAll)(props2.onChange, onChange),
200
+ onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDown),
201
+ onFocus: (0, import_utils.handlerAll)(props2.onFocus, onUpdatePrevValue)
202
+ }),
203
+ [
204
+ disabled,
205
+ id,
206
+ isEditing,
207
+ onBlur,
208
+ onChange,
209
+ onKeyDown,
210
+ onUpdatePrevValue,
211
+ placeholder,
212
+ readOnly,
213
+ required,
214
+ rest,
215
+ value
216
+ ]
217
+ );
218
+ const getTextareaProps = (0, import_react.useCallback)(
219
+ (props2 = {}, ref = null) => ({
220
+ ...(0, import_utils.pickObject)(rest, import_form_control.formControlProperties),
221
+ ...props2,
222
+ ref: (0, import_utils.mergeRefs)(ref, inputRef),
223
+ id,
224
+ placeholder,
225
+ hidden: !isEditing,
226
+ value,
227
+ required,
228
+ disabled,
229
+ readOnly,
230
+ onBlur: (0, import_utils.handlerAll)(props2.onBlur, onBlur),
231
+ onChange: (0, import_utils.handlerAll)(props2.onChange, onChange),
232
+ onKeyDown: (0, import_utils.handlerAll)(props2.onKeyDown, onKeyDownWithoutSubmit),
233
+ onFocus: (0, import_utils.handlerAll)(props2.onFocus, onUpdatePrevValue)
234
+ }),
235
+ [
236
+ disabled,
237
+ id,
238
+ isEditing,
239
+ onBlur,
240
+ onChange,
241
+ onKeyDownWithoutSubmit,
242
+ onUpdatePrevValue,
243
+ placeholder,
244
+ readOnly,
245
+ required,
246
+ rest,
247
+ value
248
+ ]
249
+ );
250
+ const getEditProps = (0, import_react.useCallback)(
251
+ (props2 = {}, ref = null) => ({
252
+ ...props2,
253
+ ...(0, import_utils.omitObject)(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
254
+ ref: (0, import_utils.mergeRefs)(ref, editRef),
255
+ type: "button",
256
+ disabled,
257
+ readOnly,
258
+ onClick: (0, import_utils.handlerAll)(props2.onClick, onEdit)
259
+ }),
260
+ [disabled, onEdit, readOnly, rest]
261
+ );
262
+ const getSubmitProps = (0, import_react.useCallback)(
263
+ (props2 = {}, ref = null) => ({
264
+ ...props2,
265
+ ...(0, import_utils.omitObject)(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
266
+ ref: (0, import_utils.mergeRefs)(submitRef, ref),
267
+ type: "button",
268
+ disabled,
269
+ readOnly,
270
+ onClick: (0, import_utils.handlerAll)(props2.onClick, onSubmit)
271
+ }),
272
+ [disabled, onSubmit, readOnly, rest]
273
+ );
274
+ const getCancelProps = (0, import_react.useCallback)(
275
+ (props2 = {}, ref = null) => ({
276
+ ...props2,
277
+ ...(0, import_utils.omitObject)(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
278
+ ref: (0, import_utils.mergeRefs)(cancelRef, ref),
279
+ type: "button",
280
+ disabled,
281
+ readOnly,
282
+ onClick: (0, import_utils.handlerAll)(props2.onClick, onCancel)
283
+ }),
284
+ [disabled, onCancel, readOnly, rest]
285
+ );
286
+ return {
287
+ isEditing,
288
+ value,
289
+ onEdit,
290
+ onCancel,
291
+ onSubmit,
292
+ getPreviewProps,
293
+ getInputProps,
294
+ getTextareaProps,
295
+ getEditProps,
296
+ getSubmitProps,
297
+ getCancelProps
298
+ };
299
+ };
300
+ var useEditableControl = () => {
301
+ const { isEditing, getEditProps, getCancelProps, getSubmitProps } = useEditableContext();
302
+ return { isEditing, getEditProps, getCancelProps, getSubmitProps };
303
+ };
304
+ var [EditableProvider, useEditableContext] = (0, import_utils.createContext)({
305
+ name: "EditableContext",
306
+ errorMessage: "useEditableContext: context is undefined. Seems you forgot to wrap the editable components in `<Editable />`"
307
+ });
308
+ var Editable = (0, import_core.forwardRef)(
309
+ ({ focusBorderColor, errorBorderColor, ...props }, ref) => {
310
+ const [styles, mergedProps] = (0, import_core.useMultiComponentStyle)("Editable", {
311
+ focusBorderColor,
312
+ errorBorderColor,
313
+ ...props
314
+ });
315
+ const { className, children, ...rest } = (0, import_core.omitThemeProps)(mergedProps);
316
+ const {
317
+ isEditing,
318
+ getPreviewProps,
319
+ getInputProps,
320
+ getTextareaProps,
321
+ getEditProps,
322
+ getCancelProps,
323
+ getSubmitProps,
324
+ onSubmit,
325
+ onCancel,
326
+ onEdit
327
+ } = useEditable(rest);
328
+ const cloneChildren = (0, import_utils.runIfFunc)(children, {
329
+ isEditing,
330
+ onSubmit,
331
+ onCancel,
332
+ onEdit
333
+ });
334
+ const css = { ...styles.container };
335
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
336
+ EditableProvider,
337
+ {
338
+ value: {
339
+ isEditing,
340
+ getPreviewProps,
341
+ getInputProps,
342
+ getTextareaProps,
343
+ getEditProps,
344
+ getCancelProps,
345
+ getSubmitProps,
346
+ styles
347
+ },
348
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
349
+ import_core.ui.div,
350
+ {
351
+ ref,
352
+ className: (0, import_utils.cx)("ui-editable", className),
353
+ ...(0, import_utils.omitObject)(rest, [
354
+ "placeholder",
355
+ "value",
356
+ "defaultValue",
357
+ "isInvalid",
358
+ "isReadOnly",
359
+ "isRequired",
360
+ "isDisabled",
361
+ "startWithEditView",
362
+ "isPreviewFocusable",
363
+ "submitOnBlur",
364
+ "selectAllOnFocus",
365
+ "onChange",
366
+ "onCancel",
367
+ "onSubmit",
368
+ "onEdit"
369
+ ]),
370
+ __css: css,
371
+ children: cloneChildren
372
+ }
373
+ )
374
+ }
375
+ );
376
+ }
377
+ );
378
+ var EditablePreview = (0, import_core.forwardRef)(
379
+ ({ className, ...rest }, ref) => {
380
+ const { styles, getPreviewProps } = useEditableContext();
381
+ const css = {
382
+ cursor: "text",
383
+ display: "inline-block",
384
+ fontSize: "inherit",
385
+ fontWeight: "inherit",
386
+ textAlign: "inherit",
387
+ bg: "transparent",
388
+ ...styles.preview
389
+ };
390
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
391
+ import_core.ui.span,
392
+ {
393
+ className: (0, import_utils.cx)("ui-editable-preview", className),
394
+ ...getPreviewProps(rest, ref),
395
+ __css: css
396
+ }
397
+ );
398
+ }
399
+ );
400
+ var EditableInput = (0, import_core.forwardRef)(
401
+ ({ className, ...rest }, ref) => {
402
+ const { styles, getInputProps } = useEditableContext();
403
+ const css = {
404
+ outline: 0,
405
+ fontSize: "inherit",
406
+ fontWeight: "inherit",
407
+ textAlign: "inherit",
408
+ bg: "transparent",
409
+ ...styles.input
410
+ };
411
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
412
+ import_core.ui.input,
413
+ {
414
+ className: (0, import_utils.cx)("ui-editable-input", className),
415
+ ...getInputProps(rest, ref),
416
+ __css: css
417
+ }
418
+ );
419
+ }
420
+ );
421
+ var EditableTextarea = (0, import_core.forwardRef)(
422
+ ({ className, ...rest }, ref) => {
423
+ const { styles, getTextareaProps } = useEditableContext();
424
+ const css = {
425
+ outline: 0,
426
+ fontSize: "inherit",
427
+ fontWeight: "inherit",
428
+ textAlign: "inherit",
429
+ bg: "transparent",
430
+ ...styles.textarea
431
+ };
432
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
433
+ import_core.ui.textarea,
434
+ {
435
+ className: (0, import_utils.cx)("ui-editable-textarea", className),
436
+ ...getTextareaProps(rest, ref),
437
+ __css: css
438
+ }
439
+ );
440
+ }
441
+ );
442
+ // Annotate the CommonJS export names for ESM import in node:
443
+ 0 && (module.exports = {
444
+ Editable,
445
+ EditableInput,
446
+ EditablePreview,
447
+ EditableTextarea,
448
+ useEditable,
449
+ useEditableControl
450
+ });
@@ -0,0 +1,16 @@
1
+ import {
2
+ Editable,
3
+ EditableInput,
4
+ EditablePreview,
5
+ EditableTextarea,
6
+ useEditable,
7
+ useEditableControl
8
+ } from "./chunk-VFULS7K5.mjs";
9
+ export {
10
+ Editable,
11
+ EditableInput,
12
+ EditablePreview,
13
+ EditableTextarea,
14
+ useEditable,
15
+ useEditableControl
16
+ };
@@ -0,0 +1,5 @@
1
+ export { Editable, EditableInput, EditableInputProps, EditablePreview, EditablePreviewProps, EditableProps, EditableTextarea, EditableTextareaProps, UseEditableProps, UseEditableReturn, useEditable, useEditableControl } from './editable.js';
2
+ import '@yamada-ui/core';
3
+ import '@yamada-ui/utils';
4
+ import '@yamada-ui/form-control';
5
+ import 'react';