@nolwenture/formik-jsonschema-form 0.4.2 → 0.5.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.
@@ -1,28 +1,15 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
2
  import { useContext } from "react";
14
3
  import { FormRegistry } from "../Form";
15
4
  import { useExtractUiSchema, getDescription, getTitle } from "../utils";
16
5
  import { RequiredIndicator } from "../styledComponents";
17
6
  export function ArrayFieldTemplate(props) {
18
- var _a = useContext(FormRegistry).components, ArrayFieldWrapper = _a.ArrayFieldWrapper, ArrayFieldTitle = _a.ArrayFieldTitle, FieldDescription = _a.FieldDescription, ErrorMessage = _a.ErrorMessage, FieldHelp = _a.FieldHelp, InfoFieldButton = _a.InfoFieldButton;
19
- var name = props.name, uiSchema = props.uiSchema, children = props.children;
20
- var title = getTitle(props);
21
- var description = getDescription(props);
22
- var _b = useExtractUiSchema(uiSchema), help = _b.help, info = _b.info,
7
+ const { components: { ArrayFieldWrapper, ArrayFieldTitle, FieldDescription, ErrorMessage, FieldHelp, InfoFieldButton, }, } = useContext(FormRegistry);
8
+ const { name, uiSchema, children } = props;
9
+ const title = getTitle(props);
10
+ const description = getDescription(props);
11
+ const { help, info,
23
12
  //TODO: This is not right
24
- _c = _b.props,
25
- //TODO: This is not right
26
- _d = _c === void 0 ? {} : _c, wrappertProps = _d.wrapper, titleProps = _d.title, descriptionProps = _d.description, helpProps = _d.help, errorProps = _d.error;
27
- return (_jsxs(ArrayFieldWrapper, __assign({}, wrappertProps, { children: [title && (_jsxs(ArrayFieldTitle, __assign({}, titleProps, { htmlFor: name }, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, __assign({}, info)), description && (_jsx(FieldDescription, __assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, __assign({}, helpProps, { children: help })), _jsx(ErrorMessage, __assign({}, errorProps, { name: name }))] })));
13
+ props: { wrapper: wrappertProps, title: titleProps, description: descriptionProps, help: helpProps, error: errorProps, } = {}, } = useExtractUiSchema(uiSchema);
14
+ return (_jsxs(ArrayFieldWrapper, Object.assign({}, wrappertProps, { children: [title && (_jsxs(ArrayFieldTitle, Object.assign({}, titleProps, { htmlFor: name }, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help })), _jsx(ErrorMessage, Object.assign({}, errorProps, { name: name }))] })));
28
15
  }
@@ -1,27 +1,14 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
2
  import { useContext } from "react";
14
3
  import { FormRegistry } from "../Form";
15
4
  import { useExtractUiSchema, getDescription, getTitle } from "../utils";
16
5
  export function FieldTemplate(props) {
17
- var _a = useContext(FormRegistry).components, FieldWrapper = _a.FieldWrapper, FieldTitle = _a.FieldTitle, FieldDescription = _a.FieldDescription, ErrorMessage = _a.ErrorMessage, FieldHelp = _a.FieldHelp, RequiredIndicator = _a.RequiredIndicator, InfoFieldButton = _a.InfoFieldButton;
18
- var name = props.name, uiSchema = props.uiSchema, children = props.children;
19
- var title = getTitle(props);
20
- var description = getDescription(props);
21
- var _b = useExtractUiSchema(uiSchema), help = _b.help, info = _b.info,
6
+ const { components: { FieldWrapper, FieldTitle, FieldDescription, ErrorMessage, FieldHelp, RequiredIndicator, InfoFieldButton, }, } = useContext(FormRegistry);
7
+ const { name, uiSchema, children } = props;
8
+ const title = getTitle(props);
9
+ const description = getDescription(props);
10
+ const { help, info,
22
11
  //TODO: This is not right
23
- _c = _b.props,
24
- //TODO: This is not right
25
- _d = _c === void 0 ? {} : _c, wrappertProps = _d.wrapper, titleProps = _d.title, descriptionProps = _d.description, helpProps = _d.help, errorProps = _d.error;
26
- return (_jsxs(FieldWrapper, __assign({}, wrappertProps, { children: [title && (_jsxs(FieldTitle, __assign({}, titleProps, { htmlFor: name }, { children: [title, props.required && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, __assign({}, info)), description && (_jsx(FieldDescription, __assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, __assign({}, helpProps, { children: help })), _jsx(ErrorMessage, __assign({}, errorProps, { name: name }))] })));
12
+ props: { wrapper: wrappertProps, title: titleProps, description: descriptionProps, help: helpProps, error: errorProps, } = {}, } = useExtractUiSchema(uiSchema);
13
+ return (_jsxs(FieldWrapper, Object.assign({}, wrappertProps, { children: [title && (_jsxs(FieldTitle, Object.assign({}, titleProps, { htmlFor: name }, { children: [title, props.required && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help })), _jsx(ErrorMessage, Object.assign({}, errorProps, { name: name }))] })));
27
14
  }
@@ -1,28 +1,17 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
2
  import { useContext } from "react";
14
3
  import { FormRegistry } from "../Form";
15
4
  import { getDescription, getTitle, useExtractUiSchema } from "../utils";
16
5
  import { RequiredIndicator } from "../styledComponents";
17
6
  export function ObjectFieldTemplate(props) {
18
- var _a = useContext(FormRegistry).components, FieldDescription = _a.FieldDescription, ObjectFieldTitle = _a.ObjectFieldTitle, ObjectFieldWrapper = _a.ObjectFieldWrapper, FieldHelp = _a.FieldHelp, InfoFieldButton = _a.InfoFieldButton;
19
- var children = props.children, uiSchema = props.uiSchema;
20
- var title = getTitle(props);
21
- var description = getDescription(props);
22
- var _b = useExtractUiSchema(uiSchema), help = _b.help, info = _b.info,
7
+ const { components: { FieldDescription, ObjectFieldTitle, ObjectFieldWrapper, FieldHelp, InfoFieldButton, }, } = useContext(FormRegistry);
8
+ const { children, uiSchema } = props;
9
+ const title = getTitle(props);
10
+ const description = getDescription(props);
11
+ const { help, info,
23
12
  //TODO: This is not right
24
- _c = _b.props,
25
- //TODO: This is not right
26
- _d = _c === void 0 ? {} : _c, wrapperProps = _d.wrapper, titleProps = _d.title, descriptionProps = _d.description, helpProps = _d.help;
27
- return (_jsxs(ObjectFieldWrapper, __assign({}, wrapperProps, { children: [title && (_jsxs(ObjectFieldTitle, __assign({}, titleProps, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, __assign({}, info)), description && (_jsx(FieldDescription, __assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, __assign({}, helpProps, { children: help }))] })));
13
+ props: { wrapper: wrapperProps, title: titleProps, description: descriptionProps, help: helpProps,
14
+ // error: errorProps,
15
+ } = {}, } = useExtractUiSchema(uiSchema);
16
+ return (_jsxs(ObjectFieldWrapper, Object.assign({}, wrapperProps, { children: [title && (_jsxs(ObjectFieldTitle, Object.assign({}, titleProps, { children: [title, (props === null || props === void 0 ? void 0 : props.required) && _jsx(RequiredIndicator, { children: "*" })] }))), info && _jsx(InfoFieldButton, Object.assign({}, info)), description && (_jsx(FieldDescription, Object.assign({}, descriptionProps, { children: description }))), children, help && _jsx(FieldHelp, Object.assign({}, helpProps, { children: help }))] })));
28
17
  }
package/dist/types.d.ts CHANGED
@@ -37,6 +37,11 @@ export type UiSchema = {
37
37
  };
38
38
  "ui:help"?: React.ReactNode | React.ReactNode[];
39
39
  "ui:info"?: InfoFieldButtonProps;
40
+ "ui:placeholderOptionOptions"?: {
41
+ props: {
42
+ [key: string]: any;
43
+ };
44
+ };
40
45
  [name: string]: any;
41
46
  };
42
47
  export type FormRegistryContextValue = {
@@ -121,6 +126,9 @@ export type ComponentsType = {
121
126
  ErrorMessage: React.FunctionComponent<any>;
122
127
  FieldInput: React.FunctionComponent<any>;
123
128
  RequiredIndicator: React.FunctionComponent<any>;
129
+ RadioButtonField: React.FunctionComponent<any>;
130
+ ToggleSwitchField: React.FunctionComponent<any>;
131
+ SearchableMultiTagSelectField: React.FunctionComponent<any>;
124
132
  };
125
133
  export declare enum ObjectFieldButtonPurpose {
126
134
  ADD = 0,
@@ -147,10 +155,33 @@ export type ArrayFieldButtonProps = {
147
155
  export type InfoFieldButtonProps = {
148
156
  label?: React.ReactNode;
149
157
  content: React.ReactNode;
150
- position?: 'top' | 'right' | 'bottom' | 'left';
158
+ position?: "top" | "right" | "bottom" | "left";
151
159
  className?: string;
152
160
  props: any;
153
161
  };
162
+ type RadioButtonLabelObject = {
163
+ true?: string;
164
+ false?: string;
165
+ };
166
+ export type RadioButtonFieldProps = {
167
+ label?: RadioButtonLabelObject;
168
+ inputProps?: any;
169
+ name?: React.ReactNode;
170
+ className?: string;
171
+ };
172
+ export type ToggleSwitchFieldProps = {
173
+ inputProps: any;
174
+ name: React.ReactNode;
175
+ className?: string;
176
+ };
177
+ export type SelectFieldProps = {
178
+ name: string;
179
+ enums: any;
180
+ emptySearchMessage?: string;
181
+ placeholder?: string;
182
+ closeElement?: React.ReactNode;
183
+ className?: string;
184
+ };
154
185
  /*** ***/
155
186
  export type AjvError = {
156
187
  message: string;
package/dist/utils.d.ts CHANGED
@@ -11,6 +11,13 @@ export declare function getDescription({ description, uiSchema, schema, }: {
11
11
  uiSchema?: UiSchema;
12
12
  schema?: Schema;
13
13
  }): any;
14
+ export declare function getEnums({ schema }: FieldProps): {
15
+ type: string;
16
+ values: {
17
+ const: any;
18
+ title: string;
19
+ }[];
20
+ } | undefined;
14
21
  export declare function getDefaultValueForType(type: JSONSchema7["type"]): {} | null;
15
22
  export declare function useSetDefault(props: FieldProps): void;
16
23
  export declare function useExtractUiSchema(props?: UiSchema | undefined): {
package/dist/utils.js CHANGED
@@ -1,14 +1,3 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  var __rest = (this && this.__rest) || function (s, e) {
13
2
  var t = {};
14
3
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -22,23 +11,51 @@ var __rest = (this && this.__rest) || function (s, e) {
22
11
  };
23
12
  import { useField } from "formik";
24
13
  import { useEffect, useMemo } from "react";
25
- export function getTitle(_a) {
26
- var _b;
27
- var title = _a.title, uiSchema = _a.uiSchema, schema = _a.schema, name = _a.name;
14
+ export function getTitle({ title, uiSchema, schema, name, }) {
15
+ var _a;
28
16
  return (title ||
29
- ((_b = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _b === void 0 ? void 0 : _b.title) ||
17
+ ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.title) ||
30
18
  (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:title"]) ||
31
19
  (schema === null || schema === void 0 ? void 0 : schema.title) ||
32
20
  (name === null || name === void 0 ? void 0 : name.split(".")[(name === null || name === void 0 ? void 0 : name.split(".").length) - 1]));
33
21
  }
34
- export function getDescription(_a) {
35
- var _b;
36
- var description = _a.description, uiSchema = _a.uiSchema, schema = _a.schema;
22
+ export function getDescription({ description, uiSchema, schema, }) {
23
+ var _a;
37
24
  return (description ||
38
- ((_b = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _b === void 0 ? void 0 : _b.description) ||
25
+ ((_a = uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:options"]) === null || _a === void 0 ? void 0 : _a.description) ||
39
26
  (uiSchema === null || uiSchema === void 0 ? void 0 : uiSchema["ui:description"]) ||
40
27
  (schema === null || schema === void 0 ? void 0 : schema.description));
41
28
  }
29
+ export function getEnums({ schema }) {
30
+ const numberOfDefined = [schema.enum, schema.oneOf, schema.anyOf].filter((x) => x).length;
31
+ if (numberOfDefined === 0) {
32
+ return;
33
+ }
34
+ if (numberOfDefined > 1) {
35
+ throw new Error('Only one of "enum", "oneOf" or "anyOf" can be defined!');
36
+ }
37
+ if (schema.enum) {
38
+ return {
39
+ type: "select",
40
+ values: schema.enum.map((option) => ({
41
+ const: option,
42
+ title: `${option}`,
43
+ })),
44
+ };
45
+ }
46
+ if (schema.oneOf) {
47
+ return {
48
+ type: "select",
49
+ values: schema.oneOf,
50
+ };
51
+ }
52
+ if (schema.anyOf) {
53
+ return {
54
+ type: "multiSelect",
55
+ values: schema.anyOf,
56
+ };
57
+ }
58
+ }
42
59
  export function getDefaultValueForType(type) {
43
60
  if (type && !Array.isArray(type)) {
44
61
  if (type === "array")
@@ -49,8 +66,8 @@ export function getDefaultValueForType(type) {
49
66
  return null;
50
67
  }
51
68
  export function useSetDefault(props) {
52
- var _a = useField(props.name), value = _a[0].value, _ = _a[1], setValue = _a[2].setValue;
53
- useEffect(function () {
69
+ const [{ value }, _, { setValue }] = useField(props.name);
70
+ useEffect(() => {
54
71
  if (!value) {
55
72
  if (props.schema.default) {
56
73
  setValue(props.schema.default);
@@ -61,20 +78,16 @@ export function useSetDefault(props) {
61
78
  }
62
79
  }, []);
63
80
  }
64
- export function useExtractUiSchema(props) {
65
- if (props === void 0) { props = {}; }
81
+ export function useExtractUiSchema(props = {}) {
66
82
  if (props.props !== undefined) {
67
- props["ui:props"] = __assign(__assign({}, props["ui:props"]), props.props);
68
- }
69
- return useMemo(function () {
70
- var _a = props["ui:options"], options = _a === void 0 ? {} : _a, uiSchema = __rest(props, ["ui:options"]);
71
- return __assign(__assign({}, Object.keys(uiSchema || {}).reduce(function (result, key) {
72
- var _a;
73
- return (__assign(__assign({}, result), (_a = {}, _a[key.replace("ui:", "")] = uiSchema[key], _a)));
74
- }, {})), options);
83
+ props["ui:props"] = Object.assign(Object.assign({}, props["ui:props"]), props.props);
84
+ }
85
+ return useMemo(() => {
86
+ const { "ui:options": options = {} } = props, uiSchema = __rest(props, ["ui:options"]);
87
+ return Object.assign(Object.assign({}, Object.keys(uiSchema || {}).reduce((result, key) => (Object.assign(Object.assign({}, result), { [key.replace("ui:", "")]: uiSchema[key] })), {})), options);
75
88
  }, [props]);
76
89
  }
77
- export var guessType = function guessType(value) {
90
+ export const guessType = function guessType(value) {
78
91
  if (Array.isArray(value)) {
79
92
  return "array";
80
93
  }
@@ -100,7 +113,7 @@ export function getSchemaType(schema) {
100
113
  if (!schema) {
101
114
  return undefined;
102
115
  }
103
- var type = schema.type;
116
+ let { type } = schema;
104
117
  if (!type && schema.const) {
105
118
  return guessType(schema.const);
106
119
  }
@@ -111,15 +124,15 @@ export function getSchemaType(schema) {
111
124
  return "object";
112
125
  }
113
126
  if (type instanceof Array && type.length === 2 && type.includes("null")) {
114
- return type.find(function (type) { return type !== "null"; });
127
+ return type.find((type) => type !== "null");
115
128
  }
116
129
  return type;
117
130
  }
118
131
  //TODO: REMOVE WHEN ALL SUPPORTED
119
132
  export function warnFromUnsupportedKeys(componentName, data, supportedKeys) {
120
- Object.keys(data).forEach(function (key) {
133
+ Object.keys(data).forEach((key) => {
121
134
  if (!supportedKeys.includes(key)) {
122
- console.warn("Component \"".concat(componentName, "\" does not support (yet) key \"").concat(key, "\""));
135
+ console.warn(`Component "${componentName}" does not support (yet) key "${key}"`);
123
136
  }
124
137
  });
125
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [