@nolwenture/formik-jsonschema-form 0.5.0 → 0.6.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.
Files changed (84) hide show
  1. package/README.md +74 -0
  2. package/dist/Form.d.ts +2 -2
  3. package/dist/Form.js +4 -4
  4. package/dist/Form.stories.d.ts +12 -11
  5. package/dist/Form.stories.js +65 -38
  6. package/dist/Form.test.js +1 -1
  7. package/dist/FormEditor.d.ts +3 -3
  8. package/dist/FormEditor.js +6 -6
  9. package/dist/FormEditor.stories.d.ts +5 -4
  10. package/dist/FormEditor.stories.js +3 -5
  11. package/dist/fields/AnyField.d.ts +3 -3
  12. package/dist/fields/AnyField.js +5 -6
  13. package/dist/fields/ArrayField.d.ts +3 -3
  14. package/dist/fields/ArrayField.js +10 -10
  15. package/dist/fields/BooleanField.d.ts +3 -3
  16. package/dist/fields/BooleanField.js +3 -3
  17. package/dist/fields/DateField.d.ts +3 -3
  18. package/dist/fields/DateField.js +3 -3
  19. package/dist/fields/NullField.d.ts +3 -3
  20. package/dist/fields/NullField.js +2 -2
  21. package/dist/fields/NumberField.d.ts +3 -3
  22. package/dist/fields/NumberField.js +2 -2
  23. package/dist/fields/ObjectField.d.ts +3 -3
  24. package/dist/fields/ObjectField.js +27 -7
  25. package/dist/fields/RadioField.d.ts +3 -3
  26. package/dist/fields/RadioField.js +3 -3
  27. package/dist/fields/SchemaField.d.ts +3 -3
  28. package/dist/fields/SchemaField.js +6 -5
  29. package/dist/fields/SearchMultiTagSelectField.d.ts +3 -3
  30. package/dist/fields/SearchMultiTagSelectField.js +4 -3
  31. package/dist/fields/StringField.d.ts +3 -3
  32. package/dist/fields/StringField.js +9 -7
  33. package/dist/fields/ToggleField.d.ts +3 -3
  34. package/dist/fields/ToggleField.js +3 -3
  35. package/dist/formEditorSchemas.d.ts +1 -1
  36. package/dist/hooks/index.d.ts +2 -0
  37. package/dist/hooks/index.js +2 -0
  38. package/dist/hooks/useExtractUiSchema.d.ts +8 -0
  39. package/dist/hooks/useExtractUiSchema.js +22 -0
  40. package/dist/hooks/useSetDefault.d.ts +2 -0
  41. package/dist/hooks/useSetDefault.js +16 -0
  42. package/dist/sampleData/completeSchema.d.ts +3 -0
  43. package/dist/{_sampleData.js → sampleData/completeSchema.js} +0 -107
  44. package/dist/sampleData/conditionsSchema.d.ts +3 -0
  45. package/dist/sampleData/conditionsSchema.js +80 -0
  46. package/dist/sampleData/generatedSchema.d.ts +2 -0
  47. package/dist/sampleData/generatedSchema.js +19 -0
  48. package/dist/sampleData/index.d.ts +5 -0
  49. package/dist/sampleData/index.js +5 -0
  50. package/dist/sampleData/requiredInputSchema.d.ts +3 -0
  51. package/dist/sampleData/requiredInputSchema.js +72 -0
  52. package/dist/sampleData/simpleData.d.ts +7 -0
  53. package/dist/sampleData/simpleData.js +16 -0
  54. package/dist/styledComponents.d.ts +181 -84
  55. package/dist/styledComponents.js +28 -29
  56. package/dist/templates/ArrayFieldTemplate.d.ts +3 -3
  57. package/dist/templates/ArrayFieldTemplate.js +5 -4
  58. package/dist/templates/FieldTemplate.d.ts +3 -3
  59. package/dist/templates/FieldTemplate.js +6 -5
  60. package/dist/templates/ObjectFieldTemplate.d.ts +3 -3
  61. package/dist/templates/ObjectFieldTemplate.js +4 -3
  62. package/dist/types.d.ts +2 -2
  63. package/dist/utils/__tests/getEnums.test.d.ts +1 -0
  64. package/dist/utils/__tests/getEnums.test.js +64 -0
  65. package/dist/utils/__tests/resolveSchema.test.d.ts +1 -0
  66. package/dist/utils/__tests/resolveSchema.test.js +106 -0
  67. package/dist/utils/deepEqual.d.ts +1 -0
  68. package/dist/utils/deepEqual.js +18 -0
  69. package/dist/utils/getEnums.d.ts +10 -0
  70. package/dist/utils/getEnums.js +30 -0
  71. package/dist/utils/getTitleAndDescription.d.ts +12 -0
  72. package/dist/utils/getTitleAndDescription.js +15 -0
  73. package/dist/utils/getTypes.d.ts +5 -0
  74. package/dist/utils/getTypes.js +53 -0
  75. package/dist/utils/index.d.ts +7 -0
  76. package/dist/utils/index.js +14 -0
  77. package/dist/utils/mergeSchemas.d.ts +1 -0
  78. package/dist/utils/mergeSchemas.js +26 -0
  79. package/dist/utils/resolveSchema.d.ts +2 -0
  80. package/dist/utils/resolveSchema.js +68 -0
  81. package/package.json +32 -37
  82. package/dist/_sampleData.d.ts +0 -12
  83. package/dist/utils.d.ts +0 -32
  84. package/dist/utils.js +0 -138
