proje-react-panel 1.8.0 → 1.9.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 -0
- package/dist/__tests__/components/form/FormGroups.test.d.ts +1 -0
- package/dist/__tests__/components/form/RichTextField.test.d.ts +4 -0
- package/dist/__tests__/optionalTiptap.test.d.ts +4 -0
- package/dist/components/form/RichTextField.d.ts +7 -0
- package/dist/decorators/form/Form.d.ts +8 -0
- package/dist/decorators/form/Input.d.ts +5 -1
- package/dist/decorators/form/inputs/RichTextInput.d.ts +13 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.esm.js +2 -2
- package/how_to.md +150 -32
- package/jest.config.js +1 -0
- package/package.json +13 -1
- package/src/__tests__/components/form/FormGroups.test.tsx +194 -0
- package/src/__tests__/components/form/RichTextField.test.tsx +181 -0
- package/src/__tests__/optionalTiptap.test.tsx +89 -0
- package/src/components/form/FormField.tsx +8 -1
- package/src/components/form/InnerForm.tsx +101 -19
- package/src/components/form/RichTextField.tsx +218 -0
- package/src/decorators/form/Form.ts +11 -0
- package/src/decorators/form/Input.ts +6 -1
- package/src/decorators/form/inputs/RichTextInput.ts +38 -0
- package/src/index.ts +8 -1
- package/src/styles/components/rich-text.scss +115 -0
- package/src/styles/form.scss +48 -0
- package/src/styles/index.scss +1 -0
- package/dist/components/DashboardContainer.d.ts +0 -7
- package/dist/components/DashboardGrid.d.ts +0 -9
- package/dist/components/DashboardItem.d.ts +0 -10
- package/dist/components/ThemeSwitcher.d.ts +0 -7
- package/dist/store/themeStore.d.ts +0 -23
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import 'reflect-metadata';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import { TextDecoder, TextEncoder } from 'util';
|
|
7
|
+
import { beforeAll, describe, expect, it, jest } from '@jest/globals';
|
|
8
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
9
|
+
import { FormProvider, useForm } from 'react-hook-form';
|
|
10
|
+
import { Input, getInputFields } from '../decorators/form/Input';
|
|
11
|
+
import { RichTextInput } from '../decorators/form/inputs/RichTextInput';
|
|
12
|
+
import { FormField } from '../components/form/FormField';
|
|
13
|
+
|
|
14
|
+
// @tiptap/* is declared optional in peerDependenciesMeta, so a panel that never uses a richtext
|
|
15
|
+
// field must work with the packages absent. Both are made unresolvable here; the flags record
|
|
16
|
+
// whether anything reached for them at all.
|
|
17
|
+
let reactRequested = false;
|
|
18
|
+
let starterKitRequested = false;
|
|
19
|
+
|
|
20
|
+
jest.mock('@tiptap/react', () => {
|
|
21
|
+
reactRequested = true;
|
|
22
|
+
throw new Error("Cannot find module '@tiptap/react'");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
jest.mock('@tiptap/starter-kit', () => {
|
|
26
|
+
starterKitRequested = true;
|
|
27
|
+
throw new Error("Cannot find module '@tiptap/starter-kit'");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
class PlainForm {
|
|
31
|
+
@Input({ label: 'Name' })
|
|
32
|
+
name: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class ArticleForm {
|
|
36
|
+
@RichTextInput({ label: 'Body' })
|
|
37
|
+
body: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
41
|
+
function Harness({ model }: { model: any }) {
|
|
42
|
+
const form = useForm({ defaultValues: { name: '', body: '' } });
|
|
43
|
+
const inputs = getInputFields(model);
|
|
44
|
+
return (
|
|
45
|
+
<FormProvider {...form}>
|
|
46
|
+
{inputs.map(input => (
|
|
47
|
+
<FormField key={input.name} input={input} register={form.register} />
|
|
48
|
+
))}
|
|
49
|
+
</FormProvider>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
describe('@tiptap as an optional peer dependency', () => {
|
|
54
|
+
beforeAll(() => {
|
|
55
|
+
// jsdom ships neither; react-router reads them at import time. Nothing to do with tiptap.
|
|
56
|
+
Object.assign(globalThis, { TextEncoder, TextDecoder });
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('imports the whole library without @tiptap installed', async () => {
|
|
60
|
+
const library = await import('../index');
|
|
61
|
+
|
|
62
|
+
expect(typeof library.FormPage).toBe('function');
|
|
63
|
+
expect(typeof library.RichTextInput).toBe('function');
|
|
64
|
+
expect(typeof library.ListPage).toBe('function');
|
|
65
|
+
expect(reactRequested).toBe(false);
|
|
66
|
+
expect(starterKitRequested).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('renders the other field types without @tiptap installed', () => {
|
|
70
|
+
render(<Harness model={PlainForm} />);
|
|
71
|
+
|
|
72
|
+
expect(screen.getByText('Name:')).toBeTruthy();
|
|
73
|
+
expect(reactRequested).toBe(false);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('tells the developer what to install when a richtext field is actually used', async () => {
|
|
77
|
+
const consoleError = jest.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
78
|
+
|
|
79
|
+
render(<Harness model={ArticleForm} />);
|
|
80
|
+
|
|
81
|
+
const message = await screen.findByRole('alert');
|
|
82
|
+
expect(message.textContent).toContain('@tiptap/react');
|
|
83
|
+
expect(message.textContent).toContain('@tiptap/starter-kit');
|
|
84
|
+
await waitFor(() => expect(reactRequested).toBe(true));
|
|
85
|
+
expect(consoleError).toHaveBeenCalled();
|
|
86
|
+
|
|
87
|
+
consoleError.mockRestore();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -6,6 +6,9 @@ import { Checkbox } from './Checkbox';
|
|
|
6
6
|
import { Label } from './Label';
|
|
7
7
|
import { Select } from './Select';
|
|
8
8
|
import { CustomField } from './CustomField';
|
|
9
|
+
//NOTE: safe to import statically — RichTextField pulls the optional @tiptap/* peers in itself,
|
|
10
|
+
// through a dynamic import, only once a richtext field is actually rendered.
|
|
11
|
+
import { RichTextField } from './RichTextField';
|
|
9
12
|
|
|
10
13
|
interface FormFieldProps {
|
|
11
14
|
input: InputConfiguration;
|
|
@@ -80,9 +83,13 @@ export function FormField({ input, register, error, baseName }: FormFieldProps)
|
|
|
80
83
|
const renderedField = useMemo(() => {
|
|
81
84
|
switch (input.type) {
|
|
82
85
|
case 'textarea':
|
|
83
|
-
return
|
|
86
|
+
return (
|
|
87
|
+
<textarea {...register(fieldName)} placeholder={input.placeholder} rows={input.rows} />
|
|
88
|
+
);
|
|
84
89
|
case 'select':
|
|
85
90
|
return <Select input={input} fieldName={fieldName} />;
|
|
91
|
+
case 'richtext':
|
|
92
|
+
return <RichTextField input={input} fieldName={fieldName} />;
|
|
86
93
|
case 'custom':
|
|
87
94
|
return <CustomField input={input} fieldName={fieldName} error={error?.message} />;
|
|
88
95
|
case 'input': {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import React, { useRef, useState } from 'react';
|
|
1
|
+
import React, { useMemo, useRef, useState } from 'react';
|
|
2
2
|
import { useFormContext } from 'react-hook-form';
|
|
3
3
|
import { InputConfiguration } from '../../decorators/form/Input';
|
|
4
4
|
import { FormField } from './FormField';
|
|
5
5
|
import { useNavigate } from 'react-router';
|
|
6
|
-
import { FormConfiguration } from '../../decorators/form/Form';
|
|
6
|
+
import { FormConfiguration, FormGroup } from '../../decorators/form/Form';
|
|
7
7
|
import { AnyClass } from '../../types/AnyClass';
|
|
8
8
|
import { toast } from 'react-toastify';
|
|
9
9
|
|
|
@@ -12,6 +12,63 @@ interface InnerFormProps<T extends AnyClass> {
|
|
|
12
12
|
formClass: FormConfiguration<T>;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
interface GroupSection {
|
|
16
|
+
key: string;
|
|
17
|
+
label: string;
|
|
18
|
+
collapsible: boolean;
|
|
19
|
+
defaultCollapsed: boolean;
|
|
20
|
+
inputs: InputConfiguration[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Splits the inputs into the fields rendered straight into the form and the grouped sections.
|
|
25
|
+
* With no `group` on any field every input stays ungrouped, so the markup is byte-for-byte what
|
|
26
|
+
* it was before groups existed — no stray fieldset around existing panels' CSS.
|
|
27
|
+
*/
|
|
28
|
+
function splitIntoGroups(
|
|
29
|
+
inputs: InputConfiguration[],
|
|
30
|
+
groups?: FormGroup[]
|
|
31
|
+
): { ungrouped: InputConfiguration[]; sections: GroupSection[] } {
|
|
32
|
+
const ungrouped: InputConfiguration[] = [];
|
|
33
|
+
const inputsByGroup = new Map<string, InputConfiguration[]>();
|
|
34
|
+
const appearanceOrder: string[] = [];
|
|
35
|
+
|
|
36
|
+
inputs?.forEach(input => {
|
|
37
|
+
// Hidden fields carry no label and no layout; a fieldset around them would only add noise.
|
|
38
|
+
if (!input.group || input.type === 'hidden') {
|
|
39
|
+
ungrouped.push(input);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!inputsByGroup.has(input.group)) {
|
|
43
|
+
inputsByGroup.set(input.group, []);
|
|
44
|
+
appearanceOrder.push(input.group);
|
|
45
|
+
}
|
|
46
|
+
inputsByGroup.get(input.group)!.push(input);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const declared = groups ?? [];
|
|
50
|
+
const declaredKeys = declared.map(group => group.key);
|
|
51
|
+
const orderedKeys = [
|
|
52
|
+
...declaredKeys.filter(key => inputsByGroup.has(key)),
|
|
53
|
+
// A group nobody declared in @Form({ groups }) still renders — labelled with its own key,
|
|
54
|
+
// appended after the declared ones, in the order the fields introduced it.
|
|
55
|
+
...appearanceOrder.filter(key => !declaredKeys.includes(key)),
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
const sections = orderedKeys.map(key => {
|
|
59
|
+
const declaration = declared.find(group => group.key === key);
|
|
60
|
+
return {
|
|
61
|
+
key,
|
|
62
|
+
label: declaration?.label ?? key,
|
|
63
|
+
collapsible: declaration?.collapsible ?? false,
|
|
64
|
+
defaultCollapsed: declaration?.defaultCollapsed ?? false,
|
|
65
|
+
inputs: inputsByGroup.get(key)!,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return { ungrouped, sections };
|
|
70
|
+
}
|
|
71
|
+
|
|
15
72
|
export function InnerForm<T extends AnyClass>({ inputs, formClass }: InnerFormProps<T>) {
|
|
16
73
|
const [errorMessage, setErrorMessage] = useState<string | null>(null);
|
|
17
74
|
//TODO: any is not a good solution, we need to find a better way to do this
|
|
@@ -19,6 +76,29 @@ export function InnerForm<T extends AnyClass>({ inputs, formClass }: InnerFormPr
|
|
|
19
76
|
const navigate = useNavigate();
|
|
20
77
|
const form = useFormContext<T>();
|
|
21
78
|
const loadingRef = useRef(false);
|
|
79
|
+
const { ungrouped, sections } = useMemo(
|
|
80
|
+
() => splitIntoGroups(inputs, formClass.groups),
|
|
81
|
+
[inputs, formClass.groups]
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const renderField = (input: InputConfiguration) => (
|
|
85
|
+
<FormField
|
|
86
|
+
key={input.name || ''}
|
|
87
|
+
input={input}
|
|
88
|
+
register={form.register}
|
|
89
|
+
error={
|
|
90
|
+
input.name
|
|
91
|
+
? {
|
|
92
|
+
message: (
|
|
93
|
+
form.formState.errors[input.name as keyof T] as {
|
|
94
|
+
message: string;
|
|
95
|
+
}
|
|
96
|
+
)?.message,
|
|
97
|
+
}
|
|
98
|
+
: undefined
|
|
99
|
+
}
|
|
100
|
+
/>
|
|
101
|
+
);
|
|
22
102
|
|
|
23
103
|
return (
|
|
24
104
|
<form
|
|
@@ -75,23 +155,25 @@ export function InnerForm<T extends AnyClass>({ inputs, formClass }: InnerFormPr
|
|
|
75
155
|
{errorMessage}
|
|
76
156
|
</div>
|
|
77
157
|
)}
|
|
78
|
-
{
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
158
|
+
{ungrouped.map(renderField)}
|
|
159
|
+
{sections.map(section => (
|
|
160
|
+
<fieldset
|
|
161
|
+
key={section.key}
|
|
162
|
+
data-group={section.key}
|
|
163
|
+
className={`form-group-section${section.collapsible ? ' form-group-section-collapsible' : ''}`}
|
|
164
|
+
>
|
|
165
|
+
{section.collapsible ? (
|
|
166
|
+
<details open={!section.defaultCollapsed}>
|
|
167
|
+
<summary className="form-group-summary">{section.label}</summary>
|
|
168
|
+
<div className="form-group-fields">{section.inputs.map(renderField)}</div>
|
|
169
|
+
</details>
|
|
170
|
+
) : (
|
|
171
|
+
<>
|
|
172
|
+
<legend className="form-group-legend">{section.label}</legend>
|
|
173
|
+
<div className="form-group-fields">{section.inputs.map(renderField)}</div>
|
|
174
|
+
</>
|
|
175
|
+
)}
|
|
176
|
+
</fieldset>
|
|
95
177
|
))}
|
|
96
178
|
<button type="submit" className="submit-button">
|
|
97
179
|
Submit
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { useController, useFormContext } from 'react-hook-form';
|
|
3
|
+
import type { Editor } from '@tiptap/react';
|
|
4
|
+
import { InputConfiguration } from '../../decorators/form/Input';
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_RICH_TEXT_TOOLBAR,
|
|
7
|
+
RichTextInputConfiguration,
|
|
8
|
+
RichTextToolbarItem,
|
|
9
|
+
} from '../../decorators/form/inputs/RichTextInput';
|
|
10
|
+
|
|
11
|
+
const DEFAULT_MIN_HEIGHT = 160;
|
|
12
|
+
|
|
13
|
+
export interface RichTextFieldProps {
|
|
14
|
+
input: InputConfiguration;
|
|
15
|
+
fieldName: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface Tiptap {
|
|
19
|
+
react: typeof import('@tiptap/react');
|
|
20
|
+
starterKit: (typeof import('@tiptap/starter-kit'))['default'];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//NOTE: @tiptap/* is an optional peer dependency, so it may only be reached through a dynamic
|
|
24
|
+
// import — a static one would put ~100kb (and a hard install requirement) on every panel,
|
|
25
|
+
// including the ones that never declare a richtext field. The promise is cached at module level
|
|
26
|
+
// so a form with several richtext fields loads the editor once.
|
|
27
|
+
let tiptapPromise: Promise<Tiptap> | null = null;
|
|
28
|
+
|
|
29
|
+
function loadTiptap(): Promise<Tiptap> {
|
|
30
|
+
if (!tiptapPromise) {
|
|
31
|
+
const pending = Promise.all([import('@tiptap/react'), import('@tiptap/starter-kit')]).then(
|
|
32
|
+
([react, starterKit]) => ({ react, starterKit: starterKit.default })
|
|
33
|
+
);
|
|
34
|
+
// A failed load is not cached: a chunk that failed to arrive should be retried on the next
|
|
35
|
+
// mount instead of leaving the field broken for the rest of the session.
|
|
36
|
+
pending.catch(() => {
|
|
37
|
+
tiptapPromise = null;
|
|
38
|
+
});
|
|
39
|
+
tiptapPromise = pending;
|
|
40
|
+
}
|
|
41
|
+
return tiptapPromise;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
interface ToolbarButton {
|
|
45
|
+
label: string;
|
|
46
|
+
title: string;
|
|
47
|
+
isActive: (editor: Editor) => boolean;
|
|
48
|
+
run: (editor: Editor) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const TOOLBAR_BUTTONS: Record<RichTextToolbarItem, ToolbarButton> = {
|
|
52
|
+
bold: {
|
|
53
|
+
label: 'B',
|
|
54
|
+
title: 'Bold',
|
|
55
|
+
isActive: editor => editor.isActive('bold'),
|
|
56
|
+
run: editor => editor.chain().focus().toggleBold().run(),
|
|
57
|
+
},
|
|
58
|
+
italic: {
|
|
59
|
+
label: 'I',
|
|
60
|
+
title: 'Italic',
|
|
61
|
+
isActive: editor => editor.isActive('italic'),
|
|
62
|
+
run: editor => editor.chain().focus().toggleItalic().run(),
|
|
63
|
+
},
|
|
64
|
+
heading: {
|
|
65
|
+
label: 'H2',
|
|
66
|
+
title: 'Heading',
|
|
67
|
+
isActive: editor => editor.isActive('heading', { level: 2 }),
|
|
68
|
+
run: editor => editor.chain().focus().toggleHeading({ level: 2 }).run(),
|
|
69
|
+
},
|
|
70
|
+
bulletList: {
|
|
71
|
+
label: '••',
|
|
72
|
+
title: 'Bullet list',
|
|
73
|
+
isActive: editor => editor.isActive('bulletList'),
|
|
74
|
+
run: editor => editor.chain().focus().toggleBulletList().run(),
|
|
75
|
+
},
|
|
76
|
+
orderedList: {
|
|
77
|
+
label: '1.',
|
|
78
|
+
title: 'Numbered list',
|
|
79
|
+
isActive: editor => editor.isActive('orderedList'),
|
|
80
|
+
run: editor => editor.chain().focus().toggleOrderedList().run(),
|
|
81
|
+
},
|
|
82
|
+
link: {
|
|
83
|
+
label: '🔗',
|
|
84
|
+
title: 'Link',
|
|
85
|
+
isActive: editor => editor.isActive('link'),
|
|
86
|
+
run: editor => {
|
|
87
|
+
const current = editor.getAttributes('link').href as string | undefined;
|
|
88
|
+
const url = window.prompt('Link URL', current ?? 'https://');
|
|
89
|
+
//NOTE: null is "cancelled", empty string is "remove the link".
|
|
90
|
+
if (url === null) return;
|
|
91
|
+
if (url === '') {
|
|
92
|
+
editor.chain().focus().extendMarkRange('link').unsetLink().run();
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
editor.chain().focus().extendMarkRange('link').setLink({ href: url }).run();
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export function RichTextField({ input, fieldName }: RichTextFieldProps) {
|
|
101
|
+
const [tiptap, setTiptap] = useState<Tiptap | null>(null);
|
|
102
|
+
const [loadFailed, setLoadFailed] = useState(false);
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
let active = true;
|
|
106
|
+
loadTiptap().then(
|
|
107
|
+
modules => {
|
|
108
|
+
if (active) setTiptap(modules);
|
|
109
|
+
},
|
|
110
|
+
(error: unknown) => {
|
|
111
|
+
console.error(
|
|
112
|
+
"proje-react-panel: a 'richtext' input needs @tiptap/react and @tiptap/starter-kit installed.",
|
|
113
|
+
error
|
|
114
|
+
);
|
|
115
|
+
if (active) setLoadFailed(true);
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
return () => {
|
|
119
|
+
active = false;
|
|
120
|
+
};
|
|
121
|
+
}, []);
|
|
122
|
+
|
|
123
|
+
if (loadFailed) {
|
|
124
|
+
return (
|
|
125
|
+
<div className="rich-text-missing" role="alert">
|
|
126
|
+
Rich text editor unavailable — install @tiptap/react and @tiptap/starter-kit.
|
|
127
|
+
</div>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!tiptap) {
|
|
132
|
+
return <div className="rich-text-loading">Loading editor…</div>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return <RichTextEditor input={input} fieldName={fieldName} tiptap={tiptap} />;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function RichTextEditor({ input, fieldName, tiptap }: RichTextFieldProps & { tiptap: Tiptap }) {
|
|
139
|
+
const richInput = input as RichTextInputConfiguration;
|
|
140
|
+
// Hooks off a dynamically loaded module: safe because the module object is cached, so the
|
|
141
|
+
// hook identity never changes between renders of this component.
|
|
142
|
+
const { EditorContent, useEditor } = tiptap.react;
|
|
143
|
+
const { control } = useFormContext();
|
|
144
|
+
//NOTE: register() would bind the value to a DOM node; TipTap owns an uncontrolled
|
|
145
|
+
// contenteditable of its own, so the value goes through the controller instead.
|
|
146
|
+
const { field } = useController({ name: fieldName, control });
|
|
147
|
+
const toolbar = richInput.toolbar ?? DEFAULT_RICH_TEXT_TOOLBAR;
|
|
148
|
+
const incomingHtml = typeof field.value === 'string' ? field.value : '';
|
|
149
|
+
|
|
150
|
+
// The HTML the editor last emitted or received. The outside -> editor sync compares against
|
|
151
|
+
// this instead of the previous prop value: while the user types, react-hook-form hands our
|
|
152
|
+
// own HTML straight back, and calling setContent for that would jump the caret to the start.
|
|
153
|
+
const editorHtmlRef = useRef<string>(incomingHtml);
|
|
154
|
+
|
|
155
|
+
// onUpdate is captured when the editor is created; the ref keeps it pointed at the live handler.
|
|
156
|
+
const onChangeRef = useRef(field.onChange);
|
|
157
|
+
onChangeRef.current = field.onChange;
|
|
158
|
+
|
|
159
|
+
const editor = useEditor({
|
|
160
|
+
extensions: [tiptap.starterKit],
|
|
161
|
+
content: editorHtmlRef.current,
|
|
162
|
+
//NOTE: toolbar highlighting reads editor state, so the field has to re-render with it.
|
|
163
|
+
shouldRerenderOnTransaction: true,
|
|
164
|
+
onUpdate: ({ editor: updatedEditor }) => {
|
|
165
|
+
// An empty document still serializes to '<p></p>'; report it as empty so required
|
|
166
|
+
// validators (@IsNotEmpty and friends) see what the user actually sees.
|
|
167
|
+
const html = updatedEditor.isEmpty ? '' : updatedEditor.getHTML();
|
|
168
|
+
editorHtmlRef.current = html;
|
|
169
|
+
onChangeRef.current(html);
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
useEffect(() => {
|
|
174
|
+
if (!editor) return;
|
|
175
|
+
// getDetailsData resolves after the form mounts, so the value can arrive late — but only
|
|
176
|
+
// replace the document when it really differs from what the editor already holds.
|
|
177
|
+
if (incomingHtml === editorHtmlRef.current) return;
|
|
178
|
+
editorHtmlRef.current = incomingHtml;
|
|
179
|
+
editor.commands.setContent(incomingHtml, { emitUpdate: false });
|
|
180
|
+
}, [editor, incomingHtml]);
|
|
181
|
+
|
|
182
|
+
const buttons = useMemo(
|
|
183
|
+
() => toolbar.map(item => ({ item, ...TOOLBAR_BUTTONS[item] })),
|
|
184
|
+
[toolbar]
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
return (
|
|
188
|
+
<div className="rich-text-field">
|
|
189
|
+
<div className="rich-text-toolbar" role="toolbar" aria-label={`${input.label ?? fieldName}`}>
|
|
190
|
+
{buttons.map(button => {
|
|
191
|
+
const active = editor ? button.isActive(editor) : false;
|
|
192
|
+
return (
|
|
193
|
+
<button
|
|
194
|
+
key={button.item}
|
|
195
|
+
type="button"
|
|
196
|
+
title={button.title}
|
|
197
|
+
aria-label={button.title}
|
|
198
|
+
aria-pressed={active}
|
|
199
|
+
disabled={!editor}
|
|
200
|
+
className={`rich-text-toolbar-button${active ? ' is-active' : ''}`}
|
|
201
|
+
onClick={() => editor && button.run(editor)}
|
|
202
|
+
>
|
|
203
|
+
{button.label}
|
|
204
|
+
</button>
|
|
205
|
+
);
|
|
206
|
+
})}
|
|
207
|
+
</div>
|
|
208
|
+
<EditorContent
|
|
209
|
+
editor={editor}
|
|
210
|
+
id={fieldName}
|
|
211
|
+
data-testid={`rich-text-${fieldName}`}
|
|
212
|
+
data-placeholder={input.placeholder}
|
|
213
|
+
className={`rich-text-content${!editor || editor.isEmpty ? ' is-empty' : ''}`}
|
|
214
|
+
style={{ minHeight: `${richInput.minHeight ?? DEFAULT_MIN_HEIGHT}px` }}
|
|
215
|
+
/>
|
|
216
|
+
</div>
|
|
217
|
+
);
|
|
218
|
+
}
|
|
@@ -5,12 +5,21 @@ import { GetDetailsDataFN } from '../details/Details';
|
|
|
5
5
|
const DETAILS_METADATA_KEY = 'DetailsMetaData';
|
|
6
6
|
export type OnSubmitFN<T, L = T> = (data: T | FormData) => Promise<L>;
|
|
7
7
|
|
|
8
|
+
export interface FormGroup {
|
|
9
|
+
key: string;
|
|
10
|
+
label: string;
|
|
11
|
+
collapsible?: boolean;
|
|
12
|
+
defaultCollapsed?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
interface FormOptions<T extends AnyClass, L = T> {
|
|
9
16
|
onSubmit: OnSubmitFN<T, L>;
|
|
10
17
|
onSubmitSuccess?: (data: L) => void;
|
|
11
18
|
getDetailsData?: GetDetailsDataFN<T>;
|
|
12
19
|
type?: 'json' | 'formData';
|
|
13
20
|
redirectSuccessUrl?: string;
|
|
21
|
+
//NOTE: declares the section headings and their order; fields opt in with @Input({ group }).
|
|
22
|
+
groups?: FormGroup[];
|
|
14
23
|
}
|
|
15
24
|
|
|
16
25
|
export interface FormConfiguration<T extends AnyClass, L = T> {
|
|
@@ -19,6 +28,7 @@ export interface FormConfiguration<T extends AnyClass, L = T> {
|
|
|
19
28
|
getDetailsData?: GetDetailsDataFN<T>;
|
|
20
29
|
type: 'json' | 'formData';
|
|
21
30
|
redirectSuccessUrl?: string;
|
|
31
|
+
groups?: FormGroup[];
|
|
22
32
|
}
|
|
23
33
|
|
|
24
34
|
export function Form<T extends AnyClass, L = T>(options?: FormOptions<T, L>): ClassDecorator {
|
|
@@ -45,6 +55,7 @@ export function getFormConfiguration<T extends AnyClass, K extends AnyClassConst
|
|
|
45
55
|
getDetailsData: formOptions.getDetailsData,
|
|
46
56
|
type: formOptions.type ?? 'json',
|
|
47
57
|
redirectSuccessUrl: formOptions.redirectSuccessUrl,
|
|
58
|
+
groups: formOptions.groups,
|
|
48
59
|
};
|
|
49
60
|
return formConfiguration;
|
|
50
61
|
}
|
|
@@ -8,7 +8,7 @@ const isFieldSensitive = (fieldName: string): boolean => {
|
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
export type InputTypes = 'input' | 'textarea' | 'file-upload' | 'checkbox' | 'hidden' | 'nested';
|
|
11
|
-
export type ExtendedInputTypes = InputTypes | 'select' | 'custom';
|
|
11
|
+
export type ExtendedInputTypes = InputTypes | 'select' | 'custom' | 'richtext';
|
|
12
12
|
|
|
13
13
|
export interface InputOptions {
|
|
14
14
|
type?: InputTypes;
|
|
@@ -19,6 +19,9 @@ export interface InputOptions {
|
|
|
19
19
|
defaultValue?: string;
|
|
20
20
|
includeInCSV?: boolean;
|
|
21
21
|
includeInJSON?: boolean;
|
|
22
|
+
//NOTE: the visual section this field belongs to, matched against @Form({ groups }) by key.
|
|
23
|
+
group?: string;
|
|
24
|
+
rows?: number;
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
export interface ExtendedInputOptions extends Omit<InputOptions, 'type'> {
|
|
@@ -36,6 +39,8 @@ export interface InputConfiguration {
|
|
|
36
39
|
defaultValue?: string;
|
|
37
40
|
includeInCSV: boolean;
|
|
38
41
|
includeInJSON: boolean;
|
|
42
|
+
group?: string;
|
|
43
|
+
rows?: number;
|
|
39
44
|
}
|
|
40
45
|
|
|
41
46
|
export function Input(options?: InputOptions): PropertyDecorator {
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ExtendedInput, ExtendedInputOptions, InputConfiguration, InputOptions } from '../Input';
|
|
2
|
+
|
|
3
|
+
export type RichTextToolbarItem =
|
|
4
|
+
| 'bold'
|
|
5
|
+
| 'italic'
|
|
6
|
+
| 'heading'
|
|
7
|
+
| 'bulletList'
|
|
8
|
+
| 'orderedList'
|
|
9
|
+
| 'link';
|
|
10
|
+
|
|
11
|
+
export const DEFAULT_RICH_TEXT_TOOLBAR: RichTextToolbarItem[] = [
|
|
12
|
+
'bold',
|
|
13
|
+
'italic',
|
|
14
|
+
'heading',
|
|
15
|
+
'bulletList',
|
|
16
|
+
'orderedList',
|
|
17
|
+
'link',
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
export interface RichTextInputOptions extends InputOptions {
|
|
21
|
+
toolbar?: RichTextToolbarItem[];
|
|
22
|
+
minHeight?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RichTextInputConfiguration extends InputConfiguration {
|
|
26
|
+
type: 'richtext';
|
|
27
|
+
toolbar?: RichTextToolbarItem[];
|
|
28
|
+
minHeight?: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//NOTE: the editor itself lives behind a lazy import in FormField, because @tiptap/* is an
|
|
32
|
+
// optional peer dependency — this decorator is safe to import without it installed.
|
|
33
|
+
export function RichTextInput(options?: RichTextInputOptions): PropertyDecorator {
|
|
34
|
+
return ExtendedInput({
|
|
35
|
+
...options,
|
|
36
|
+
type: 'richtext',
|
|
37
|
+
} as ExtendedInputOptions);
|
|
38
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -18,9 +18,16 @@ export { LinkCell } from './decorators/list/cells/LinkCell';
|
|
|
18
18
|
|
|
19
19
|
//FORM
|
|
20
20
|
export { FormPage } from './components/form/FormPage';
|
|
21
|
-
export { Form, type OnSubmitFN } from './decorators/form/Form';
|
|
21
|
+
export { Form, type OnSubmitFN, type FormGroup } from './decorators/form/Form';
|
|
22
22
|
export { Input } from './decorators/form/Input';
|
|
23
23
|
export { SelectInput } from './decorators/form/inputs/SelectInput';
|
|
24
|
+
export {
|
|
25
|
+
RichTextInput,
|
|
26
|
+
DEFAULT_RICH_TEXT_TOOLBAR,
|
|
27
|
+
type RichTextInputOptions,
|
|
28
|
+
type RichTextInputConfiguration,
|
|
29
|
+
type RichTextToolbarItem,
|
|
30
|
+
} from './decorators/form/inputs/RichTextInput';
|
|
24
31
|
export {
|
|
25
32
|
CustomInput,
|
|
26
33
|
type CustomInputOptions,
|