autoforma 1.0.2 → 1.0.4
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 +157 -157
- package/dist/{src/components → components}/AutoForm.d.ts +1 -3
- package/dist/{src/components → components}/FieldRender.d.ts +0 -1
- package/dist/{src/components → components}/fields/ArrayField.d.ts +0 -1
- package/dist/{src/components → components}/fields/CheckField.d.ts +0 -1
- package/dist/{src/components → components}/fields/DateField.d.ts +0 -1
- package/dist/{src/components → components}/fields/DateTimeField.d.ts +0 -1
- package/dist/{src/components → components}/fields/NumberField.d.ts +0 -1
- package/dist/{src/components → components}/fields/ObjectField.d.ts +0 -1
- package/dist/{src/components → components}/fields/SelectField.d.ts +0 -1
- package/dist/{src/components → components}/fields/TextAreaField.d.ts +0 -1
- package/dist/{src/components → components}/fields/TextField.d.ts +0 -1
- package/dist/{src/components → components}/fields/TimeField.d.ts +0 -1
- package/dist/index.cjs.js +18 -15
- package/dist/{src/index.d.ts → index.d.ts} +0 -1
- package/dist/index.es.js +6261 -5405
- package/dist/{src/stories → stories}/AutoForm.stories.d.ts +0 -1
- package/dist/{src/stories → stories}/Fields.stories.d.ts +0 -1
- package/dist/{src/theme.d.ts → theme.d.ts} +0 -1
- package/dist/{src/types → types}/custom-render.d.ts +0 -1
- package/dist/{src/types → types}/field.d.ts +0 -1
- package/dist/{src/types → types}/form.d.ts +0 -1
- package/package.json +23 -58
- package/dist/src/components/AutoForm.d.ts.map +0 -1
- package/dist/src/components/FieldRender.d.ts.map +0 -1
- package/dist/src/components/fields/ArrayField.d.ts.map +0 -1
- package/dist/src/components/fields/CheckField.d.ts.map +0 -1
- package/dist/src/components/fields/DateField.d.ts.map +0 -1
- package/dist/src/components/fields/DateTimeField.d.ts.map +0 -1
- package/dist/src/components/fields/NumberField.d.ts.map +0 -1
- package/dist/src/components/fields/ObjectField.d.ts.map +0 -1
- package/dist/src/components/fields/SelectField.d.ts.map +0 -1
- package/dist/src/components/fields/TextAreaField.d.ts.map +0 -1
- package/dist/src/components/fields/TextField.d.ts.map +0 -1
- package/dist/src/components/fields/TimeField.d.ts.map +0 -1
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/stories/AutoForm.stories.d.ts.map +0 -1
- package/dist/src/stories/Fields.stories.d.ts.map +0 -1
- package/dist/src/theme.d.ts.map +0 -1
- package/dist/src/types/custom-render.d.ts.map +0 -1
- package/dist/src/types/field.d.ts.map +0 -1
- package/dist/src/types/form.d.ts.map +0 -1
- package/dist/test-utils/index.d.ts +0 -5
- package/dist/test-utils/index.d.ts.map +0 -1
- package/dist/test-utils/render.d.ts +0 -2
- package/dist/test-utils/render.d.ts.map +0 -1
- package/src/components/AutoForm.tsx +0 -201
- package/src/components/FieldRender.tsx +0 -217
- package/src/components/fields/ArrayField.tsx +0 -59
- package/src/components/fields/CheckField.tsx +0 -23
- package/src/components/fields/DateField.tsx +0 -26
- package/src/components/fields/DateTimeField.tsx +0 -26
- package/src/components/fields/NumberField.tsx +0 -20
- package/src/components/fields/ObjectField.tsx +0 -41
- package/src/components/fields/SelectField.tsx +0 -23
- package/src/components/fields/TextAreaField.tsx +0 -25
- package/src/components/fields/TextField.tsx +0 -20
- package/src/components/fields/TimeField.tsx +0 -32
- package/src/types/custom-render.ts +0 -22
- package/src/types/field.ts +0 -32
- package/src/types/form.ts +0 -8
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import React, { Fragment } from 'react';
|
|
2
|
-
import { Button, Text } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps, FieldSchema } from '@/types/field';
|
|
4
|
-
import FieldRender from '../FieldRender';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
interface ArrayFieldProps extends BaseFieldProps<Record<string, any>[]> {
|
|
8
|
-
fields: FieldSchema[];
|
|
9
|
-
options: {
|
|
10
|
-
addElement: (val: Record<string, any>) => void;
|
|
11
|
-
replaceElement: (index: number, val: Record<string, any>) => void;
|
|
12
|
-
removeElement: (index: number) => void;
|
|
13
|
-
};
|
|
14
|
-
error?: Record<string, React.ReactNode>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const ArrayField: React.FC<ArrayFieldProps> = (props) => {
|
|
18
|
-
const { fields, options, value, error = {}, name: parentName, readOnly } = props;
|
|
19
|
-
|
|
20
|
-
const content = value.map((row, rowIndex) => {
|
|
21
|
-
const onChange = (name: string, value: any) => {
|
|
22
|
-
row[name] = value;
|
|
23
|
-
options.replaceElement(rowIndex, row);
|
|
24
|
-
};
|
|
25
|
-
return fields.map((field, index) => {
|
|
26
|
-
return (
|
|
27
|
-
<Fragment key={index}>
|
|
28
|
-
<FieldRender
|
|
29
|
-
field={field}
|
|
30
|
-
formValues={row}
|
|
31
|
-
onChange={onChange}
|
|
32
|
-
value={row[field.name]}
|
|
33
|
-
error={field.name in (error[rowIndex] ?? {}) ? error[rowIndex][field.name] : undefined}
|
|
34
|
-
readOnly={readOnly}
|
|
35
|
-
/>
|
|
36
|
-
{!readOnly && <Button onClick={() => options.removeElement(rowIndex)}>Remove</Button>}
|
|
37
|
-
</Fragment>
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return (
|
|
43
|
-
<>
|
|
44
|
-
{typeof error === 'string' && (
|
|
45
|
-
<Text c="red" fz="sm" fw={500}>
|
|
46
|
-
{error}
|
|
47
|
-
</Text>
|
|
48
|
-
)}
|
|
49
|
-
{content}
|
|
50
|
-
{!readOnly && (
|
|
51
|
-
<Button display="block" onClick={() => options.addElement({})}>
|
|
52
|
-
Add
|
|
53
|
-
</Button>
|
|
54
|
-
)}
|
|
55
|
-
</>
|
|
56
|
-
);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
export default ArrayField;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { Checkbox } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps } from '@/types/field';
|
|
4
|
-
|
|
5
|
-
interface CheckFieldProps extends BaseFieldProps<boolean> {
|
|
6
|
-
label: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const CheckField: React.FC<CheckFieldProps> = (props) => {
|
|
10
|
-
const { name, onChange, value, readOnly, label } = props;
|
|
11
|
-
|
|
12
|
-
return (
|
|
13
|
-
<Checkbox
|
|
14
|
-
name={name}
|
|
15
|
-
checked={value}
|
|
16
|
-
readOnly={readOnly}
|
|
17
|
-
onChange={(e) => onChange(name, e.target.checked)}
|
|
18
|
-
label={label}
|
|
19
|
-
/>
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export default CheckField;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import dayjs from 'dayjs';
|
|
2
|
-
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { DateInput } from '@mantine/dates';
|
|
5
|
-
import { BaseFieldProps } from '@/types/field';
|
|
6
|
-
|
|
7
|
-
dayjs.extend(customParseFormat);
|
|
8
|
-
|
|
9
|
-
interface DateFieldProps extends BaseFieldProps<Date> {}
|
|
10
|
-
|
|
11
|
-
const DateField: React.FC<DateFieldProps> = (props) => {
|
|
12
|
-
const { name, onChange, value, readOnly } = props;
|
|
13
|
-
|
|
14
|
-
return (
|
|
15
|
-
<DateInput
|
|
16
|
-
name={name}
|
|
17
|
-
onChange={(value) => onChange(name, value)}
|
|
18
|
-
value={value}
|
|
19
|
-
readOnly={readOnly}
|
|
20
|
-
valueFormat="YYYY-MM-DD"
|
|
21
|
-
clearable
|
|
22
|
-
/>
|
|
23
|
-
);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export default DateField;
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import dayjs from 'dayjs';
|
|
2
|
-
import customParseFormat from 'dayjs/plugin/customParseFormat';
|
|
3
|
-
import React from 'react';
|
|
4
|
-
import { DateTimePicker } from '@mantine/dates';
|
|
5
|
-
import { BaseFieldProps } from '@/types/field';
|
|
6
|
-
|
|
7
|
-
dayjs.extend(customParseFormat);
|
|
8
|
-
|
|
9
|
-
interface DateTimeFieldProps extends BaseFieldProps<Date> {}
|
|
10
|
-
|
|
11
|
-
const DateTimeField: React.FC<DateTimeFieldProps> = (props) => {
|
|
12
|
-
const { name, onChange, value, readOnly } = props;
|
|
13
|
-
|
|
14
|
-
return (
|
|
15
|
-
<DateTimePicker
|
|
16
|
-
name={name}
|
|
17
|
-
onChange={(value) => onChange(name, value)}
|
|
18
|
-
value={value}
|
|
19
|
-
readOnly={readOnly}
|
|
20
|
-
valueFormat="YYYY-MM-DD HH:mm:ss"
|
|
21
|
-
clearable
|
|
22
|
-
/>
|
|
23
|
-
);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export default DateTimeField;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { NumberInput } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps } from '@/types/field';
|
|
4
|
-
|
|
5
|
-
interface NumberFieldProps extends BaseFieldProps<number> {}
|
|
6
|
-
|
|
7
|
-
const NumberField: React.FC<NumberFieldProps> = (props) => {
|
|
8
|
-
const { name, onChange, value, readOnly } = props;
|
|
9
|
-
|
|
10
|
-
return (
|
|
11
|
-
<NumberInput
|
|
12
|
-
name={name}
|
|
13
|
-
onChange={(value) => onChange(name, value as number)}
|
|
14
|
-
value={value}
|
|
15
|
-
readOnly={readOnly}
|
|
16
|
-
/>
|
|
17
|
-
);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export default NumberField;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { BaseFieldProps, FieldSchema } from '@/types/field';
|
|
3
|
-
import FieldRender from '../FieldRender';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
interface ObjectFieldProps extends BaseFieldProps<Record<string, any>> {
|
|
7
|
-
fields: FieldSchema[];
|
|
8
|
-
formValues: Record<string, any>;
|
|
9
|
-
error?: Record<string, React.ReactNode>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const ObjectField: React.FC<ObjectFieldProps> = (props) => {
|
|
13
|
-
const {
|
|
14
|
-
fields,
|
|
15
|
-
name: parentName,
|
|
16
|
-
onChange: onParentChange,
|
|
17
|
-
value,
|
|
18
|
-
formValues,
|
|
19
|
-
error = {},
|
|
20
|
-
readOnly,
|
|
21
|
-
} = props;
|
|
22
|
-
|
|
23
|
-
const onChange = (name: string, value: any) => {
|
|
24
|
-
onParentChange(`${parentName}.${name}`, value);
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const content = fields.map((field, index) => (
|
|
28
|
-
<FieldRender
|
|
29
|
-
key={index}
|
|
30
|
-
field={field}
|
|
31
|
-
formValues={formValues}
|
|
32
|
-
onChange={onChange}
|
|
33
|
-
value={value[field.name]}
|
|
34
|
-
error={error[field.name]}
|
|
35
|
-
readOnly={readOnly}
|
|
36
|
-
/>
|
|
37
|
-
));
|
|
38
|
-
return content;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export default ObjectField;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { Select } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps } from '@/types/field';
|
|
4
|
-
|
|
5
|
-
interface SelectFieldProps extends BaseFieldProps<string> {
|
|
6
|
-
data: { label: string; value: string }[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const SelectField: React.FC<SelectFieldProps> = (props) => {
|
|
10
|
-
const { data, name, onChange, value, readOnly } = props;
|
|
11
|
-
return (
|
|
12
|
-
<Select
|
|
13
|
-
name={name}
|
|
14
|
-
data={data}
|
|
15
|
-
value={value}
|
|
16
|
-
onChange={(value) => onChange(name, value)}
|
|
17
|
-
readOnly={readOnly}
|
|
18
|
-
clearable
|
|
19
|
-
/>
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export default SelectField;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { Textarea } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps } from '@/types/field';
|
|
4
|
-
|
|
5
|
-
interface TextAreaFieldProps extends BaseFieldProps<string> {}
|
|
6
|
-
|
|
7
|
-
const TextAreaField: React.FC<TextAreaFieldProps> = (props) => {
|
|
8
|
-
const { name, onChange, value, readOnly } = props;
|
|
9
|
-
|
|
10
|
-
return (
|
|
11
|
-
<Textarea
|
|
12
|
-
name={name}
|
|
13
|
-
onChange={(e) => onChange(name, e.target.value)}
|
|
14
|
-
value={value}
|
|
15
|
-
readOnly={readOnly}
|
|
16
|
-
styles={{
|
|
17
|
-
input: {
|
|
18
|
-
height: 200
|
|
19
|
-
}
|
|
20
|
-
}}
|
|
21
|
-
/>
|
|
22
|
-
);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export default TextAreaField;
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { TextInput } from '@mantine/core';
|
|
3
|
-
import { BaseFieldProps } from '@/types/field';
|
|
4
|
-
|
|
5
|
-
interface TextFieldProps extends BaseFieldProps<string> {}
|
|
6
|
-
|
|
7
|
-
const TextField: React.FC<TextFieldProps> = (props) => {
|
|
8
|
-
const { name, onChange, value, readOnly } = props;
|
|
9
|
-
|
|
10
|
-
return (
|
|
11
|
-
<TextInput
|
|
12
|
-
name={name}
|
|
13
|
-
onChange={(value) => onChange(name, value.target.value)}
|
|
14
|
-
value={value}
|
|
15
|
-
readOnly={readOnly}
|
|
16
|
-
/>
|
|
17
|
-
);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export default TextField;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import React, { useRef } from 'react';
|
|
2
|
-
import { ActionIcon } from '@mantine/core';
|
|
3
|
-
import { TimeInput } from '@mantine/dates';
|
|
4
|
-
import { BaseFieldProps } from '@/types/field';
|
|
5
|
-
|
|
6
|
-
interface TimeFieldProps extends BaseFieldProps<string> {}
|
|
7
|
-
|
|
8
|
-
const TimeField: React.FC<TimeFieldProps> = (props) => {
|
|
9
|
-
const { name, onChange, value, readOnly } = props;
|
|
10
|
-
|
|
11
|
-
const ref = useRef<HTMLInputElement>(null);
|
|
12
|
-
|
|
13
|
-
const pickerControl = (
|
|
14
|
-
<ActionIcon variant="subtle" color="gray" onClick={() => ref.current?.showPicker()}>
|
|
15
|
-
🕛
|
|
16
|
-
</ActionIcon>
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
return (
|
|
20
|
-
<TimeInput
|
|
21
|
-
name={name}
|
|
22
|
-
onChange={(e) => onChange(name, e.target.value)}
|
|
23
|
-
value={value}
|
|
24
|
-
readOnly={readOnly}
|
|
25
|
-
ref={ref}
|
|
26
|
-
rightSection={pickerControl}
|
|
27
|
-
withSeconds
|
|
28
|
-
/>
|
|
29
|
-
);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export default TimeField;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
// types/custom-render.ts
|
|
2
|
-
import { ArrayFieldOptions, FieldSchema } from './field';
|
|
3
|
-
|
|
4
|
-
export type FieldRenderCustomRender = (
|
|
5
|
-
field: FieldSchema,
|
|
6
|
-
value: any,
|
|
7
|
-
error: React.ReactNode | Record<string, React.ReactNode>,
|
|
8
|
-
onChange: (name: string, value: any) => void,
|
|
9
|
-
formValues: Record<string, any>,
|
|
10
|
-
options?: ArrayFieldOptions,
|
|
11
|
-
readOnly?: true
|
|
12
|
-
) => React.ReactNode;
|
|
13
|
-
|
|
14
|
-
export interface CustomFieldRender {
|
|
15
|
-
field: FieldSchema;
|
|
16
|
-
value: any;
|
|
17
|
-
error: React.ReactNode | Record<string, React.ReactNode>;
|
|
18
|
-
onChange: (name: string, value: any) => void;
|
|
19
|
-
formValues: Record<string, any>;
|
|
20
|
-
options?: ArrayFieldOptions;
|
|
21
|
-
readOnly?: true;
|
|
22
|
-
}
|
package/src/types/field.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export type FieldType = 'text' | 'number' | 'object' | 'array' | 'check' | 'select' | 'textarea' | 'date' | 'datetime' | 'time';
|
|
2
|
-
|
|
3
|
-
export interface FieldSchema {
|
|
4
|
-
name: string;
|
|
5
|
-
type: FieldType;
|
|
6
|
-
label: string;
|
|
7
|
-
description?: string;
|
|
8
|
-
placeholder?: string;
|
|
9
|
-
extra?: Record<string, any>;
|
|
10
|
-
disabled?: (values: Record<string, any>) => boolean | boolean;
|
|
11
|
-
visible?: (values: Record<string, any>) => boolean | boolean;
|
|
12
|
-
initialValue?: any;
|
|
13
|
-
required?: true;
|
|
14
|
-
readOnly?: true;
|
|
15
|
-
// for object type only
|
|
16
|
-
fields?: FieldSchema[];
|
|
17
|
-
// for select type only
|
|
18
|
-
data?: { label: string; value: string }[];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface BaseFieldProps<T> {
|
|
22
|
-
name: string;
|
|
23
|
-
value: T;
|
|
24
|
-
onChange: (name: string, value: T | null) => void;
|
|
25
|
-
readOnly?: true;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type ArrayFieldOptions = {
|
|
29
|
-
addElement: (value: Record<string, any>) => void;
|
|
30
|
-
replaceElement: (index: number, val: Record<string, any>) => void;
|
|
31
|
-
removeElement: (index: number) => void;
|
|
32
|
-
};
|