package/README.md CHANGED
@@ -453,6 +453,80 @@ RadioButtonField.styles = {
453
453
  };
454
454
  ```
455
455
 
456
+ ### Conditional fields
457
+
458
+ Conditional fields depending on a parent field input can be given with an `if-then-else` logic inside the schema making use of `allOf`. While `if`and `then` are required to make the condition work, `else` is optional.
459
+
460
+ ```tsx
461
+ export const conditionsSchema = {
462
+ type: "object",
463
+ properties: {
464
+ animal: {
465
+ type: "string",
466
+ oneOf: [
467
+ { const: "cat", title: "Cat" },
468
+ { const: "fish", title: "Fish" },
469
+ { const: "dog", title: "Dog" },
470
+ ],
471
+ },
472
+ },
473
+ allOf: [
474
+ {
475
+ if: {
476
+ properties: {
477
+ animal: {
478
+ const: "cat",
479
+ },
480
+ },
481
+ },
482
+ then: {
483
+ properties: {
484
+ food: {
485
+ type: "string",
486
+ enum: ["meat", "grass", "fish"],
487
+ },
488
+ },
489
+ required: ["food"],
490
+ },
491
+ },
492
+ {
493
+ if: {
494
+ properties: {
495
+ animal: {
496
+ const: "fish",
497
+ },
498
+ },
499
+ },
500
+ then: {
501
+ properties: {
502
+ food: {
503
+ type: "string",
504
+ enum: ["insect", "worms"],
505
+ },
506
+ water: {
507
+ type: "string",
508
+ enum: ["lake", "sea"],
509
+ },
510
+ },
511
+ required: ["food", "water"],
512
+ },
513
+ else: {
514
+ properties: {
515
+ likesWalkiesWhere: {
516
+ type: "string",
517
+ enum: ["park", "forest", "garden"],
518
+ },
519
+ },
520
+ },
521
+ },
522
+
523
+ {
524
+ required: ["animal"],
525
+ },
526
+ ],
527
+ };
528
+ ```
529
+
456
530
  ## Props
457
531
 
458
532
  `Form` component props:
package/dist/Form.d.ts CHANGED
@@ -1,4 +1,4 @@
1
+ import type { FormProps, FormRegistryContextValue } from "./types";
1
2
  import React from "react";
2
- import { FormProps, FormRegistryContextValue } from "./types";
3
3
  export declare const FormRegistry: React.Context<FormRegistryContextValue>;
4
- export declare function Form<T>(props: FormProps<T>): JSX.Element;
4
+ export declare const Form: <T>(props: FormProps<T>) => import("react/jsx-runtime").JSX.Element;
package/dist/Form.js CHANGED
@@ -14,7 +14,7 @@ export const FormRegistry = React.createContext({
14
14
  useItemsAsAdditionalItems: false,
15
15
  hideUnmarkedAdditionalProperties: false,
16
16
  });
17
- export function Form(props) {
17
+ export const Form = (props) => {
18
18
  warnFromUnsupportedKeys("Form", props, [
19
19
  "schema",
20
20
  "onSubmit",
@@ -40,6 +40,6 @@ export function Form(props) {
40
40
  hideUnmarkedAdditionalProperties,
41
41
  };
42
42
  const { Form: FormEl, FormButton, FormButtonWrapper } = registry.components;
43
- const _SchemaField = registry.fields.SchemaField;
44
- return (_jsx(FormRegistry.Provider, Object.assign({ value: registry }, { children: _jsxs(FormEl, Object.assign({ className: className }, { children: [_jsx(_SchemaField, { name: "", schema: schema, uiSchema: uiSchema }), children ? (children) : (_jsx(FormButtonWrapper, { children: _jsx(FormButton, Object.assign({ type: "submit", disabled: isSubmitting }, { children: "Send" })) }))] })) })));
45
- }
43
+ const SchemaField = registry.fields.SchemaField;
44
+ return (_jsx(FormRegistry.Provider, { value: registry, children: _jsxs(FormEl, { className: className, children: [_jsx(SchemaField, { name: "", schema: schema, uiSchema: uiSchema }), children ? (children) : (_jsx(FormButtonWrapper, { children: _jsx(FormButton, { type: "submit", disabled: isSubmitting, children: "Send" }) }))] }) }));
45
+ };
@@ -1,12 +1,13 @@
1
- /// <reference types="react" />
2
- import { ComponentStory, ComponentMeta } from "@storybook/react";
1
+ import type { StoryObj, Meta } from "@storybook/react";
3
2
  import { Form } from "./";
4
- declare const _default: ComponentMeta<typeof Form>;
5
- export default _default;
6
- export declare const Simple: ComponentStory<typeof Form>;
7
- export declare const WithUiSchema: ComponentStory<typeof Form>;
8
- export declare const CompleteSchema: ComponentStory<typeof Form>;
9
- export declare const StrictlyTyped: () => JSX.Element;
10
- export declare const StylesCustomized: () => JSX.Element;
11
- export declare const WithGeneratedSchema: ComponentStory<typeof Form>;
12
- export declare const WithRequiredInput: ComponentStory<typeof Form>;
3
+ declare const meta: Meta<typeof Form>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof Form>;
6
+ export declare const Simple: Story;
7
+ export declare const WithUiSchema: Story;
8
+ export declare const CompleteSchema: Story;
9
+ export declare const StrictlyTyped: () => import("react/jsx-runtime").JSX.Element;
10
+ export declare const StylesCustomized: () => import("react/jsx-runtime").JSX.Element;
11
+ export declare const WithGeneratedSchema: Story;
12
+ export declare const WithRequiredInput: Story;
13
+ export declare const WithConditions: Story;
@@ -7,14 +7,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
+ var __rest = (this && this.__rest) || function (s, e) {
11
+ var t = {};
12
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
13
+ t[p] = s[p];
14
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
15
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
16
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
17
+ t[p[i]] = s[p[i]];
18
+ }
19
+ return t;
20
+ };
10
21
  import { jsx as _jsx } from "react/jsx-runtime";
11
22
  import { action } from "@storybook/addon-actions";
12
23
  import styled from "styled-components";
13
24
  import { Formik, useFormikContext } from "formik";
14
25
  import { InfoFieldButton, SearchableMultiTagSelectField, } from "./styledComponents";
15
- import { completeSchema, completeUiSchema, simpleSchema, simpleUiSchema, generatedSchema, requiredInputSchema, requiredInputUiSchema, } from "./_sampleData";
26
+ import { completeSchema, completeUiSchema, simpleSchema, simpleUiSchema, generatedSchema, requiredInputSchema, requiredInputUiSchema, conditionsSchema, conditionsUiSchema, } from "./sampleData";
16
27
  import { Form, styledComponents } from "./";
17
- export default {
28
+ const meta = {
18
29
  title: "Form",
19
30
  component: Form,
20
31
  // More on argTypes: https://storybook.js.org/docs/react/api/argtypes
@@ -22,15 +33,20 @@ export default {
22
33
  schema: { control: { type: "object" } },
23
34
  uiSchema: { control: { type: "object" } },
24
35
  },
36
+ render: (_a) => {
37
+ var args = __rest(_a, []);
38
+ return (_jsx(Formik, { initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
39
+ action("onSubmit(_, formHelper)")(formikHelpers);
40
+ action("onSubmit(values)")(values);
41
+ formikHelpers.setSubmitting(false);
42
+ }), children: _jsx(Form, Object.assign({}, args)) }));
43
+ },
25
44
  };
26
- const Template = (args) => (_jsx(Formik, Object.assign({ initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
27
- action("onSubmit(_, formHelper)")(formikHelpers);
28
- action("onSubmit(values)")(values);
29
- formikHelpers.setSubmitting(false);
30
- }) }, { children: _jsx(Form, Object.assign({}, args)) })));
31
- export const Simple = Template.bind({});
32
- Simple.args = {
33
- schema: simpleSchema,
45
+ export default meta;
46
+ export const Simple = {
47
+ args: {
48
+ schema: simpleSchema,
49
+ },
34
50
  };
35
51
  const InfoFieldButtonNew = styled(InfoFieldButton) `
