@nolwenture/formik-jsonschema-form 0.9.3 → 0.10.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 CHANGED
@@ -420,7 +420,7 @@ const uiSchema = {
420
420
  ### Alternative fields
421
421
 
422
422
  There are alternative fields available for certain types.
423
- For `type: "string"` there is `DateField` and `MultipleChoiceField`.
423
+ For `type: "string"` there is `DateField`, `MultipleChoiceField` and `TextAreaField`.
424
424
 
425
425
  `DateField` has a "dd.mm.yyyy" date input field and a calendar date picker and will save the date input as "yyyy-mm-dd" string.
426
426
 
@@ -465,6 +465,20 @@ const uiSchema = {
465
465
  };
466
466
  ```
467
467
 
468
+ `TextAreaField` gives more space for a longer string input.
469
+
470
+ ```tsx
471
+ const schema = {
472
+ properties: {
473
+ story: { type: "string", title: "A short story" },
474
+ },
475
+ };
476
+
477
+ const uiSchema = {
478
+ story: { "ui:field": "TextAreaField" },
479
+ };
480
+ ```
481
+
468
482
  For `type: "boolean"` there is `RadioField` and `ToggleField`.
469
483
  `RadioField` can take labels for the "true" and "false" options. If left empty, the default labels will be "true" and "false".
470
484
 
@@ -0,0 +1,3 @@
1
+ import type { FieldProps } from "../types";
2
+ import React from "react";
3
+ export declare const TextAreaField: React.FC<FieldProps>;
@@ -0,0 +1,12 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useContext } from "react";
3
+ import { FormRegistry } from "../Form";
4
+ import { useExtractUiSchema, useSetDefault } from "../hooks";
5
+ import { TextAreaInputField } from "../styledComponents";
6
+ export const TextAreaField = (props) => {
7
+ const { templates: { FieldTemplate }, } = useContext(FormRegistry);
8
+ useSetDefault(props);
9
+ const { name, uiSchema } = props;
10
+ const { props: inputProps } = useExtractUiSchema(uiSchema);
11
+ return (_jsx(FieldTemplate, Object.assign({}, props, { children: _jsx(TextAreaInputField, { inputProps: inputProps, name: name }) })));
12
+ };
@@ -11,3 +11,4 @@ export * from "./RadioField";
11
11
  export * from "./ToggleField";
12
12
  export * from "./SearchMultiTagSelectField";
13
13
  export * from "./MultipleChoiceField";
14
+ export * from "./TextAreaField";
@@ -11,3 +11,4 @@ export * from "./RadioField";
11
11
  export * from "./ToggleField";
12
12
  export * from "./SearchMultiTagSelectField";
13
13
  export * from "./MultipleChoiceField";
14
+ export * from "./TextAreaField";
@@ -0,0 +1,5 @@
1
+ import { StoryObj } from "@storybook/react";
2
+ import { TextAreaField } from "../TextAreaField";
3
+ declare const _default: import("@storybook/types").ComponentAnnotations<import("@storybook/react").ReactRenderer, import("../../types").FieldProps>;
4
+ export default _default;
5
+ export declare const TextAreaInput: StoryObj<typeof TextAreaField>;
@@ -0,0 +1,34 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { jsx as _jsx } from "react/jsx-runtime";
11
+ import { userEvent, expect } from "@storybook/test";
12
+ import { TextAreaField } from "../TextAreaField";
13
+ import { Formik, Form } from "formik";
14
+ export default {
15
+ title: "Fields/StringField",
16
+ component: TextAreaField,
17
+ decorators: [
18
+ (Story) => (_jsx(Formik, { initialValues: { name: "" }, onSubmit: () => { }, children: _jsx(Form, { children: Story() }) })),
19
+ ],
20
+ };
21
+ // Story for a regular text input
22
+ export const TextAreaInput = {
23
+ args: {
24
+ name: "TextAreaField",
25
+ schema: { type: "string" },
26
+ uiSchema: { "ui:field": "TextAreaField" },
27
+ },
28
+ play: (_a) => __awaiter(void 0, [_a], void 0, function* ({ canvasElement }) {
29
+ const input = canvasElement.querySelector('textarea[name="TextAreaField"]');
30
+ expect(input).toBeInTheDocument();
31
+ yield userEvent.type(input, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Arcu felis bibendum ut tristique et egestas quis. Urna duis convallis convallis tellus id interdum velit laoreet. Nisl condimentum id venenatis a condimentum vitae sapien. Massa sed elementum tempus egestas sed sed risus pretium. Nec feugiat nisl pretium fusce id velit. Vitae aliquet nec ullamcorper sit amet. Imperdiet sed euismod nisi porta lorem mollis aliquam. Tellus mauris a diam maecenas. Mauris pellentesque pulvinar pellentesque habitant morbi tristique senectus et. Id diam vel quam elementum pulvinar etiam. Urna id volutpat lacus laoreet non curabitur gravida. Faucibus purus in massa tempor nec feugiat nisl pretium. In hac habitasse platea dictumst vestibulum. Dui accumsan sit amet nulla. Est lorem ipsum dolor sit amet consectetur adipiscing elit pellentesque.");
32
+ yield expect(input).toHaveValue("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Arcu felis bibendum ut tristique et egestas quis. Urna duis convallis convallis tellus id interdum velit laoreet. Nisl condimentum id venenatis a condimentum vitae sapien. Massa sed elementum tempus egestas sed sed risus pretium. Nec feugiat nisl pretium fusce id velit. Vitae aliquet nec ullamcorper sit amet. Imperdiet sed euismod nisi porta lorem mollis aliquam. Tellus mauris a diam maecenas. Mauris pellentesque pulvinar pellentesque habitant morbi tristique senectus et. Id diam vel quam elementum pulvinar etiam. Urna id volutpat lacus laoreet non curabitur gravida. Faucibus purus in massa tempor nec feugiat nisl pretium. In hac habitasse platea dictumst vestibulum. Dui accumsan sit amet nulla. Est lorem ipsum dolor sit amet consectetur adipiscing elit pellentesque.");
33
+ }),
34
+ };
@@ -2,6 +2,9 @@ import { useEffect } from "react";
2
2
  import { useField } from "formik";
3
3
  import { getDefaultValueForType } from "../utils/getTypes";
4
4
  export const useSetDefault = (props) => {
5
+ if (!props.name) {
6
+ return;
7
+ }
5
8
  const [{ value }, _, { setValue }] = useField(props.name);
6
9
  useEffect(() => {
7
10
  if (!value) {
@@ -2,7 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  export const completeSchema = {
3
3
  title: "Inventory",
4
4
  type: "object",
5
- required: ["string", "number", "boolean", "enum"],
5
+ required: ["string", "number", "boolean", "enum", "textArea"],
6
6
  properties: {
7
7
  string: {
8
8
  type: "string",
@@ -186,6 +186,11 @@ export const completeSchema = {
186
186
  },
187
187
  default: [{ string: "Hello" }],
188
188
  },
189
+ textArea: {
190
+ type: "string",
191
+ title: "TextArea Field",
192
+ description: "TextArea field description",
193
+ },
189
194
  },
190
195
  };
191
196
  export const completeUiSchema = {
@@ -288,4 +293,5 @@ export const completeUiSchema = {
288
293
  emptySearchMessage: "...no matching results found",
289
294
  },
290
295
  },
296
+ textArea: { "ui:field": "TextAreaField" },
291
297
  };
@@ -1,5 +1,5 @@
1
1
  /// <reference types="hoist-non-react-statics" />
2
- import type { ArrayFieldButtonProps, InfoFieldButtonProps, ObjectFieldButtonProps, RadioButtonFieldProps, ToggleSwitchFieldProps, SelectFieldProps, MultipleChoiceFieldProps } from "./types";
2
+ import type { ArrayFieldButtonProps, InfoFieldButtonProps, ObjectFieldButtonProps, RadioButtonFieldProps, ToggleSwitchFieldProps, SelectFieldProps, MultipleChoiceFieldProps, TextAreaInputFieldProps } from "./types";
3
3
  import React from "react";
4
4
  import * as formik from "formik";
5
5
  export declare const Form: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<Omit<Omit<React.DetailedHTMLProps<React.FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, "ref"> & React.RefAttributes<HTMLFormElement>, "ref"> & {
@@ -200,4 +200,12 @@ export declare const SearchableMultiTagSelectField: {
200
200
  MultiSelectOptionListWithoutItemStyle: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>, never>>;
201
201
  };
202
202
  };
203
+ export declare const TextAreaFieldInput: import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<Omit<import("styled-components/dist/types").FastOmit<Omit<Omit<any, "as">, "ref"> & {
204
+ ref?: unknown;
205
+ }, never>, "ref"> & {
206
+ ref?: unknown;
207
+ }, never>> & Omit<import("styled-components").IStyledComponent<"web", import("styled-components/dist/types").FastOmit<Omit<Omit<any, "as">, "ref"> & {
208
+ ref?: unknown;
209
+ }, never>> & Omit<({ formikAs, ...props }: Omit<formik.FieldAttributes<any>, "as">) => import("react/jsx-runtime").JSX.Element, keyof React.Component<any, {}, any>>, keyof React.Component<any, {}, any>>;
210
+ export declare const TextAreaInputField: ({ inputProps, name, className, }: TextAreaInputFieldProps) => import("react/jsx-runtime").JSX.Element;
203
211
  export {};
@@ -386,3 +386,10 @@ SearchableMultiTagSelectField.styles = {
386
386
  MultiSelectOptionListItemStyle: MultiSelectOptionListItem,
387
387
  MultiSelectOptionListWithoutItemStyle: MultiSelectOptionListWithoutItem,
388
388
  };
389
+ export const TextAreaFieldInput = styled(FieldInput) `
390
+ width: 100%;
391
+ min-height: 5rem;
392
+ `;
393
+ export const TextAreaInputField = ({ inputProps, name, className, }) => {
394
+ return _jsx(TextAreaFieldInput, Object.assign({}, inputProps, { name: name, formikAs: "textarea" }));
395
+ };
package/dist/types.d.ts CHANGED
@@ -132,6 +132,7 @@ export type ComponentsType = {
132
132
  MultipleChoiceField: React.FunctionComponent<any>;
133
133
  ToggleSwitchField: React.FunctionComponent<any>;
134
134
  SearchableMultiTagSelectField: React.FunctionComponent<any>;
135
+ TextAreaInputField: React.FunctionComponent<any>;
135
136
  };
136
137
  export declare enum ObjectFieldButtonPurpose {
137
138
  ADD = 0,
@@ -172,6 +173,11 @@ export type RadioButtonFieldProps = {
172
173
  name?: React.ReactNode;
173
174
  className?: string;
174
175
  };
176
+ export type TextAreaInputFieldProps = {
177
+ inputProps?: any;
178
+ name: string;
179
+ className?: string;
180
+ };
175
181
  type Enum = {
176
182
  type: string;
177
183
  values: {
@@ -2,6 +2,9 @@ import type { FieldProps } from "../types";
2
2
  export declare const getEnums: ({ schema }: {
3
3
  schema: FieldProps["schema"];
4
4
  }) => {
5
+ values: never[];
6
+ type?: undefined;
7
+ } | {
5
8
  type: string;
6
9
  values: {
7
10
  const: any;
@@ -1,4 +1,7 @@
1
1
  export const getEnums = ({ schema }) => {
2
+ if (!schema) {
3
+ return { values: [] };
4
+ }
2
5
  const numberOfDefined = [schema.enum, schema.oneOf, schema.anyOf].filter((x) => x).length;
3
6
  if (numberOfDefined === 0) {
4
7
  return;
@@ -59,12 +59,14 @@ export const resolveSchema = (_a, value) => {
59
59
  return schema;
60
60
  }, schemaCopy);
61
61
  }
62
- const usedProperties = Object.keys((_b = schemaCopy.properties) !== null && _b !== void 0 ? _b : {});
63
- Object.keys(value).map((key) => {
64
- if (!usedProperties.includes(key)) {
65
- valuesToRemoveDeps.push(key);
66
- }
67
- });
62
+ if (dependencies) {
63
+ const usedProperties = Object.keys((_b = schemaCopy.properties) !== null && _b !== void 0 ? _b : {});
64
+ Object.keys(value).map((key) => {
65
+ if (!usedProperties.includes(key)) {
66
+ valuesToRemoveDeps.push(key);
67
+ }
68
+ });
69
+ }
68
70
  if (!allOf && dependencies) {
69
71
  return [schemaCopy, valuesToRemoveDeps];
70
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "0.9.3",
3
+ "version": "0.10.0",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "Antti Vikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [