@yamada-ui/editable 0.0.0-dev-20230603042803

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Hirotomo Yamada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @yamada-ui/editable
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ pnpm add @yamada-ui/editable
7
+ ```
8
+
9
+ or
10
+
11
+ ```sh
12
+ $ yarn add @yamada-ui/editable
13
+ ```
14
+
15
+ or
16
+
17
+ ```sh
18
+ $ npm install @yamada-ui/editable
19
+ ```
20
+
21
+ ## Contribution
22
+
23
+ Wouldn't you like to contribute? That's amazing! We have prepared a [contribution guide](https://github.com/hirotomoyamada/yamada-ui/blob/main/CONTRIBUTING.md) to assist you.
24
+
25
+ ## Licence
26
+
27
+ This package is licensed under the terms of the
28
+ [MIT license](https://github.com/hirotomoyamada/yamada-ui/blob/main/LICENSE).
@@ -0,0 +1,446 @@
1
+ // src/editable.tsx
2
+ import {
3
+ ui,
4
+ forwardRef,
5
+ useMultiComponentStyle,
6
+ omitThemeProps
7
+ } from "@yamada-ui/core";
8
+ import {
9
+ formControlProperties,
10
+ useFormControlProps
11
+ } from "@yamada-ui/form-control";
12
+ import { useControllableState } from "@yamada-ui/use-controllable-state";
13
+ import { useFocusOnPointerDown } from "@yamada-ui/use-focus";
14
+ import {
15
+ createContext,
16
+ cx,
17
+ omitObject,
18
+ runIfFunc,
19
+ useCallbackRef,
20
+ useSafeLayoutEffect,
21
+ useUpdateEffect,
22
+ isContains,
23
+ handlerAll,
24
+ mergeRefs,
25
+ pickObject
26
+ } from "@yamada-ui/utils";
27
+ import {
28
+ useCallback,
29
+ useEffect,
30
+ useRef,
31
+ useState
32
+ } from "react";
33
+ import { jsx } from "react/jsx-runtime";
34
+ var useEditable = (props) => {
35
+ const {
36
+ id,
37
+ placeholder,
38
+ defaultValue,
39
+ required,
40
+ disabled,
41
+ readOnly,
42
+ startWithEditView,
43
+ isPreviewFocusable = true,
44
+ submitOnBlur = true,
45
+ selectAllOnFocus = true,
46
+ ...rest
47
+ } = useFormControlProps(props);
48
+ rest.onEdit = useCallbackRef(rest.onEdit);
49
+ const [isEditing, setIsEditing] = useState(!!startWithEditView && !disabled);
50
+ const [value, setValue] = useControllableState({
51
+ defaultValue: defaultValue || "",
52
+ value: rest.value,
53
+ onChange: rest.onChange
54
+ });
55
+ const isInteractive = !isEditing && !disabled;
56
+ const isValueEmpty = value.length === 0;
57
+ const [prevValue, setPrevValue] = useState(value);
58
+ const inputRef = useRef(null);
59
+ const previewRef = useRef(null);
60
+ const editRef = useRef(null);
61
+ const cancelRef = useRef(null);
62
+ const submitRef = useRef(null);
63
+ useFocusOnPointerDown({
64
+ ref: inputRef,
65
+ enabled: isEditing,
66
+ elements: [cancelRef, submitRef]
67
+ });
68
+ useSafeLayoutEffect(() => {
69
+ var _a, _b;
70
+ if (!isEditing)
71
+ return;
72
+ (_a = inputRef.current) == null ? void 0 : _a.focus();
73
+ if (selectAllOnFocus)
74
+ (_b = inputRef.current) == null ? void 0 : _b.select();
75
+ }, []);
76
+ useUpdateEffect(() => {
77
+ var _a, _b, _c, _d;
78
+ if (!isEditing) {
79
+ (_a = editRef.current) == null ? void 0 : _a.focus();
80
+ return;
81
+ }
82
+ (_b = inputRef.current) == null ? void 0 : _b.focus();
83
+ if (selectAllOnFocus)
84
+ (_c = inputRef.current) == null ? void 0 : _c.select();
85
+ (_d = rest.onEdit) == null ? void 0 : _d.call(rest);
86
+ }, [isEditing, rest.onEdit, selectAllOnFocus]);
87
+ useEffect(() => {
88
+ if (isEditing)
89
+ return;
90
+ const el = inputRef.current;
91
+ const activeEl = el == null ? void 0 : el.ownerDocument.activeElement;
92
+ if (activeEl === el)
93
+ el == null ? void 0 : el.blur();
94
+ }, [isEditing]);
95
+ const onChange = useCallback(
96
+ (ev) => setValue(ev.currentTarget.value),
97
+ [setValue]
98
+ );
99
+ const onUpdatePrevValue = useCallback(() => setPrevValue(value), [value]);
100
+ const onEdit = useCallback(() => {
101
+ if (isInteractive)
102
+ setIsEditing(true);
103
+ }, [isInteractive]);
104
+ const onCancel = useCallback(() => {
105
+ var _a;
106
+ setIsEditing(false);
107
+ setValue(prevValue);
108
+ (_a = rest.onCancel) == null ? void 0 : _a.call(rest, prevValue);
109
+ }, [prevValue, rest, setValue]);
110
+ const onSubmit = useCallback(() => {
111
+ var _a;
112
+ setIsEditing(false);
113
+ setPrevValue(value);
114
+ (_a = rest.onSubmit) == null ? void 0 : _a.call(rest, value);
115
+ }, [rest, value]);
116
+ const onKeyDown = useCallback(
117
+ (ev) => {
118
+ if (ev.key !== "Escape" && ev.key !== "Enter")
119
+ return;
120
+ ev.preventDefault();
121
+ if (ev.key === "Escape") {
122
+ onCancel();
123
+ } else {
124
+ const { shiftKey, metaKey } = ev;
125
+ if (!shiftKey && !metaKey)
126
+ onSubmit();
127
+ }
128
+ },
129
+ [onCancel, onSubmit]
130
+ );
131
+ const onKeyDownWithoutSubmit = useCallback(
132
+ (ev) => {
133
+ if (ev.key !== "Escape")
134
+ return;
135
+ ev.preventDefault();
136
+ onCancel();
137
+ },
138
+ [onCancel]
139
+ );
140
+ const onBlur = useCallback(
141
+ (ev) => {
142
+ var _a;
143
+ if (!isEditing)
144
+ return;
145
+ const ownerDocument = ev.currentTarget.ownerDocument;
146
+ const relatedTarget = (_a = ev.relatedTarget) != null ? _a : ownerDocument.activeElement;
147
+ const targetIsCancel = isContains(cancelRef.current, relatedTarget);
148
+ const targetIsSubmit = isContains(submitRef.current, relatedTarget);
149
+ const isValidBlur = !targetIsCancel && !targetIsSubmit;
150
+ if (!isValidBlur)
151
+ return;
152
+ if (submitOnBlur) {
153
+ onSubmit();
154
+ } else {
155
+ onCancel();
156
+ }
157
+ },
158
+ [isEditing, submitOnBlur, onSubmit, onCancel]
159
+ );
160
+ const getPreviewProps = useCallback(
161
+ (props2 = {}, ref = null) => ({
162
+ ...pickObject(rest, formControlProperties),
163
+ ...props2,
164
+ ref: mergeRefs(ref, previewRef),
165
+ hidden: isEditing,
166
+ tabIndex: isInteractive && isPreviewFocusable ? 0 : void 0,
167
+ children: isValueEmpty ? placeholder : value,
168
+ onFocus: handlerAll(props2.onFocus, onEdit, onUpdatePrevValue)
169
+ }),
170
+ [
171
+ isEditing,
172
+ isInteractive,
173
+ isPreviewFocusable,
174
+ isValueEmpty,
175
+ onEdit,
176
+ onUpdatePrevValue,
177
+ placeholder,
178
+ rest,
179
+ value
180
+ ]
181
+ );
182
+ const getInputProps = useCallback(
183
+ (props2 = {}, ref = null) => ({
184
+ ...pickObject(rest, formControlProperties),
185
+ ...props2,
186
+ ref: mergeRefs(ref, inputRef),
187
+ id,
188
+ placeholder,
189
+ hidden: !isEditing,
190
+ value,
191
+ required,
192
+ disabled,
193
+ readOnly,
194
+ onBlur: handlerAll(props2.onBlur, onBlur),
195
+ onChange: handlerAll(props2.onChange, onChange),
196
+ onKeyDown: handlerAll(props2.onKeyDown, onKeyDown),
197
+ onFocus: handlerAll(props2.onFocus, onUpdatePrevValue)
198
+ }),
199
+ [
200
+ disabled,
201
+ id,
202
+ isEditing,
203
+ onBlur,
204
+ onChange,
205
+ onKeyDown,
206
+ onUpdatePrevValue,
207
+ placeholder,
208
+ readOnly,
209
+ required,
210
+ rest,
211
+ value
212
+ ]
213
+ );
214
+ const getTextareaProps = useCallback(
215
+ (props2 = {}, ref = null) => ({
216
+ ...pickObject(rest, formControlProperties),
217
+ ...props2,
218
+ ref: mergeRefs(ref, inputRef),
219
+ id,
220
+ placeholder,
221
+ hidden: !isEditing,
222
+ value,
223
+ required,
224
+ disabled,
225
+ readOnly,
226
+ onBlur: handlerAll(props2.onBlur, onBlur),
227
+ onChange: handlerAll(props2.onChange, onChange),
228
+ onKeyDown: handlerAll(props2.onKeyDown, onKeyDownWithoutSubmit),
229
+ onFocus: handlerAll(props2.onFocus, onUpdatePrevValue)
230
+ }),
231
+ [
232
+ disabled,
233
+ id,
234
+ isEditing,
235
+ onBlur,
236
+ onChange,
237
+ onKeyDownWithoutSubmit,
238
+ onUpdatePrevValue,
239
+ placeholder,
240
+ readOnly,
241
+ required,
242
+ rest,
243
+ value
244
+ ]
245
+ );
246
+ const getEditProps = useCallback(
247
+ (props2 = {}, ref = null) => ({
248
+ ...props2,
249
+ ...omitObject(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
250
+ ref: mergeRefs(ref, editRef),
251
+ type: "button",
252
+ disabled,
253
+ readOnly,
254
+ onClick: handlerAll(props2.onClick, onEdit)
255
+ }),
256
+ [disabled, onEdit, readOnly, rest]
257
+ );
258
+ const getSubmitProps = useCallback(
259
+ (props2 = {}, ref = null) => ({
260
+ ...props2,
261
+ ...omitObject(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
262
+ ref: mergeRefs(submitRef, ref),
263
+ type: "button",
264
+ disabled,
265
+ readOnly,
266
+ onClick: handlerAll(props2.onClick, onSubmit)
267
+ }),
268
+ [disabled, onSubmit, readOnly, rest]
269
+ );
270
+ const getCancelProps = useCallback(
271
+ (props2 = {}, ref = null) => ({
272
+ ...props2,
273
+ ...omitObject(rest, ["value", "onChange", "onCancel", "onSubmit", "onEdit"]),
274
+ ref: mergeRefs(cancelRef, ref),
275
+ type: "button",
276
+ disabled,
277
+ readOnly,
278
+ onClick: handlerAll(props2.onClick, onCancel)
279
+ }),
280
+ [disabled, onCancel, readOnly, rest]
281
+ );
282
+ return {
283
+ isEditing,
284
+ value,
285
+ onEdit,
286
+ onCancel,
287
+ onSubmit,
288
+ getPreviewProps,
289
+ getInputProps,
290
+ getTextareaProps,
291
+ getEditProps,
292
+ getSubmitProps,
293
+ getCancelProps
294
+ };
295
+ };
296
+ var useEditableControl = () => {
297
+ const { isEditing, getEditProps, getCancelProps, getSubmitProps } = useEditableContext();
298
+ return { isEditing, getEditProps, getCancelProps, getSubmitProps };
299
+ };
300
+ var [EditableProvider, useEditableContext] = createContext({
301
+ name: "EditableContext",
302
+ errorMessage: "useEditableContext: context is undefined. Seems you forgot to wrap the editable components in `<Editable />`"
303
+ });
304
+ var Editable = forwardRef(
305
+ ({ focusBorderColor, errorBorderColor, ...props }, ref) => {
306
+ const [styles, mergedProps] = useMultiComponentStyle("Editable", {
307
+ focusBorderColor,
308
+ errorBorderColor,
309
+ ...props
310
+ });
311
+ const { className, children, ...rest } = omitThemeProps(mergedProps);
312
+ const {
313
+ isEditing,
314
+ getPreviewProps,
315
+ getInputProps,
316
+ getTextareaProps,
317
+ getEditProps,
318
+ getCancelProps,
319
+ getSubmitProps,
320
+ onSubmit,
321
+ onCancel,
322
+ onEdit
323
+ } = useEditable(rest);
324
+ const cloneChildren = runIfFunc(children, {
325
+ isEditing,
326
+ onSubmit,
327
+ onCancel,
328
+ onEdit
329
+ });
330
+ const css = { ...styles.container };
331
+ return /* @__PURE__ */ jsx(
332
+ EditableProvider,
333
+ {
334
+ value: {
335
+ isEditing,
336
+ getPreviewProps,
337
+ getInputProps,
338
+ getTextareaProps,
339
+ getEditProps,
340
+ getCancelProps,
341
+ getSubmitProps,
342
+ styles
343
+ },
344
+ children: /* @__PURE__ */ jsx(
345
+ ui.div,
346
+ {
347
+ ref,
348
+ className: cx("ui-editable", className),
349
+ ...omitObject(rest, [
350
+ "placeholder",
351
+ "value",
352
+ "defaultValue",
353
+ "isInvalid",
354
+ "isReadOnly",
355
+ "isRequired",
356
+ "isDisabled",
357
+ "startWithEditView",
358
+ "isPreviewFocusable",
359
+ "submitOnBlur",
360
+ "selectAllOnFocus",
361
+ "onChange",
362
+ "onCancel",
363
+ "onSubmit",
364
+ "onEdit"
365
+ ]),
366
+ __css: css,
367
+ children: cloneChildren
368
+ }
369
+ )
370
+ }
371
+ );
372
+ }
373
+ );
374
+ var EditablePreview = forwardRef(
375
+ ({ className, ...rest }, ref) => {
376
+ const { styles, getPreviewProps } = useEditableContext();
377
+ const css = {
378
+ cursor: "text",
379
+ display: "inline-block",
380
+ fontSize: "inherit",
381
+ fontWeight: "inherit",
382
+ textAlign: "inherit",
383
+ bg: "transparent",
384
+ ...styles.preview
385
+ };
386
+ return /* @__PURE__ */ jsx(
387
+ ui.span,
388
+ {
389
+ className: cx("ui-editable-preview", className),
390
+ ...getPreviewProps(rest, ref),
391
+ __css: css
392
+ }
393
+ );
394
+ }
395
+ );
396
+ var EditableInput = forwardRef(
397
+ ({ className, ...rest }, ref) => {
398
+ const { styles, getInputProps } = useEditableContext();
399
+ const css = {
400
+ outline: 0,
401
+ fontSize: "inherit",
402
+ fontWeight: "inherit",
403
+ textAlign: "inherit",
404
+ bg: "transparent",
405
+ ...styles.input
406
+ };
407
+ return /* @__PURE__ */ jsx(
408
+ ui.input,
409
+ {
410
+ className: cx("ui-editable-input", className),
411
+ ...getInputProps(rest, ref),
412
+ __css: css
413
+ }
414
+ );
415
+ }
416
+ );
417
+ var EditableTextarea = forwardRef(
418
+ ({ className, ...rest }, ref) => {
419
+ const { styles, getTextareaProps } = useEditableContext();
420
+ const css = {
421
+ outline: 0,
422
+ fontSize: "inherit",
423
+ fontWeight: "inherit",
424
+ textAlign: "inherit",
425
+ bg: "transparent",
426
+ ...styles.textarea
427
+ };
428
+ return /* @__PURE__ */ jsx(
429
+ ui.textarea,
430
+ {
431
+ className: cx("ui-editable-textarea", className),
432
+ ...getTextareaProps(rest, ref),
433
+ __css: css
434
+ }
435
+ );
436
+ }
437
+ );
438
+
439
+ export {
440
+ useEditable,
441
+ useEditableControl,
442
+ Editable,
443
+ EditablePreview,
444
+ EditableInput,
445
+ EditableTextarea
446
+ };
@@ -0,0 +1,55 @@
1
+ import * as _yamada_ui_core from '@yamada-ui/core';
2
+ import { HTMLUIProps, ThemeProps, CSSUIProps } from '@yamada-ui/core';
3
+ import * as _yamada_ui_utils from '@yamada-ui/utils';
4
+ import { PropGetter } from '@yamada-ui/utils';
5
+ import { FormControlOptions } from '@yamada-ui/form-control';
6
+ import { ReactNode } from 'react';
7
+
8
+ type UseEditableProps = FormControlOptions & {
9
+ placeholder?: string;
10
+ value?: string;
11
+ defaultValue?: string;
12
+ startWithEditView?: boolean;
13
+ isPreviewFocusable?: boolean;
14
+ submitOnBlur?: boolean;
15
+ selectAllOnFocus?: boolean;
16
+ onChange?: (value: string) => void;
17
+ onCancel?: (preValue: string) => void;
18
+ onSubmit?: (value: string) => void;
19
+ onEdit?: () => void;
20
+ };
21
+ declare const useEditable: (props: UseEditableProps) => {
22
+ isEditing: boolean;
23
+ value: string;
24
+ onEdit: () => void;
25
+ onCancel: () => void;
26
+ onSubmit: () => void;
27
+ getPreviewProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
28
+ getInputProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
29
+ getTextareaProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
30
+ getEditProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
31
+ getSubmitProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
32
+ getCancelProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
33
+ };
34
+ type UseEditableReturn = ReturnType<typeof useEditable>;
35
+ declare const useEditableControl: () => {
36
+ isEditing: boolean;
37
+ getEditProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
38
+ getCancelProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
39
+ getSubmitProps: PropGetter<Record<string, unknown>, _yamada_ui_utils.DOMAttributes<Element & HTMLOrSVGElement>>;
40
+ };
41
+ type EditableOptions = {
42
+ focusBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
43
+ errorBorderColor?: CSSUIProps<'unresponsive'>['borderColor'];
44
+ children?: ReactNode | ((props: Pick<UseEditableReturn, 'isEditing' | 'onSubmit' | 'onCancel' | 'onEdit'>) => ReactNode);
45
+ };
46
+ type EditableProps = Omit<HTMLUIProps<'div'>, 'onChange' | 'value' | 'defaultValue' | 'onSubmit' | 'children'> & ThemeProps<'Editable'> & UseEditableProps & EditableOptions;
47
+ declare const Editable: _yamada_ui_core.Component<"div", EditableProps>;
48
+ type EditablePreviewProps = HTMLUIProps<'span'>;
49
+ declare const EditablePreview: _yamada_ui_core.Component<"span", EditablePreviewProps>;
50
+ type EditableInputProps = HTMLUIProps<'input'>;
51
+ declare const EditableInput: _yamada_ui_core.Component<"input", EditableInputProps>;
52
+ type EditableTextareaProps = HTMLUIProps<'textarea'>;
53
+ declare const EditableTextarea: _yamada_ui_core.Component<"textarea", EditableTextareaProps>;
54
+
55
+ export { Editable, EditableInput, EditableInputProps, EditablePreview, EditablePreviewProps, EditableProps, EditableTextarea, EditableTextareaProps, UseEditableProps, UseEditableReturn, useEditable, useEditableControl };