36
52
  outline: 2px red solid;
@@ -44,11 +60,12 @@ const InfoFieldButtonNew = styled(InfoFieldButton) `
44
60
  color: green;
45
61
  }
46
62
  `;
47
- export const WithUiSchema = Template.bind({});
48
- WithUiSchema.args = {
49
- schema: simpleSchema,
50
- uiSchema: simpleUiSchema,
51
- components: { InfoFieldButton: InfoFieldButtonNew },
63
+ export const WithUiSchema = {
64
+ args: {
65
+ schema: simpleSchema,
66
+ uiSchema: simpleUiSchema,
67
+ components: { InfoFieldButton: InfoFieldButtonNew },
68
+ },
52
69
  };
53
70
  // TODO: get customisable styling to work
54
71
  const MyStyledSearchField = styled(SearchableMultiTagSelectField) `
@@ -59,18 +76,19 @@ const MyStyledSearchField = styled(SearchableMultiTagSelectField) `
59
76
  background: blue;
60
77
  }
61
78
  `;
62
- export const CompleteSchema = Template.bind({});
63
- CompleteSchema.args = {
64
- schema: completeSchema,
65
- uiSchema: completeUiSchema,
66
- components: { SearchableMultiTagSelectField: MyStyledSearchField },
79
+ export const CompleteSchema = {
80
+ args: {
81
+ schema: completeSchema,
82
+ uiSchema: completeUiSchema,
83
+ components: { SearchableMultiTagSelectField: MyStyledSearchField },
84
+ },
67
85
  };
68
86
  export const StrictlyTyped = () => {
69
- return (_jsx(Formik, Object.assign({ initialValues: { firstName: "", lastName: "" }, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
87
+ return (_jsx(Formik, { initialValues: { firstName: "", lastName: "" }, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
70
88
  action("onSubmit(_, formHelper)")(formikHelpers);
71
89
  action("onSubmit(values)")(values);
72
90
  formikHelpers.setSubmitting(false);
73
- }) }, { children: _jsx(Form, { schema: simpleSchema }) })));
91
+ }), children: _jsx(Form, { schema: simpleSchema }) }));
74
92
  };
75
93
  /*** CUSTOMIZATION ***/
76
94
  const CustomizedForm = styled(Form) `
