@jsb188/form 1.1.2 → 1.1.13
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 +21 -0
- package/README.md +1 -1
- package/package.json +16 -7
- package/src/modules/Form-Line.tsx +8 -2
- package/src/modules/ListItemForm.tsx +9 -8
- package/src/modules/ListItemForm.types.ts +1 -0
- package/src/modules/PopOverInput.tsx +1 -0
- package/src/ui/FormUI.tsx +20 -9
- package/src/ui/Input.tsx +1 -0
- package/src/ui/Input.types.ts +1 -1
- package/src/ui/LineForm.tsx +5 -2
- package/tests/controller.test.tsx +0 -336
- package/tests/controls.test.tsx +0 -261
- package/tests/schema-form.test.tsx +0 -303
- package/tests/settings-definitions.test.ts +0 -129
- package/tests/sms-input.test.tsx +0 -131
- package/tests/time-input.test.tsx +0 -395
- package/tests/types.ts +0 -102
- package/tests/upload-input.test.tsx +0 -114
- package/tests/validation.test.ts +0 -65
- package/tsconfig.json +0 -13
- package/vitest.config.ts +0 -8
package/tests/types.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { defineForm } from '../src/libs/defineForm';
|
|
3
|
-
import { useSchemaForm } from '../src/libs/useSchemaForm';
|
|
4
|
-
|
|
5
|
-
const profile = defineForm({
|
|
6
|
-
schema: z.object({
|
|
7
|
-
name: z.string(),
|
|
8
|
-
contact: z.object({ email: z.string(), label: z.string() }),
|
|
9
|
-
age: z.string().transform(Number),
|
|
10
|
-
createdAt: z.date(),
|
|
11
|
-
contacts: z.array(z.object({ email: z.string(), label: z.string() })),
|
|
12
|
-
}),
|
|
13
|
-
defaults: { name: '', contact: { email: '', label: '' }, age: '0', createdAt: new Date(), contacts: [] },
|
|
14
|
-
fields: {
|
|
15
|
-
name: {
|
|
16
|
-
kind: 'input',
|
|
17
|
-
visibleWhen: ({ values }) => {
|
|
18
|
-
values.age satisfies string;
|
|
19
|
-
// @ts-expect-error Presentation conditions read editable input values, not transformed output.
|
|
20
|
-
values.age satisfies number;
|
|
21
|
-
return !!values.name;
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
'contact.email': { kind: 'input' },
|
|
25
|
-
age: { kind: 'input' },
|
|
26
|
-
'contacts.0.email': { kind: 'input' },
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
defineForm({
|
|
31
|
-
...profile,
|
|
32
|
-
fields: {
|
|
33
|
-
name: { kind: 'input' },
|
|
34
|
-
// @ts-expect-error Unknown field names remain errors even beside a valid field.
|
|
35
|
-
missing: { kind: 'input' },
|
|
36
|
-
},
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
defineForm({
|
|
40
|
-
...profile,
|
|
41
|
-
layout: [
|
|
42
|
-
'name',
|
|
43
|
-
// @ts-expect-error Layout references must point to paths in the value schema.
|
|
44
|
-
'contact.missing',
|
|
45
|
-
],
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
const address = defineForm({
|
|
49
|
-
schema: z.object({ address: z.object({ city: z.string(), country: z.string() }) }),
|
|
50
|
-
defaults: { address: { city: '', country: '' } },
|
|
51
|
-
fields: {
|
|
52
|
-
'address.city': { kind: 'input', label: 'City' },
|
|
53
|
-
'address.country': {
|
|
54
|
-
kind: 'input',
|
|
55
|
-
resolve: ({ values }) => ({ placeholder: values.address.country }),
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const organization = defineForm({
|
|
61
|
-
schema: address.schema.extend({ name: z.string() }),
|
|
62
|
-
defaults: { ...address.defaults, name: '' },
|
|
63
|
-
fields: {
|
|
64
|
-
...address.fields,
|
|
65
|
-
name: { kind: 'input', visibleWhen: ({ values }) => !!values.address.city },
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
organization.fields['address.city'].kind satisfies 'input';
|
|
70
|
-
|
|
71
|
-
/* Check the public editing and submission contracts at compile time; this fixture is never executed. */
|
|
72
|
-
export function assertFormTypes() {
|
|
73
|
-
const form = useSchemaForm(profile, {
|
|
74
|
-
initialValues: { contact: { email: 'member@example.com' }, createdAt: new Date() },
|
|
75
|
-
onSubmit: (values) => {
|
|
76
|
-
values.age satisfies number;
|
|
77
|
-
// @ts-expect-error Schema transforms apply to submitted output, not editable input.
|
|
78
|
-
values.age satisfies string;
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
form.values.age satisfies string;
|
|
82
|
-
form.getValues().age satisfies string;
|
|
83
|
-
form.setFieldValue('contact.email', 'member@example.com');
|
|
84
|
-
form.setFieldValue('contacts.0.email', 'member@example.com');
|
|
85
|
-
form.setFieldValue('contacts.0', { email: 'member@example.com', label: 'Work' });
|
|
86
|
-
form.setFieldValue('createdAt', new Date());
|
|
87
|
-
// @ts-expect-error Unknown paths are rejected by the controller.
|
|
88
|
-
form.setFieldValue('contact.missing', 'no');
|
|
89
|
-
// @ts-expect-error Nested field values retain their declared type.
|
|
90
|
-
form.setFieldValue('contact.email', 42);
|
|
91
|
-
// @ts-expect-error The form edits the input string even though submission yields a number.
|
|
92
|
-
form.setFieldValue('age', 42);
|
|
93
|
-
|
|
94
|
-
useSchemaForm(profile, {
|
|
95
|
-
// @ts-expect-error Arrays replace defaults, so each supplied item must be complete.
|
|
96
|
-
initialValues: { contacts: [{ email: 'member@example.com' }] },
|
|
97
|
-
});
|
|
98
|
-
useSchemaForm(profile, {
|
|
99
|
-
// @ts-expect-error Date values remain Dates instead of being made deeply partial objects.
|
|
100
|
-
initialValues: { createdAt: {} },
|
|
101
|
-
});
|
|
102
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { JSDOM } from 'jsdom';
|
|
2
|
-
import { act, createRef } from 'react';
|
|
3
|
-
import type { Root } from 'react-dom/client';
|
|
4
|
-
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
|
5
|
-
import { UploadInput, type UploadInputRef } from '../src/modules/UploadInput';
|
|
6
|
-
|
|
7
|
-
let root: Root;
|
|
8
|
-
let dom: JSDOM;
|
|
9
|
-
let container: HTMLDivElement;
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'https://example.test' });
|
|
13
|
-
vi.stubGlobal('window', dom.window);
|
|
14
|
-
vi.stubGlobal('document', dom.window.document);
|
|
15
|
-
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
|
|
16
|
-
const { createRoot } = await import('react-dom/client');
|
|
17
|
-
container = document.createElement('div');
|
|
18
|
-
document.body.append(container);
|
|
19
|
-
root = createRoot(container);
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
afterEach(() => {
|
|
23
|
-
act(() => root.unmount());
|
|
24
|
-
dom.window.close();
|
|
25
|
-
vi.unstubAllGlobals();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
/*
|
|
29
|
-
* Select a file through the native change event without a storage provider.
|
|
30
|
-
*/
|
|
31
|
-
function pickFile(file: File) {
|
|
32
|
-
const input = container.querySelector('input')!;
|
|
33
|
-
Object.defineProperty(input, 'files', { configurable: true, value: [file] });
|
|
34
|
-
act(() => input.dispatchEvent(new dom.window.Event('change', { bubbles: true })));
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
it('delegates uploads and keeps the picked file available for retries when requested', async () => {
|
|
38
|
-
const ref = createRef<UploadInputRef>();
|
|
39
|
-
const onChangeFile = vi.fn();
|
|
40
|
-
const onUpload = vi.fn().mockResolvedValueOnce(null).mockResolvedValueOnce({ fileUri: 'uploaded.csv' });
|
|
41
|
-
act(() =>
|
|
42
|
-
root.render(
|
|
43
|
-
<UploadInput
|
|
44
|
-
ref={ref}
|
|
45
|
-
onChangeFile={onChangeFile}
|
|
46
|
-
onUpload={onUpload}
|
|
47
|
-
/>,
|
|
48
|
-
),
|
|
49
|
-
);
|
|
50
|
-
const file = new dom.window.File(['name\nAlice'], 'people.csv', { type: 'text/csv' });
|
|
51
|
-
pickFile(file);
|
|
52
|
-
expect(onChangeFile).toHaveBeenLastCalledWith(expect.objectContaining({ file, name: 'people.csv', contentType: 'text/csv' }));
|
|
53
|
-
expect(await ref.current!.startUpload({ doNotClearInput: true })).toBe(false);
|
|
54
|
-
expect(await ref.current!.upload({ doNotClearInput: true })).toBe(true);
|
|
55
|
-
expect(onUpload.mock.calls).toEqual([[file], [file]]);
|
|
56
|
-
expect(onChangeFile).toHaveBeenCalledTimes(1);
|
|
57
|
-
act(() => ref.current!.clearPickedFile());
|
|
58
|
-
expect(onChangeFile).toHaveBeenLastCalledWith(null);
|
|
59
|
-
expect(await ref.current!.startUpload()).toBe(false);
|
|
60
|
-
expect(onUpload).toHaveBeenCalledTimes(2);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('clears selection before an ordinary upload and uses the latest callback', async () => {
|
|
64
|
-
const ref = createRef<UploadInputRef>();
|
|
65
|
-
const firstUpload = vi.fn();
|
|
66
|
-
const onChangeFile = vi.fn();
|
|
67
|
-
act(() =>
|
|
68
|
-
root.render(
|
|
69
|
-
<UploadInput
|
|
70
|
-
ref={ref}
|
|
71
|
-
onChangeFile={onChangeFile}
|
|
72
|
-
onUpload={firstUpload}
|
|
73
|
-
/>,
|
|
74
|
-
),
|
|
75
|
-
);
|
|
76
|
-
const file = new dom.window.File(['data'], 'attachment.txt');
|
|
77
|
-
pickFile(file);
|
|
78
|
-
const onUpload = vi.fn(async () => {
|
|
79
|
-
expect(onChangeFile).toHaveBeenLastCalledWith(null);
|
|
80
|
-
return true;
|
|
81
|
-
});
|
|
82
|
-
act(() =>
|
|
83
|
-
root.render(
|
|
84
|
-
<UploadInput
|
|
85
|
-
ref={ref}
|
|
86
|
-
onChangeFile={onChangeFile}
|
|
87
|
-
onUpload={onUpload}
|
|
88
|
-
/>,
|
|
89
|
-
),
|
|
90
|
-
);
|
|
91
|
-
expect(await ref.current!.startUpload()).toBe(true);
|
|
92
|
-
expect(onUpload).toHaveBeenCalledWith(file);
|
|
93
|
-
expect(firstUpload).not.toHaveBeenCalled();
|
|
94
|
-
expect(await ref.current!.startUpload()).toBe(false);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('uploads on pick when requested and allows selecting the same file again', async () => {
|
|
98
|
-
const onUpload = vi.fn().mockResolvedValue(true);
|
|
99
|
-
act(() =>
|
|
100
|
-
root.render(
|
|
101
|
-
<UploadInput
|
|
102
|
-
initiateUploadOnPick
|
|
103
|
-
onUpload={onUpload}
|
|
104
|
-
/>,
|
|
105
|
-
),
|
|
106
|
-
);
|
|
107
|
-
const file = new dom.window.File(['data'], 'attachment.txt');
|
|
108
|
-
pickFile(file);
|
|
109
|
-
await act(async () => {});
|
|
110
|
-
expect(container.querySelector('input')!.value).toBe('');
|
|
111
|
-
pickFile(file);
|
|
112
|
-
await act(async () => {});
|
|
113
|
-
expect(onUpload.mock.calls).toEqual([[file], [file]]);
|
|
114
|
-
});
|
package/tests/validation.test.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { rules } from '../src/libs/rules';
|
|
4
|
-
import { getFieldValue, initialFormValues, setFieldValue, valuesEqual } from '../src/libs/useSchemaForm.helpers';
|
|
5
|
-
import { submissionResult, validationErrors } from '../src/libs/validation';
|
|
6
|
-
|
|
7
|
-
describe('shared form validation and snapshots', () => {
|
|
8
|
-
it('does not treat aborted or partially failed saves as a saved baseline', () => {
|
|
9
|
-
expect(submissionResult({ aborted: true }).status).toBe('continue');
|
|
10
|
-
expect(submissionResult([{ saved: true }, { aborted: true }]).status).toBe('continue');
|
|
11
|
-
expect(submissionResult([{ saved: true }, { error: { message: 'Denied' } }])).toEqual({
|
|
12
|
-
status: 'error',
|
|
13
|
-
error: { message: 'Denied' },
|
|
14
|
-
});
|
|
15
|
-
expect(submissionResult([{ aborted: true }, { errors: [{ message: 'Denied' }] }]).status).toBe('error');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('keeps empty optional text inputs distinct from invalid nonempty inputs', () => {
|
|
19
|
-
expect(rules.phone({ optional: true }).safeParse('').success).toBe(true);
|
|
20
|
-
expect(rules.phone().safeParse('').success).toBe(false);
|
|
21
|
-
expect(rules.phone({ optional: true }).safeParse('not a phone').success).toBe(false);
|
|
22
|
-
expect(rules.email().safeParse('person@example.com').success).toBe(true);
|
|
23
|
-
expect(rules.url({ optional: true }).safeParse('').success).toBe(true);
|
|
24
|
-
expect(rules.url().safeParse('javascript:alert(1)').success).toBe(false);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('does not turn an empty required number into zero', () => {
|
|
28
|
-
expect(rules.number().safeParse('').success).toBe(false);
|
|
29
|
-
expect(rules.number().parse('0')).toBe(0);
|
|
30
|
-
expect(rules.number({ optional: true }).parse('')).toBeUndefined();
|
|
31
|
-
expect(rules.number({ integer: true }).safeParse('1.5').success).toBe(false);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it('rejects impossible calendar dates', () => {
|
|
35
|
-
expect(rules.date().safeParse('2026-02-30').success).toBe(false);
|
|
36
|
-
expect(rules.date().safeParse('2024-02-29').success).toBe(true);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it('retains all issues with their complete nested paths', () => {
|
|
40
|
-
const result = z
|
|
41
|
-
.object({ address: z.object({ city: z.string().min(1), zip: z.string().min(1) }) })
|
|
42
|
-
.safeParse({ address: { city: '', zip: '' } });
|
|
43
|
-
if (result.success) throw new Error('Expected invalid input');
|
|
44
|
-
expect(Object.keys(validationErrors(result.error.issues))).toEqual(['address.city', 'address.zip']);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('updates arrays without mutating previous values and blocks unsafe paths', () => {
|
|
48
|
-
const previous = { contacts: [{ name: 'Before' }], stable: {} };
|
|
49
|
-
const next = setFieldValue(previous, 'contacts.0.name', 'After');
|
|
50
|
-
expect(previous.contacts[0].name).toBe('Before');
|
|
51
|
-
expect(next.contacts[0].name).toBe('After');
|
|
52
|
-
expect(next.stable).toBe(previous.stable);
|
|
53
|
-
expect(setFieldValue(previous, '__proto__.polluted', true)).toBe(previous);
|
|
54
|
-
expect(getFieldValue({}, '__proto__.polluted')).toBeUndefined();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('isolates merged default dates and treats changed types as dirty', () => {
|
|
58
|
-
const defaults = { address: { city: '', country: 'CA' }, date: new Date('2026-01-01') };
|
|
59
|
-
const values = initialFormValues(defaults, { address: { city: 'Toronto', country: 'CA' } });
|
|
60
|
-
values.date.setUTCFullYear(2027);
|
|
61
|
-
expect(defaults.date.getUTCFullYear()).toBe(2026);
|
|
62
|
-
expect(valuesEqual({ count: '1' }, { count: 1 })).toBe(false);
|
|
63
|
-
expect(valuesEqual(new Date('2026-01-01'), {})).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ESNext",
|
|
4
|
-
"module": "ESNext",
|
|
5
|
-
"moduleResolution": "Bundler",
|
|
6
|
-
"allowImportingTsExtensions": true,
|
|
7
|
-
"jsx": "react-jsx",
|
|
8
|
-
"noEmit": true,
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true
|
|
11
|
-
},
|
|
12
|
-
"include": ["src/**/*", "tests/types.ts"]
|
|
13
|
-
}
|
package/vitest.config.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { fileURLToPath } from 'node:url';
|
|
2
|
-
import { defineConfig } from 'vitest/config';
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
root: fileURLToPath(new URL('.', import.meta.url)),
|
|
6
|
-
oxc: { jsx: { runtime: 'automatic' } },
|
|
7
|
-
test: { environment: 'node', include: ['tests/**/*.test.ts?(x)'] },
|
|
8
|
-
});
|