@gridui/inline-editor 1.0.5 → 2.0.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.
- package/README.md +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +176 -107
- package/dist/index.js.map +4 -4
- package/dist/inline-editor/inline-editor.d.ts +2 -12
- package/dist/inline-editor-actions/inline-editor-actions.d.ts +12 -0
- package/dist/inline-editor-display/inline-editor-display.d.ts +9 -0
- package/dist/inline-editor-field/inline-editor-field.d.ts +12 -0
- package/dist/inline-editor-trigger/inline-editor-trigger.d.ts +10 -0
- package/dist/shared/editor-context.d.ts +22 -0
- package/dist/styles.css +1 -1
- package/docs/catalog.json +41 -1
- package/docs/inline-editor.md +22 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @gridui/inline-editor
|
|
2
2
|
|
|
3
|
-
Core family. Public API: `InlineEditor`, `
|
|
3
|
+
Core family. Public API: `InlineEditor`, `InlineEditorDisplay`, `InlineEditorTrigger`, `InlineEditorField`, and `InlineEditorActions`.
|
|
4
4
|
|
|
5
5
|
Import components from `@gridui/inline-editor` and load `@gridui/inline-editor/styles.css` after Theme.
|
|
6
6
|
Load styles for any composed dependency used by this family. Applications own routes, labels, state persistence, and branding.
|
package/dist/index.d.ts
CHANGED
|
@@ -6,3 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export type { InlineEditorProps } from "./inline-editor/inline-editor";
|
|
8
8
|
export { InlineEditor } from "./inline-editor/inline-editor";
|
|
9
|
+
export type { InlineEditorActionsProps } from "./inline-editor-actions/inline-editor-actions";
|
|
10
|
+
export { InlineEditorActions } from "./inline-editor-actions/inline-editor-actions";
|
|
11
|
+
export type { InlineEditorDisplayProps } from "./inline-editor-display/inline-editor-display";
|
|
12
|
+
export { InlineEditorDisplay } from "./inline-editor-display/inline-editor-display";
|
|
13
|
+
export type { InlineEditorFieldProps } from "./inline-editor-field/inline-editor-field";
|
|
14
|
+
export { InlineEditorField } from "./inline-editor-field/inline-editor-field";
|
|
15
|
+
export type { InlineEditorTriggerProps } from "./inline-editor-trigger/inline-editor-trigger";
|
|
16
|
+
export { InlineEditorTrigger } from "./inline-editor-trigger/inline-editor-trigger";
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
// src/shared/editor-context.ts
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
var editorContext = createContext(null);
|
|
4
|
+
var useEditor = () => {
|
|
5
|
+
const editor = useContext(editorContext);
|
|
6
|
+
if (!editor) {
|
|
7
|
+
throw new Error("InlineEditor parts require an InlineEditor.");
|
|
8
|
+
}
|
|
9
|
+
return editor;
|
|
10
|
+
};
|
|
11
|
+
|
|
1
12
|
// src/inline-editor/inline-editor.tsx
|
|
2
|
-
import { Button } from "@gridui/button";
|
|
3
|
-
import { Input } from "@gridui/input";
|
|
4
|
-
import { isComposingKeyEvent } from "@gridui/interaction";
|
|
5
13
|
import { cn } from "@gridui/style-utils";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
14
|
+
import { useLayoutEffect, useRef } from "react";
|
|
15
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
16
|
var InlineEditor = (props) => {
|
|
9
17
|
const {
|
|
10
18
|
editing,
|
|
@@ -16,14 +24,7 @@ var InlineEditor = (props) => {
|
|
|
16
24
|
busy = false,
|
|
17
25
|
disabled = false,
|
|
18
26
|
submitDisabled = false,
|
|
19
|
-
|
|
20
|
-
label,
|
|
21
|
-
editLabel,
|
|
22
|
-
saveLabel,
|
|
23
|
-
cancelLabel,
|
|
24
|
-
feedback,
|
|
25
|
-
editButtonProps,
|
|
26
|
-
inputProps,
|
|
27
|
+
children,
|
|
27
28
|
className,
|
|
28
29
|
...restProps
|
|
29
30
|
} = props;
|
|
@@ -31,14 +32,6 @@ var InlineEditor = (props) => {
|
|
|
31
32
|
const trigger = useRef(null);
|
|
32
33
|
const previousEditing = useRef(false);
|
|
33
34
|
const restoreFocus = useRef(false);
|
|
34
|
-
const generatedId = useId();
|
|
35
|
-
const inputId = inputProps?.id ?? generatedId;
|
|
36
|
-
useImperativeHandle(inputProps?.ref, () => {
|
|
37
|
-
return input.current;
|
|
38
|
-
});
|
|
39
|
-
useImperativeHandle(editButtonProps?.ref, () => {
|
|
40
|
-
return trigger.current;
|
|
41
|
-
});
|
|
42
35
|
useLayoutEffect(() => {
|
|
43
36
|
if (editing && !previousEditing.current) {
|
|
44
37
|
restoreFocus.current = false;
|
|
@@ -70,106 +63,182 @@ var InlineEditor = (props) => {
|
|
|
70
63
|
}
|
|
71
64
|
onSubmit(value);
|
|
72
65
|
};
|
|
73
|
-
const editAppearance = editButtonProps && editButtonProps.variant !== void 0 && editButtonProps.variant !== null ? editButtonProps : {
|
|
74
|
-
...editButtonProps,
|
|
75
|
-
variant: "quiet"
|
|
76
|
-
};
|
|
77
66
|
return /* @__PURE__ */ jsx(
|
|
67
|
+
editorContext.Provider,
|
|
68
|
+
{
|
|
69
|
+
value: {
|
|
70
|
+
editing,
|
|
71
|
+
busy,
|
|
72
|
+
disabled,
|
|
73
|
+
submitDisabled,
|
|
74
|
+
value,
|
|
75
|
+
input,
|
|
76
|
+
trigger,
|
|
77
|
+
onValueChange,
|
|
78
|
+
cancel,
|
|
79
|
+
submit,
|
|
80
|
+
start: () => {
|
|
81
|
+
onEditingChange(true);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
children: /* @__PURE__ */ jsx(
|
|
85
|
+
"div",
|
|
86
|
+
{
|
|
87
|
+
...restProps,
|
|
88
|
+
className: cn("gridui-inline-editor", className),
|
|
89
|
+
"aria-busy": busy || void 0,
|
|
90
|
+
children
|
|
91
|
+
}
|
|
92
|
+
)
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
// src/inline-editor-actions/inline-editor-actions.tsx
|
|
98
|
+
import { Button } from "@gridui/button";
|
|
99
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
100
|
+
var InlineEditorActions = (props) => {
|
|
101
|
+
const { busy, cancel, disabled, editing, submit, submitDisabled } = useEditor();
|
|
102
|
+
if (!editing) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return /* @__PURE__ */ jsxs(
|
|
78
106
|
"div",
|
|
79
107
|
{
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/* @__PURE__ */ jsx(
|
|
85
|
-
"label",
|
|
86
|
-
{
|
|
87
|
-
htmlFor: inputId,
|
|
88
|
-
children: label
|
|
89
|
-
}
|
|
90
|
-
),
|
|
91
|
-
/* @__PURE__ */ jsx(
|
|
92
|
-
Input,
|
|
93
|
-
{
|
|
94
|
-
...inputProps,
|
|
95
|
-
id: inputId,
|
|
96
|
-
ref: input,
|
|
97
|
-
value,
|
|
98
|
-
onValueChange,
|
|
99
|
-
disabled: disabled || busy,
|
|
100
|
-
onKeyDown: (event) => {
|
|
101
|
-
if (isComposingKeyEvent(event.nativeEvent) || busy || disabled) {
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
if (event.key === "Escape") {
|
|
105
|
-
event.preventDefault();
|
|
106
|
-
event.stopPropagation();
|
|
107
|
-
cancel();
|
|
108
|
-
} else if (event.key === "Enter") {
|
|
109
|
-
event.preventDefault();
|
|
110
|
-
submit();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
),
|
|
115
|
-
/* @__PURE__ */ jsxs(
|
|
116
|
-
"div",
|
|
117
|
-
{
|
|
118
|
-
className: "gridui-inline-editor-actions",
|
|
119
|
-
children: [
|
|
120
|
-
/* @__PURE__ */ jsx(
|
|
121
|
-
Button,
|
|
122
|
-
{
|
|
123
|
-
type: "button",
|
|
124
|
-
size: "small",
|
|
125
|
-
disabled: disabled || busy || submitDisabled,
|
|
126
|
-
loading: busy,
|
|
127
|
-
onClick: submit,
|
|
128
|
-
children: saveLabel
|
|
129
|
-
}
|
|
130
|
-
),
|
|
131
|
-
/* @__PURE__ */ jsx(
|
|
132
|
-
Button,
|
|
133
|
-
{
|
|
134
|
-
type: "button",
|
|
135
|
-
size: "small",
|
|
136
|
-
variant: "quiet",
|
|
137
|
-
disabled: busy,
|
|
138
|
-
onClick: cancel,
|
|
139
|
-
children: cancelLabel
|
|
140
|
-
}
|
|
141
|
-
)
|
|
142
|
-
]
|
|
143
|
-
}
|
|
144
|
-
),
|
|
145
|
-
feedback
|
|
146
|
-
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
147
|
-
display !== null && display !== void 0 && /* @__PURE__ */ jsx(
|
|
148
|
-
"div",
|
|
108
|
+
className: "gridui-inline-editor-actions",
|
|
109
|
+
children: [
|
|
110
|
+
/* @__PURE__ */ jsx2(
|
|
111
|
+
Button,
|
|
149
112
|
{
|
|
150
|
-
|
|
151
|
-
|
|
113
|
+
type: "button",
|
|
114
|
+
size: "small",
|
|
115
|
+
disabled: disabled || busy || submitDisabled,
|
|
116
|
+
loading: busy,
|
|
117
|
+
onClick: submit,
|
|
118
|
+
children: props.saveLabel
|
|
152
119
|
}
|
|
153
120
|
),
|
|
154
|
-
/* @__PURE__ */
|
|
121
|
+
/* @__PURE__ */ jsx2(
|
|
155
122
|
Button,
|
|
156
123
|
{
|
|
157
|
-
...editAppearance,
|
|
158
124
|
type: "button",
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
disabled:
|
|
162
|
-
onClick:
|
|
163
|
-
|
|
164
|
-
},
|
|
165
|
-
children: editLabel
|
|
125
|
+
size: "small",
|
|
126
|
+
variant: "quiet",
|
|
127
|
+
disabled: busy,
|
|
128
|
+
onClick: cancel,
|
|
129
|
+
children: props.cancelLabel
|
|
166
130
|
}
|
|
167
131
|
)
|
|
168
|
-
]
|
|
132
|
+
]
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// src/inline-editor-display/inline-editor-display.tsx
|
|
138
|
+
import { cn as cn2 } from "@gridui/style-utils";
|
|
139
|
+
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
140
|
+
var InlineEditorDisplay = (props) => {
|
|
141
|
+
const { editing } = useEditor();
|
|
142
|
+
const { children, className, ...restProps } = props;
|
|
143
|
+
if (editing || children === void 0 || children === null) {
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return /* @__PURE__ */ jsx3(
|
|
147
|
+
"div",
|
|
148
|
+
{
|
|
149
|
+
...restProps,
|
|
150
|
+
className: cn2("gridui-inline-editor-display", className),
|
|
151
|
+
children
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/inline-editor-field/inline-editor-field.tsx
|
|
157
|
+
import { Input } from "@gridui/input";
|
|
158
|
+
import { isComposingKeyEvent } from "@gridui/interaction";
|
|
159
|
+
import { useId, useImperativeHandle } from "react";
|
|
160
|
+
import { Fragment, jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
161
|
+
var InlineEditorField = (props) => {
|
|
162
|
+
const { id, label, onKeyDown, ref, ...restProps } = props;
|
|
163
|
+
const { busy, cancel, disabled, editing, input, onValueChange, submit, value } = useEditor();
|
|
164
|
+
const generatedId = useId();
|
|
165
|
+
const inputId = id ?? generatedId;
|
|
166
|
+
useImperativeHandle(ref, () => {
|
|
167
|
+
return input.current;
|
|
168
|
+
});
|
|
169
|
+
if (!editing) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return /* @__PURE__ */ jsxs2(Fragment, { children: [
|
|
173
|
+
/* @__PURE__ */ jsx4(
|
|
174
|
+
"label",
|
|
175
|
+
{
|
|
176
|
+
htmlFor: inputId,
|
|
177
|
+
children: label
|
|
178
|
+
}
|
|
179
|
+
),
|
|
180
|
+
/* @__PURE__ */ jsx4(
|
|
181
|
+
Input,
|
|
182
|
+
{
|
|
183
|
+
...restProps,
|
|
184
|
+
id: inputId,
|
|
185
|
+
ref: input,
|
|
186
|
+
value,
|
|
187
|
+
onValueChange,
|
|
188
|
+
disabled: disabled || busy,
|
|
189
|
+
onKeyDown: (event) => {
|
|
190
|
+
onKeyDown?.(event);
|
|
191
|
+
if (event.defaultPrevented || isComposingKeyEvent(event.nativeEvent) || busy || disabled) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
if (event.key === "Escape") {
|
|
195
|
+
event.preventDefault();
|
|
196
|
+
event.stopPropagation();
|
|
197
|
+
cancel();
|
|
198
|
+
} else if (event.key === "Enter") {
|
|
199
|
+
event.preventDefault();
|
|
200
|
+
submit();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
)
|
|
205
|
+
] });
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
// src/inline-editor-trigger/inline-editor-trigger.tsx
|
|
209
|
+
import { Button as Button2 } from "@gridui/button";
|
|
210
|
+
import { useImperativeHandle as useImperativeHandle2 } from "react";
|
|
211
|
+
import { jsx as jsx5 } from "react/jsx-runtime";
|
|
212
|
+
var InlineEditorTrigger = (props) => {
|
|
213
|
+
const appearance = props.variant === void 0 || props.variant === null ? {
|
|
214
|
+
...props,
|
|
215
|
+
variant: "quiet"
|
|
216
|
+
} : props;
|
|
217
|
+
const { ref, size, ...restProps } = appearance;
|
|
218
|
+
const { busy, disabled, editing, start, trigger } = useEditor();
|
|
219
|
+
useImperativeHandle2(ref, () => {
|
|
220
|
+
return trigger.current;
|
|
221
|
+
});
|
|
222
|
+
if (editing) {
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
return /* @__PURE__ */ jsx5(
|
|
226
|
+
Button2,
|
|
227
|
+
{
|
|
228
|
+
...restProps,
|
|
229
|
+
size: size ?? "small",
|
|
230
|
+
ref: trigger,
|
|
231
|
+
type: "button",
|
|
232
|
+
disabled: disabled || busy,
|
|
233
|
+
onClick: start
|
|
169
234
|
}
|
|
170
235
|
);
|
|
171
236
|
};
|
|
172
237
|
export {
|
|
173
|
-
InlineEditor
|
|
238
|
+
InlineEditor,
|
|
239
|
+
InlineEditorActions,
|
|
240
|
+
InlineEditorDisplay,
|
|
241
|
+
InlineEditorField,
|
|
242
|
+
InlineEditorTrigger
|
|
174
243
|
};
|
|
175
244
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/inline-editor/inline-editor.tsx"],
|
|
4
|
-
"sourcesContent": ["/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace
|
|
5
|
-
"mappings": ";AAOA,SAAS,
|
|
6
|
-
"names": []
|
|
3
|
+
"sources": ["../src/shared/editor-context.ts", "../src/inline-editor/inline-editor.tsx", "../src/inline-editor-actions/inline-editor-actions.tsx", "../src/inline-editor-display/inline-editor-display.tsx", "../src/inline-editor-field/inline-editor-field.tsx", "../src/inline-editor-trigger/inline-editor-trigger.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace Shared\n * @description Editor Context\n */\n\nimport { createContext, type RefObject, useContext } from \"react\";\n\nexport type EditorContext = {\n readonly editing: boolean;\n readonly busy: boolean;\n readonly disabled: boolean;\n readonly submitDisabled: boolean;\n readonly value: string;\n readonly input: RefObject<HTMLInputElement | null>;\n readonly trigger: RefObject<HTMLButtonElement | null>;\n readonly onValueChange: (value: string) => void;\n readonly start: () => void;\n readonly cancel: () => void;\n readonly submit: () => void;\n};\n\nexport const editorContext = createContext<EditorContext | null>(null);\n\nexport const useEditor = (): EditorContext => {\n const editor = useContext(editorContext);\n if (!editor) {\n throw new Error(\"InlineEditor parts require an InlineEditor.\");\n }\n return editor;\n};\n", "/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace InlineEditor\n * @description Inline Editor\n */\n\nimport { editorContext } from \"../shared/editor-context\";\nimport { cn } from \"@gridui/style-utils\";\nimport { type ComponentPropsWithRef, type FC, useLayoutEffect, useRef } from \"react\";\n\nexport type InlineEditorProps = Omit<ComponentPropsWithRef<\"div\">, \"onSubmit\" | \"onChange\"> & {\n readonly editing: boolean;\n readonly onEditingChange: (editing: boolean) => void;\n readonly value: string;\n readonly onValueChange: (value: string) => void;\n readonly onSubmit: (value: string) => void;\n readonly onCancel?: () => void;\n readonly busy?: boolean;\n readonly disabled?: boolean;\n readonly submitDisabled?: boolean;\n};\n\nexport const InlineEditor: FC<InlineEditorProps> = (props: InlineEditorProps) => {\n const { editing, onEditingChange, value, onValueChange, onSubmit, onCancel,\n busy = false, disabled = false, submitDisabled = false, children, className, ...restProps } = props;\n const input = useRef<HTMLInputElement>(null);\n const trigger = useRef<HTMLButtonElement>(null);\n const previousEditing = useRef(false);\n const restoreFocus = useRef(false);\n useLayoutEffect(() => {\n if (editing && !previousEditing.current) {\n restoreFocus.current = false;\n input.current?.focus();\n } else if (!editing && previousEditing.current) {\n restoreFocus.current = true;\n }\n if (!editing && restoreFocus.current && !busy && !disabled) {\n trigger.current?.focus();\n restoreFocus.current = false;\n }\n previousEditing.current = editing;\n }, [\n editing,\n busy,\n disabled,\n ]);\n const cancel = () => {\n onCancel?.();\n onEditingChange(false);\n };\n const submit = () => {\n const element = input.current;\n if (busy || disabled || submitDisabled || !element || element.matches(\":disabled\")) {\n return;\n }\n if (!element.reportValidity()) {\n return;\n }\n onSubmit(value);\n };\n\n return (<editorContext.Provider\n value={{\n editing,\n busy,\n disabled,\n submitDisabled,\n value,\n input,\n trigger,\n onValueChange,\n cancel,\n submit,\n start: () => {\n onEditingChange(true);\n },\n }}\n >\n <div\n {...restProps}\n className={cn(\"gridui-inline-editor\", className)}\n aria-busy={busy || undefined}\n >\n {children}\n </div>\n </editorContext.Provider>);\n};\n", "/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace InlineEditorActions\n * @description Inline Editor Actions\n */\n\nimport { useEditor } from \"../shared/editor-context\";\nimport { Button } from \"@gridui/button\";\nimport type { FC, ReactNode } from \"react\";\n\nexport type InlineEditorActionsProps = {\n readonly saveLabel: ReactNode;\n readonly cancelLabel: ReactNode;\n};\n\nexport const InlineEditorActions: FC<InlineEditorActionsProps> = (props: InlineEditorActionsProps) => {\n const { busy, cancel, disabled, editing, submit, submitDisabled } = useEditor();\n if (!editing) {\n return null;\n }\n return (<div\n className=\"gridui-inline-editor-actions\"\n >\n <Button\n type=\"button\"\n size=\"small\"\n disabled={disabled || busy || submitDisabled}\n loading={busy}\n onClick={submit}\n >\n {props.saveLabel}\n </Button>\n <Button\n type=\"button\"\n size=\"small\"\n variant=\"quiet\"\n disabled={busy}\n onClick={cancel}\n >\n {props.cancelLabel}\n </Button>\n </div>);\n};\n", "/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace InlineEditorDisplay\n * @description Inline Editor Display\n */\n\nimport { useEditor } from \"../shared/editor-context\";\nimport { cn } from \"@gridui/style-utils\";\nimport type { ComponentPropsWithRef, FC } from \"react\";\n\nexport type InlineEditorDisplayProps = ComponentPropsWithRef<\"div\">;\n\nexport const InlineEditorDisplay: FC<InlineEditorDisplayProps> = (props: InlineEditorDisplayProps) => {\n const { editing } = useEditor();\n const { children, className, ...restProps } = props;\n if (editing || children === undefined || children === null) {\n return null;\n }\n return (<div\n {...restProps}\n className={cn(\"gridui-inline-editor-display\", className)}\n >\n {children}\n </div>);\n};\n", "/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace InlineEditorField\n * @description Inline Editor Field\n */\n\nimport { useEditor } from \"../shared/editor-context\";\nimport { Input, type InputProps } from \"@gridui/input\";\nimport { isComposingKeyEvent } from \"@gridui/interaction\";\nimport { type FC, useId, useImperativeHandle } from \"react\";\n\nexport type InlineEditorFieldProps = Omit<InputProps, \"value\" | \"defaultValue\" | \"onValueChange\" | \"disabled\"> & {\n readonly label: string;\n};\n\nexport const InlineEditorField: FC<InlineEditorFieldProps> = (props: InlineEditorFieldProps) => {\n const { id, label, onKeyDown, ref, ...restProps } = props;\n const { busy, cancel, disabled, editing, input, onValueChange, submit, value } = useEditor();\n const generatedId = useId();\n const inputId = id ?? generatedId;\n useImperativeHandle(ref, () => {\n return input.current!;\n });\n if (!editing) {\n return null;\n }\n return (<>\n <label\n htmlFor={inputId}\n >\n {label}\n </label>\n <Input\n {...restProps}\n id={inputId}\n ref={input}\n value={value}\n onValueChange={onValueChange}\n disabled={disabled || busy}\n onKeyDown={event => {\n onKeyDown?.(event);\n if (event.defaultPrevented || isComposingKeyEvent(event.nativeEvent) || busy || disabled) {\n return;\n }\n if (event.key === \"Escape\") {\n event.preventDefault();\n event.stopPropagation();\n cancel();\n } else if (event.key === \"Enter\") {\n event.preventDefault();\n submit();\n }\n }}\n />\n </>);\n};\n", "/**\n * @author WMXPY\n * @package Inline Editor\n * @namespace InlineEditorTrigger\n * @description Inline Editor Trigger\n */\n\nimport { useEditor } from \"../shared/editor-context\";\nimport { Button, type ButtonProps, type ButtonVariants } from \"@gridui/button\";\nimport { type FC, useImperativeHandle } from \"react\";\n\nexport type InlineEditorTriggerProps = Omit<ButtonProps, \"disabled\" | \"loading\" | \"onClick\" | \"type\"> & ButtonVariants;\n\nexport const InlineEditorTrigger: FC<InlineEditorTriggerProps> = (props: InlineEditorTriggerProps) => {\n const appearance = props.variant === undefined || props.variant === null ? {\n ...props,\n variant: \"quiet\" as const,\n } : props;\n const { ref, size, ...restProps } = appearance;\n const { busy, disabled, editing, start, trigger } = useEditor();\n useImperativeHandle(ref, () => {\n return trigger.current!;\n });\n if (editing) {\n return null;\n }\n return (<Button\n {...restProps}\n size={size ?? \"small\"}\n ref={trigger}\n type=\"button\"\n disabled={disabled || busy}\n onClick={start}\n />);\n};\n"],
|
|
5
|
+
"mappings": ";AAOA,SAAS,eAA+B,kBAAkB;AAgBnD,IAAM,gBAAgB,cAAoC,IAAI;AAE9D,IAAM,YAAY,MAAqB;AAC1C,QAAM,SAAS,WAAW,aAAa;AACvC,MAAI,CAAC,QAAQ;AACT,UAAM,IAAI,MAAM,6CAA6C;AAAA,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O;AAAA,IACvE,GAAG;AAAA,IACH,SAAS;AAAA,EACb,IAAI;AACJ,QAAM,EAAE,KAAK,MAAM,GAAG,UAAU,IAAI;AACpC,QAAM,EAAE,MAAM,UAAU,SAAS,OAAO,QAAQ,IAAI,UAAU;AAC9D,EAAAD,qBAAoB,KAAK,MAAM;AAC3B,WAAO,QAAQ;AAAA,EACnB,CAAC;AACD,MAAI,SAAS;AACT,WAAO;AAAA,EACX;AACA,SAAQ,gBAAAC;AAAA,IAACF;AAAA,IAAA;AAAA,MACJ,GAAG;AAAA,MACJ,MAAM,QAAQ;AAAA,MACd,KAAK;AAAA,MACL,MAAK;AAAA,MACL,UAAU,YAAY;AAAA,MACtB,SAAS;AAAA;AAAA,EACb;AACJ;",
|
|
6
|
+
"names": ["jsx", "cn", "jsx", "jsx", "jsxs", "Button", "useImperativeHandle", "jsx"]
|
|
7
7
|
}
|
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
* @namespace InlineEditor
|
|
5
5
|
* @description Inline Editor
|
|
6
6
|
*/
|
|
7
|
-
import { type
|
|
8
|
-
|
|
9
|
-
import { type ComponentPropsWithRef, type FC, type ReactNode } from "react";
|
|
10
|
-
export type InlineEditorProps = Omit<ComponentPropsWithRef<"div">, "children" | "onSubmit" | "onChange"> & {
|
|
7
|
+
import { type ComponentPropsWithRef, type FC } from "react";
|
|
8
|
+
export type InlineEditorProps = Omit<ComponentPropsWithRef<"div">, "onSubmit" | "onChange"> & {
|
|
11
9
|
readonly editing: boolean;
|
|
12
10
|
readonly onEditingChange: (editing: boolean) => void;
|
|
13
11
|
readonly value: string;
|
|
@@ -17,13 +15,5 @@ export type InlineEditorProps = Omit<ComponentPropsWithRef<"div">, "children" |
|
|
|
17
15
|
readonly busy?: boolean;
|
|
18
16
|
readonly disabled?: boolean;
|
|
19
17
|
readonly submitDisabled?: boolean;
|
|
20
|
-
readonly display: ReactNode;
|
|
21
|
-
readonly label: string;
|
|
22
|
-
readonly editLabel: string;
|
|
23
|
-
readonly saveLabel: string;
|
|
24
|
-
readonly cancelLabel: string;
|
|
25
|
-
readonly feedback?: ReactNode;
|
|
26
|
-
readonly editButtonProps?: Omit<ButtonProps, "children" | "disabled" | "loading" | "onClick" | "type"> & ButtonVariants;
|
|
27
|
-
readonly inputProps?: Omit<InputProps, "value" | "defaultValue" | "onValueChange" | "disabled" | "onKeyDown">;
|
|
28
18
|
};
|
|
29
19
|
export declare const InlineEditor: FC<InlineEditorProps>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author WMXPY
|
|
3
|
+
* @package Inline Editor
|
|
4
|
+
* @namespace InlineEditorActions
|
|
5
|
+
* @description Inline Editor Actions
|
|
6
|
+
*/
|
|
7
|
+
import type { FC, ReactNode } from "react";
|
|
8
|
+
export type InlineEditorActionsProps = {
|
|
9
|
+
readonly saveLabel: ReactNode;
|
|
10
|
+
readonly cancelLabel: ReactNode;
|
|
11
|
+
};
|
|
12
|
+
export declare const InlineEditorActions: FC<InlineEditorActionsProps>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author WMXPY
|
|
3
|
+
* @package Inline Editor
|
|
4
|
+
* @namespace InlineEditorDisplay
|
|
5
|
+
* @description Inline Editor Display
|
|
6
|
+
*/
|
|
7
|
+
import type { ComponentPropsWithRef, FC } from "react";
|
|
8
|
+
export type InlineEditorDisplayProps = ComponentPropsWithRef<"div">;
|
|
9
|
+
export declare const InlineEditorDisplay: FC<InlineEditorDisplayProps>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author WMXPY
|
|
3
|
+
* @package Inline Editor
|
|
4
|
+
* @namespace InlineEditorField
|
|
5
|
+
* @description Inline Editor Field
|
|
6
|
+
*/
|
|
7
|
+
import { type InputProps } from "@gridui/input";
|
|
8
|
+
import { type FC } from "react";
|
|
9
|
+
export type InlineEditorFieldProps = Omit<InputProps, "value" | "defaultValue" | "onValueChange" | "disabled"> & {
|
|
10
|
+
readonly label: string;
|
|
11
|
+
};
|
|
12
|
+
export declare const InlineEditorField: FC<InlineEditorFieldProps>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author WMXPY
|
|
3
|
+
* @package Inline Editor
|
|
4
|
+
* @namespace InlineEditorTrigger
|
|
5
|
+
* @description Inline Editor Trigger
|
|
6
|
+
*/
|
|
7
|
+
import { type ButtonProps, type ButtonVariants } from "@gridui/button";
|
|
8
|
+
import { type FC } from "react";
|
|
9
|
+
export type InlineEditorTriggerProps = Omit<ButtonProps, "disabled" | "loading" | "onClick" | "type"> & ButtonVariants;
|
|
10
|
+
export declare const InlineEditorTrigger: FC<InlineEditorTriggerProps>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author WMXPY
|
|
3
|
+
* @package Inline Editor
|
|
4
|
+
* @namespace Shared
|
|
5
|
+
* @description Editor Context
|
|
6
|
+
*/
|
|
7
|
+
import { type RefObject } from "react";
|
|
8
|
+
export type EditorContext = {
|
|
9
|
+
readonly editing: boolean;
|
|
10
|
+
readonly busy: boolean;
|
|
11
|
+
readonly disabled: boolean;
|
|
12
|
+
readonly submitDisabled: boolean;
|
|
13
|
+
readonly value: string;
|
|
14
|
+
readonly input: RefObject<HTMLInputElement | null>;
|
|
15
|
+
readonly trigger: RefObject<HTMLButtonElement | null>;
|
|
16
|
+
readonly onValueChange: (value: string) => void;
|
|
17
|
+
readonly start: () => void;
|
|
18
|
+
readonly cancel: () => void;
|
|
19
|
+
readonly submit: () => void;
|
|
20
|
+
};
|
|
21
|
+
export declare const editorContext: import("react").Context<EditorContext | null>;
|
|
22
|
+
export declare const useEditor: () => EditorContext;
|
package/dist/styles.css
CHANGED
package/docs/catalog.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
3
|
"name": "@gridui/inline-editor",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"category": "core",
|
|
6
6
|
"description": "GridUI React inline editor",
|
|
7
7
|
"usage": "GridUI React inline editor",
|
|
@@ -15,10 +15,50 @@
|
|
|
15
15
|
"kind": "component",
|
|
16
16
|
"importPath": "@gridui/inline-editor"
|
|
17
17
|
},
|
|
18
|
+
{
|
|
19
|
+
"name": "InlineEditorActions",
|
|
20
|
+
"kind": "component",
|
|
21
|
+
"importPath": "@gridui/inline-editor"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"name": "InlineEditorActionsProps",
|
|
25
|
+
"kind": "type",
|
|
26
|
+
"importPath": "@gridui/inline-editor"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"name": "InlineEditorDisplay",
|
|
30
|
+
"kind": "component",
|
|
31
|
+
"importPath": "@gridui/inline-editor"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "InlineEditorDisplayProps",
|
|
35
|
+
"kind": "type",
|
|
36
|
+
"importPath": "@gridui/inline-editor"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"name": "InlineEditorField",
|
|
40
|
+
"kind": "component",
|
|
41
|
+
"importPath": "@gridui/inline-editor"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
"name": "InlineEditorFieldProps",
|
|
45
|
+
"kind": "type",
|
|
46
|
+
"importPath": "@gridui/inline-editor"
|
|
47
|
+
},
|
|
18
48
|
{
|
|
19
49
|
"name": "InlineEditorProps",
|
|
20
50
|
"kind": "type",
|
|
21
51
|
"importPath": "@gridui/inline-editor"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "InlineEditorTrigger",
|
|
55
|
+
"kind": "component",
|
|
56
|
+
"importPath": "@gridui/inline-editor"
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"name": "InlineEditorTriggerProps",
|
|
60
|
+
"kind": "type",
|
|
61
|
+
"importPath": "@gridui/inline-editor"
|
|
22
62
|
}
|
|
23
63
|
],
|
|
24
64
|
"styles": [
|
package/docs/inline-editor.md
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
# InlineEditor
|
|
2
2
|
|
|
3
|
-
Import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
This is a controlled single-line editing composition. The caller supplies
|
|
7
|
-
`editing` / `onEditingChange`, draft `value` / `onValueChange`, `display`, field
|
|
8
|
-
`label`, and `editLabel`, `saveLabel`, `cancelLabel`. View mode mounts only display
|
|
9
|
-
content and the edit button; the draft field is not mounted.
|
|
10
|
-
Passing `display={null}` omits the display region without reserving empty space.
|
|
3
|
+
Import the editor and its parts from `@gridui/inline-editor`, and load its
|
|
4
|
+
stylesheet after Theme and its rendered dependencies.
|
|
11
5
|
|
|
12
6
|
```tsx
|
|
13
7
|
<InlineEditor editing={editing} onEditingChange={setEditing}
|
|
14
|
-
value={draft} onValueChange={setDraft}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
value={draft} onValueChange={setDraft}
|
|
9
|
+
busy={saving} onSubmit={saveDraft} onCancel={restoreSavedTitle}>
|
|
10
|
+
<InlineEditorDisplay>{savedTitle}</InlineEditorDisplay>
|
|
11
|
+
<InlineEditorTrigger>Edit</InlineEditorTrigger>
|
|
12
|
+
<InlineEditorField label="Title" required />
|
|
13
|
+
<InlineEditorActions saveLabel="Save" cancelLabel="Cancel" />
|
|
14
|
+
{editing && feedback}
|
|
15
|
+
</InlineEditor>
|
|
18
16
|
```
|
|
19
17
|
|
|
18
|
+
The caller owns the draft and editing state. The display and trigger mount in
|
|
19
|
+
view mode. The field and actions mount in edit mode. Supply one trigger and
|
|
20
|
+
one field per editor so keyboard actions and focus can target them.
|
|
21
|
+
|
|
20
22
|
Entering edit mode focuses the input; leaving returns focus to the edit button.
|
|
21
23
|
Enter requests `onSubmit(value)` without changing editing state. Escape (outside
|
|
22
24
|
IME composition) and Cancel call `onCancel`, then request `onEditingChange(false)`.
|
|
@@ -28,8 +30,7 @@ enabled again.
|
|
|
28
30
|
and submitting, but an existing edit can still be cancelled with its Cancel
|
|
29
31
|
button. The caller owns async execution, duplicate-request protection, error
|
|
30
32
|
handling and draft clearing. Close only after successful save by setting
|
|
31
|
-
`editing=false`; on failure retain the value, clear busy, and
|
|
32
|
-
`feedback`. No promise is awaited and no errors or input values are logged.
|
|
33
|
+
`editing=false`; on failure retain the value, clear busy, and render feedback while editing. No promise is awaited and no errors or input values are logged.
|
|
33
34
|
|
|
34
35
|
`submitDisabled` disables only Save and blocks Enter submission while keeping the
|
|
35
36
|
input and Cancel usable. Both submission paths call the input's native
|
|
@@ -37,12 +38,10 @@ input and Cancel usable. Both submission paths call the input's native
|
|
|
37
38
|
constraints. Inherited fieldset disabledness also blocks submission. Application
|
|
38
39
|
validation and authorization still run inside `onSubmit`.
|
|
39
40
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
are explicitly non-submit buttons, so it can be composed inside a larger form.
|
|
48
|
-
Applications own validation and authorization before accepting a submit request.
|
|
41
|
+
Pass appearance, accessible naming and the native button ref directly to
|
|
42
|
+
`InlineEditorTrigger`. Pass native constraints, autocomplete, a native input
|
|
43
|
+
ref, styling and accessible descriptions directly to `InlineEditorField`. Its
|
|
44
|
+
key handler can prevent the default editor shortcut. Native div props and the
|
|
45
|
+
outer ref go to the root. The editor uses non-submit buttons and can sit inside
|
|
46
|
+
a larger form. Applications own validation and authorization before accepting a
|
|
47
|
+
submit request.
|