@@ -92,11 +110,11 @@ const CustomizedRequiredIndicator = styled(styledComponents.RequiredIndicator) `
92
110
  color: aqua;
93
111
  `;
94
112
  export const StylesCustomized = () => {
95
- return (_jsx(Formik, Object.assign({ initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
113
+ return (_jsx(Formik, { initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
96
114
  action("onSubmit(_, formHelper)")(formikHelpers);
97
115
  action("onSubmit(values)")(values);
98
116
  formikHelpers.setSubmitting(false);
99
- }) }, { children: _jsx(CustomizedForm, { schema: simpleSchema, uiSchema: {
117
+ }), children: _jsx(CustomizedForm, { schema: simpleSchema, uiSchema: {
100
118
  age: {
101
119
  props: {
102
120
  wrapper: {
@@ -115,13 +133,14 @@ export const StylesCustomized = () => {
115
133
  FieldWrapper: CustomizedFieldWrapper,
116
134
  ErrorMessage: CustomizedErrorMessage,
117
135
  RequiredIndicator: CustomizedRequiredIndicator,
118
- } }) })));
136
+ } }) }));
119
137
  };
120
- export const WithGeneratedSchema = Template.bind({});
121
- WithGeneratedSchema.args = {
122
- schema: generatedSchema,
123
- uiSchema: {},
124
- useItemsAsAdditionalItems: true,
138
+ export const WithGeneratedSchema = {
139
+ args: {
140
+ schema: generatedSchema,
141
+ uiSchema: {},
142
+ useItemsAsAdditionalItems: true,
143
+ },
125
144
  };
126
145
  // Test to add style to RadioButtonField and ToggleSwitchField
127
146
  // const MyRadioButtonField = styled(RadioButtonField)`
@@ -147,12 +166,20 @@ WithGeneratedSchema.args = {
147
166
  // background: pink;
148
167
  // }
149
168
  // `;
150
- export const WithRequiredInput = Template.bind({});
151
- WithRequiredInput.args = {
152
- schema: requiredInputSchema,
153
- uiSchema: requiredInputUiSchema,
154
- // components: {
155
- // RadioButtonField: MyRadioButtonField,
156
- // ToggleSwitchField: MyToggleSwitchField,
157
- // },
169
+ export const WithRequiredInput = {
170
+ args: {
171
+ schema: requiredInputSchema,
172
+ uiSchema: requiredInputUiSchema,
173
+ // components: {
174
+ // RadioButtonField: MyRadioButtonField,
175
+ // ToggleSwitchField: MyToggleSwitchField,
176
+ // },
177
+ },
178
+ };
179
+ export const WithConditions = {
180
+ args: {
181
+ schema: conditionsSchema,
182
+ uiSchema: conditionsUiSchema,
183
+ hideUnmarkedAdditionalProperties: true,
184
+ },
158
185
  };
package/dist/Form.test.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { render, screen } from "@testing-library/react";
3
3
  import { Form } from "./Form";
4
- import { simpleSchema } from "./_sampleData";
4
+ import { simpleSchema } from "./sampleData";
5
5
  test("renders title", () => {
6
6
  render(_jsx(Form, { schema: simpleSchema }));
7
7
  const titleElement = screen.getByText(/TITLE/i);
@@ -1,8 +1,8 @@
1
- /// <reference types="react" />
2
- import { Schema, UiSchema } from "./types";
1
+ import type { Schema, UiSchema } from "./types";
2
+ import React from "react";
3
3
  export type FormEditorProps = {};
4
4
  export type FormEditorValues = {
5
5
  schema: Schema;
6
6
  uiSchema: UiSchema;
7
7
  };
8
- export declare function FormEditor(props: FormEditorProps): JSX.Element;
8
+ export declare const FormEditor: React.FC<FormEditorProps>;
@@ -8,22 +8,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
11
- import { Formik } from "formik";
12
11
  import { useEffect, useState } from "react";
12
+ import { Formik } from "formik";
13
13
  import { Form } from "./Form";
14
14
  import * as formEditorSchemas from "./formEditorSchemas";
15
- export function FormEditor(props) {
15
+ export const FormEditor = (props) => {
16
16
  const [editorGeneratedSchemas, setEditorGeneratedSchemas] = useState({
17
17
  schema: {},
18
18
  uiSchema: {},
19
19
  });
20
20
  useEffect(() => console.log(editorGeneratedSchemas), [editorGeneratedSchemas]);
21
- return (_jsxs("div", Object.assign({ style: { display: "flex", justifyContent: "space-between" } }, { children: [_jsxs("div", { children: [_jsx("h1", { children: "Editor" }), _jsx(Formik, Object.assign({ initialValues: { schema: {}, uiSchema: {} }, onSubmit: (values, formikHelpers) => __awaiter(this, void 0, void 0, function* () {
21
+ return (_jsxs("div", { style: { display: "flex", justifyContent: "space-between" }, children: [_jsxs("div", { children: [_jsx("h1", { children: "Editor" }), _jsx(Formik, { initialValues: { schema: {}, uiSchema: {} }, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
22
22
  setEditorGeneratedSchemas(values);
23
23
  formikHelpers.setSubmitting(false);
24
- }) }, { children: _jsx(Form, Object.assign({}, formEditorSchemas)) }))] }), _jsxs("div", { children: [_jsx("h1", { children: "Preview" }), _jsx(Formik, Object.assign({ initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(this, void 0, void 0, function* () {
24
+ }), children: _jsx(Form, Object.assign({}, formEditorSchemas)) })] }), _jsxs("div", { children: [_jsx("h1", { children: "Preview" }), _jsx(Formik, { initialValues: {}, onSubmit: (values, formikHelpers) => __awaiter(void 0, void 0, void 0, function* () {
25
25
  // action("onSubmit(_, formHelper)")(formikHelpers);
26
26
  // action("onSubmit(values)")(values);
27
27
  formikHelpers.setSubmitting(false);
28
- }) }, { children: _jsx(Form, Object.assign({}, editorGeneratedSchemas)) }))] })] })));
29
- }
28
+ }), children: _jsx(Form, Object.assign({}, editorGeneratedSchemas)) })] })] }));
29
+ };
@@ -1,5 +1,6 @@
1
- import { ComponentStory, ComponentMeta } from "@storybook/react";
1
+ import { StoryObj, Meta } from "@storybook/react";
2
2
  import { FormEditor } from "./FormEditor";
3
- declare const _default: ComponentMeta<typeof FormEditor>;
4
- export default _default;
5
- export declare const Default: ComponentStory<typeof FormEditor>;
3
+ declare const meta: Meta<typeof FormEditor>;
4
+ export default meta;
5
+ type Story = StoryObj<typeof FormEditor>;
6
+ export declare const Default: Story;
@@ -1,11 +1,9 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
1
  import { FormEditor } from "./FormEditor";
3
- export default {
2
+ const meta = {
4
3
  title: "FormEditor",
5
4
  component: FormEditor,
6
5
  // More on argTypes: https://storybook.js.org/docs/react/api/argtypes
7
6
  argTypes: {},
8
7
  };
9
- const Template = (args) => (_jsx(FormEditor, Object.assign({}, args)));
10
- export const Default = Template.bind({});
11
- Default.args = {};
8
+ export default meta;
9
+ export const Default = {};
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function AnyField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const AnyField: React.FC<FieldProps>;
@@ -1,11 +1,10 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useMemo } from "react";
3
- import { useState } from "react";
2
+ import { useMemo, useState } from "react";
4
3
  import { useField } from "formik";
5
- import { SchemaField } from "./SchemaField";
6
4
  import { guessType } from "../utils";
5
+ import { SchemaField } from "./SchemaField";
7
6
  import { COMPONENT_TYPES } from "./types";
8
- export function AnyField(props) {
7
+ export const AnyField = (props) => {
9
8
  const { name, schema, uiSchema } = props;
10
9
  const [{ value }] = useField(name);
11
10
  const [type, setType] = useState(value !== undefined ? guessType(value) : "string");
@@ -19,5 +18,5 @@ export function AnyField(props) {
19
18
  return Object.assign(Object.assign({}, (schema || {})), { type });
20
19
  }
21
20
  }, [type, schema]);
22
- return (_jsxs("div", { children: [_jsx("select", Object.assign({ value: type, onChange: (event) => setType(event.target.value) }, { children: Object.keys(COMPONENT_TYPES).map((type) => (_jsx("option", Object.assign({ value: type }, { children: type }), type))) })), _jsx(SchemaField, { name: name, schema: enrichedSchema, uiSchema: uiSchema })] }));
23
- }
21
+ return (_jsxs("div", { children: [_jsx("select", { value: type, onChange: (event) => setType(event.target.value), children: Object.keys(COMPONENT_TYPES).map((type) => (_jsx("option", { value: type, children: type }, type))) }), _jsx(SchemaField, { name: name, schema: enrichedSchema, uiSchema: uiSchema })] }));
22
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function ArrayField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const ArrayField: React.FC<FieldProps>;
@@ -1,17 +1,17 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useContext, useMemo } from "react";
3
- import { FormRegistry } from "../Form";
4
3
  import { useField } from "formik";
4
+ import { FormRegistry } from "../Form";
5
5
  import { ArrayFieldButtonPurpose } from "../types";
6
- import { useExtractUiSchema, useSetDefault } from "../utils";
6
+ import { useExtractUiSchema, useSetDefault } from "../hooks";
7
7
  import { GetArrayFieldButton } from "../styledComponents";
8
- function appendArray(values) {
8
+ const appendArray = (values) => {
9
9
  return values ? [...values, ""] : [""];
10
- }
11
- function removeFromArray(values, index) {
10
+ };
11
+ const removeFromArray = (values, index) => {
12
12
  return [...values.slice(0, index), ...values.slice(index + 1)];
13
- }
14
- function moveInArray(values, index, direction) {
13
+ };
14
+ const moveInArray = (values, index, direction) => {
15
15
  if (direction === -1) {
16
16
  return [
17
17
  ...values.slice(0, index - 1),
@@ -28,8 +28,8 @@ function moveInArray(values, index, direction) {
28
28
  ...values.slice(index + 2),
29
29
  ];
30
30
  }
31
- }
32
- export function ArrayField(props) {
31
+ };
32
+ export const ArrayField = (props) => {
33
33
  const { fields: { SchemaField }, templates: { ArrayFieldTemplate }, components: { ArrayFieldItem, ArrayFieldList, ArrayFieldItemButtons }, useItemsAsAdditionalItems, } = useContext(FormRegistry);
34
34
  useSetDefault(props);
35
35
  const { name, schema, uiSchema } = props;
@@ -117,4 +117,4 @@ export function ArrayField(props) {
117
117
  onClick: () => setValue(appendArray(value)),
118
118
  uiSchema,
119
119
  })] })));
120
- }
120
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function BooleanField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const BooleanField: React.FC<FieldProps>;
@@ -1,11 +1,11 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { FormRegistry } from "../Form";
4
- import { useExtractUiSchema, useSetDefault } from "../utils";
5
- export function BooleanField(props) {
4
+ import { useExtractUiSchema, useSetDefault } from "../hooks";
5
+ export const BooleanField = (props) => {
6
6
  const { templates: { FieldTemplate }, components: { FieldInput }, } = useContext(FormRegistry);
7
7
  useSetDefault(props);
8
8
  const { name, uiSchema } = props;
9
9
  const { label, props: inputProps } = useExtractUiSchema(uiSchema);
10
10
  return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsxs(_Fragment, { children: [_jsx(FieldInput, Object.assign({}, inputProps, { name: name, type: "checkbox" })), label] }) })));
11
- }
11
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function DateField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const DateField: React.FC<FieldProps>;
@@ -1,11 +1,11 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { FormRegistry } from "../Form";
4
- import { useExtractUiSchema, useSetDefault } from "../utils";
5
- export function DateField(props) {
4
+ import { useExtractUiSchema, useSetDefault } from "../hooks";
5
+ export const DateField = (props) => {
6
6
  const { templates: { FieldTemplate }, components: { FieldInput }, } = useContext(FormRegistry);
7
7
  useSetDefault(props);
8
8
  const { name, uiSchema } = props;
9
9
  const { props: inputProps } = useExtractUiSchema(uiSchema);
10
10
  return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(FieldInput, Object.assign({}, inputProps, { name: name, type: "date" })) })));
11
- }
11
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function NullField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const NullField: React.FC<FieldProps>;
@@ -1,8 +1,8 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { FormRegistry } from "../Form";
4
- export function NullField(props) {
4
+ export const NullField = (props) => {
5
5
  const { templates: { FieldTemplate }, } = useContext(FormRegistry);
6
6
  const { name } = props;
7
7
  return _jsx(FieldTemplate, Object.assign({}, props));
8
- }
8
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function NumberField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const NumberField: React.FC<FieldProps>;
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { StringField } from "./StringField";
3
- export function NumberField(props) {
3
+ export const NumberField = (props) => {
4
4
  var _a;
5
5
  return (_jsx(StringField, Object.assign({}, props, { uiSchema: Object.assign(Object.assign({}, props.uiSchema), { "ui:props": Object.assign(Object.assign({}, (((_a = props.uiSchema) === null || _a === void 0 ? void 0 : _a["ui:props"]) || {})), { type: "number" }) }) })));
6
- }
6
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function ObjectField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const ObjectField: React.FC<FieldProps>;
@@ -10,18 +10,38 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  return t;
11
11
  };
12
12
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
- import { useField } from "formik";
14
- import { useContext, useMemo } from "react";
13
+ import { useField, useFormikContext } from "formik";
14
+ import { useContext, useEffect, useMemo } from "react";
15
15
  import { FormRegistry } from "../Form";
16
16
  import { ObjectFieldButtonPurpose } from "../types";
17
- import { useSetDefault } from "../utils";
17
+ import { resolveSchema } from "../utils";
18
+ import { useSetDefault } from "../hooks";
18
19
  import { getObjectFieldButton } from "../styledComponents";
19
- export function ObjectField(props) {
20
+ const useObjectField = (name) => {
21
+ if (!name) {
22
+ const { values = {}, setValues } = useFormikContext();
23
+ return { value: values, setValue: setValues };
24
+ }
25
+ const [{ value = {} }, _, { setValue }] = useField(name);
26
+ return { value, setValue };
27
+ };
28
+ export const ObjectField = (props) => {
20
29
  const { fields: { SchemaField }, templates: { ObjectFieldTemplate }, components: { ObjectFieldItem, ObjectFieldItemButtons }, hideUnmarkedAdditionalProperties, } = useContext(FormRegistry);
21
30
  useSetDefault(props);
22
31
  const { name, schema, uiSchema } = props;
23
- const [{ value }, _, { setValue }] = useField(name);
24
- const { properties, additionalProperties, required } = schema;
32
+ const { value, setValue } = useObjectField(name);
33
+ const [{ properties, additionalProperties, required }, valuesToRemove] = useMemo(() => {
34
+ return resolveSchema(schema, value);
35
+ }, [schema, value]);
36
+ useEffect(() => {
37
+ if (valuesToRemove.length > 0) {
38
+ const newValue = valuesToRemove.reduce((prevValue, key) => {
39
+ const _a = prevValue, _b = key, _removedProperty = _a[_b], newValue = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]);
40
+ return newValue;
41
+ }, value);
42
+ setValue(newValue);
43
+ }
44
+ }, [valuesToRemove]);
25
45
  const propertyKeys = useMemo(() => Object.keys(properties || {}), [properties]);
26
46
  const additionalValues = useMemo(() => Object.fromEntries(Object.entries(value || {}).filter(([key, value]) => !propertyKeys.includes(key))), [value, propertyKeys]);
27
47
  return (_jsxs(ObjectFieldTemplate, Object.assign({}, props, { children: [Object.entries(properties || {}).map(([childName, childSchema]) => (_jsx(SchemaField, { required: required === null || required === void 0 ? void 0 : required.includes(childName), name: name ? [name, childName].join(".") : childName, schema: typeof childSchema !== "boolean" ? childSchema : {}, uiSchema: uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema[childName] }, childName))), (!hideUnmarkedAdditionalProperties || additionalProperties) &&
@@ -43,4 +63,4 @@ export function ObjectField(props) {
43
63
  },
44
64
  uiSchema: uiSchema,
45
65
  })] })));
46
- }
66
+ };
@@ -1,3 +1,3 @@
1
- /// <reference types="react" />
2
- import { FieldProps } from "../types";
3
- export declare function RadioField(props: FieldProps): JSX.Element;
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const RadioField: React.FC<FieldProps>;
@@ -1,11 +1,11 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
3
  import { FormRegistry } from "../Form";
4
- import { useExtractUiSchema, useSetDefault } from "../utils";
5
- export function RadioField(props) {
4
+ import { useExtractUiSchema, useSetDefault } from "../hooks";
5
+ export const RadioField = (props) => {
6
6
  const { templates: { FieldTemplate }, components: { RadioButtonField }, } = useContext(FormRegistry);
7
7
  useSetDefault(props);
8
8
  const { name, uiSchema } = props;
9
9
  const { label, props: inputProps } = useExtractUiSchema(uiSchema);
10
10
  return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(RadioButtonField, { inputProps: inputProps, label: label, name: name }) })));
11
- }
11
+ };