@nolwenture/formik-jsonschema-form 1.3.0 → 1.3.2

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.
@@ -124,11 +124,7 @@ const BooleanFieldValue = ({ name, className, value, label, }) => {
124
124
  return ((0, jsx_runtime_1.jsx)("div", { className: className, children: value === true ? ((0, jsx_runtime_1.jsxs)(Selected, { children: [(0, jsx_runtime_1.jsx)(SelectedBox, { children: (0, jsx_runtime_1.jsx)(exports.SelectedIcon, {}) }), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: label !== undefined ? label : "Selected" })] })) : ((0, jsx_runtime_1.jsxs)(Selected, { children: [(0, jsx_runtime_1.jsx)(NotSelectedBox, {}), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: label !== undefined ? label : "Not selected" })] })) }));
125
125
  };
126
126
  exports.BooleanFieldValue = BooleanFieldValue;
127
- const MultipleChoiceFieldValue = ({ inputProps, name, className, style, selected, notSelected }) => {
128
- return ((0, jsx_runtime_1.jsx)(exports.MultipleChoiceFieldWrapper, { children: (0, jsx_runtime_1.jsxs)(styledComponents_1.MultipleChoiceFieldContainer, { className: className, style: (0, getStyle_1.getStyle)(style ?? ""), children: [!selected && !notSelected && (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, {}), selected &&
129
- selected.length > 0 &&
130
- selected.map((s) => ((0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldElement, { "data-fjsf-enum-value-view": s.const, children: (0, jsx_runtime_1.jsxs)(Selected, { children: [(0, jsx_runtime_1.jsx)(SelectedBox, { children: (0, jsx_runtime_1.jsx)(exports.SelectedIcon, {}) }), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: s.title ?? "Hello" })] }) }, s.title ?? s))), notSelected &&
131
- notSelected?.length > 0 &&
132
- notSelected.map((n) => ((0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldElement, { children: (0, jsx_runtime_1.jsxs)(NotSelected, { children: [(0, jsx_runtime_1.jsx)(NotSelectedBox, {}), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: n.title })] }) }, n.title ?? n)))] }) }));
127
+ const MultipleChoiceFieldValue = ({ inputProps, name, className, style, values }) => {
128
+ return ((0, jsx_runtime_1.jsx)(exports.MultipleChoiceFieldWrapper, { children: (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldContainer, { className: className, style: (0, getStyle_1.getStyle)(style ?? ""), children: values.map((item) => ((0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldElement, { "data-fjsf-enum-value-view": item.const, children: item.selected ? ((0, jsx_runtime_1.jsxs)(Selected, { children: [(0, jsx_runtime_1.jsx)(SelectedBox, { children: (0, jsx_runtime_1.jsx)(exports.SelectedIcon, {}) }), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: item.title })] })) : ((0, jsx_runtime_1.jsxs)(NotSelected, { children: [(0, jsx_runtime_1.jsx)(NotSelectedBox, {}), (0, jsx_runtime_1.jsx)(styledComponents_1.MultipleChoiceFieldLabel, { children: item.title })] })) }, item.const))) }) }));
133
129
  };
134
130
  exports.MultipleChoiceFieldValue = MultipleChoiceFieldValue;
@@ -9,16 +9,19 @@ const FormViewRegistry_1 = require("../FormViewRegistry");
9
9
  const MultipleChoiceField = (props) => {
10
10
  const { templates, components: { MultipleChoiceFieldValue }, } = (0, react_1.useContext)(FormViewRegistry_1.FormViewRegistry);
11
11
  const { name, uiSchema } = props;
12
- const { props: inputProps, style, onlyShowSelected, } = (0, react_1.useMemo)(() => (0, utils_1.extractUiSchema)(uiSchema), [uiSchema]);
12
+ const { props: inputProps, style } = (0, react_1.useMemo)(() => (0, utils_1.extractUiSchema)(uiSchema), [uiSchema]);
13
13
  const enums = (0, react_1.useMemo)(() => (0, utils_1.getEnums)(props), [props]);
14
14
  const { values } = (0, formik_1.useFormikContext)();
15
15
  const value = (0, formik_1.getIn)(values, name);
16
- const selected = (0, utils_1.getSelectedEnum)(value, enums);
17
- const notSelected = enums?.values?.filter((v) => Array.isArray(value) && !value.includes(v.const));
18
- const hideNotSelected = onlyShowSelected === true || selected === undefined;
16
+ const allValues = enums?.values?.map((v) => ({
17
+ ...v,
18
+ selected: Array.isArray(value)
19
+ ? value.includes(v.const)
20
+ : value === v.const,
21
+ })) ?? [];
19
22
  const SelectedFieldTemplate = (props.uiSchema?.["ui:template"] &&
20
23
  templates[props.uiSchema?.["ui:template"]]) ??
21
24
  templates.FieldTemplate;
22
- return ((0, jsx_runtime_1.jsx)(SelectedFieldTemplate, { ...props, children: (0, jsx_runtime_1.jsx)(MultipleChoiceFieldValue, { inputProps: inputProps, name: name, style: style, selected: selected, notSelected: hideNotSelected ? undefined : notSelected }) }));
25
+ return ((0, jsx_runtime_1.jsx)(SelectedFieldTemplate, { ...props, children: (0, jsx_runtime_1.jsx)(MultipleChoiceFieldValue, { inputProps: inputProps, name: name, style: style, values: allValues }) }));
23
26
  };
24
27
  exports.MultipleChoiceField = MultipleChoiceField;
@@ -10,8 +10,8 @@ const setTitleValues = (name, title, defaultTitle, values) => {
10
10
  }
11
11
  const index = match ? parseInt(match[2]) : -1;
12
12
  const itemValues = match ? (0, formik_1.getIn)(values, name)[index] : (0, formik_1.getIn)(values, name);
13
- return title.replace(/\$\{([\w$.]+)\}/g, (match) => {
14
- const keyMatch = match.match(/^\$\{([\w$.]+)\}$/);
13
+ return title.replace(/\$\{([\w$.:]+)\}/g, (match) => {
14
+ const keyMatch = match.match(/^\$\{([\w$.:]+)\}$/);
15
15
  const key = keyMatch ? keyMatch[1] : "";
16
16
  return key === "index"
17
17
  ? (index + 1).toString()
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import styled from "styled-components";
3
3
  import { MultipleChoiceFieldContainer, MultipleChoiceFieldElement, MultipleChoiceFieldLabel, } from "../../Form/fields/MultipleChoiceField/styledComponents";
4
4
  import { getStyle } from "../../utils/getStyle";
@@ -117,10 +117,6 @@ export const MultipleChoiceFieldWrapper = styled.div `
117
117
  export const BooleanFieldValue = ({ name, className, value, label, }) => {
118
118
  return (_jsx("div", { className: className, children: value === true ? (_jsxs(Selected, { children: [_jsx(SelectedBox, { children: _jsx(SelectedIcon, {}) }), _jsx(MultipleChoiceFieldLabel, { children: label !== undefined ? label : "Selected" })] })) : (_jsxs(Selected, { children: [_jsx(NotSelectedBox, {}), _jsx(MultipleChoiceFieldLabel, { children: label !== undefined ? label : "Not selected" })] })) }));
119
119
  };
120
- export const MultipleChoiceFieldValue = ({ inputProps, name, className, style, selected, notSelected }) => {
121
- return (_jsx(MultipleChoiceFieldWrapper, { children: _jsxs(MultipleChoiceFieldContainer, { className: className, style: getStyle(style ?? ""), children: [!selected && !notSelected && _jsx(_Fragment, {}), selected &&
122
- selected.length > 0 &&
123
- selected.map((s) => (_jsx(MultipleChoiceFieldElement, { "data-fjsf-enum-value-view": s.const, children: _jsxs(Selected, { children: [_jsx(SelectedBox, { children: _jsx(SelectedIcon, {}) }), _jsx(MultipleChoiceFieldLabel, { children: s.title ?? "Hello" })] }) }, s.title ?? s))), notSelected &&
124
- notSelected?.length > 0 &&
125
- notSelected.map((n) => (_jsx(MultipleChoiceFieldElement, { children: _jsxs(NotSelected, { children: [_jsx(NotSelectedBox, {}), _jsx(MultipleChoiceFieldLabel, { children: n.title })] }) }, n.title ?? n)))] }) }));
120
+ export const MultipleChoiceFieldValue = ({ inputProps, name, className, style, values }) => {
121
+ return (_jsx(MultipleChoiceFieldWrapper, { children: _jsx(MultipleChoiceFieldContainer, { className: className, style: getStyle(style ?? ""), children: values.map((item) => (_jsx(MultipleChoiceFieldElement, { "data-fjsf-enum-value-view": item.const, children: item.selected ? (_jsxs(Selected, { children: [_jsx(SelectedBox, { children: _jsx(SelectedIcon, {}) }), _jsx(MultipleChoiceFieldLabel, { children: item.title })] })) : (_jsxs(NotSelected, { children: [_jsx(NotSelectedBox, {}), _jsx(MultipleChoiceFieldLabel, { children: item.title })] })) }, item.const))) }) }));
126
122
  };
@@ -1,20 +1,23 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext, useMemo } from "react";
3
3
  import { getIn, useFormikContext } from "formik";
4
- import { extractUiSchema, getEnums, getSelectedEnum } from "../../utils";
4
+ import { extractUiSchema, getEnums } from "../../utils";
5
5
  import { FormViewRegistry } from "../FormViewRegistry";
6
6
  export const MultipleChoiceField = (props) => {
7
7
  const { templates, components: { MultipleChoiceFieldValue }, } = useContext(FormViewRegistry);
8
8
  const { name, uiSchema } = props;
9
- const { props: inputProps, style, onlyShowSelected, } = useMemo(() => extractUiSchema(uiSchema), [uiSchema]);
9
+ const { props: inputProps, style } = useMemo(() => extractUiSchema(uiSchema), [uiSchema]);
10
10
  const enums = useMemo(() => getEnums(props), [props]);
11
11
  const { values } = useFormikContext();
12
12
  const value = getIn(values, name);
13
- const selected = getSelectedEnum(value, enums);
14
- const notSelected = enums?.values?.filter((v) => Array.isArray(value) && !value.includes(v.const));
15
- const hideNotSelected = onlyShowSelected === true || selected === undefined;
13
+ const allValues = enums?.values?.map((v) => ({
14
+ ...v,
15
+ selected: Array.isArray(value)
16
+ ? value.includes(v.const)
17
+ : value === v.const,
18
+ })) ?? [];
16
19
  const SelectedFieldTemplate = (props.uiSchema?.["ui:template"] &&
17
20
  templates[props.uiSchema?.["ui:template"]]) ??
18
21
  templates.FieldTemplate;
19
- return (_jsx(SelectedFieldTemplate, { ...props, children: _jsx(MultipleChoiceFieldValue, { inputProps: inputProps, name: name, style: style, selected: selected, notSelected: hideNotSelected ? undefined : notSelected }) }));
22
+ return (_jsx(SelectedFieldTemplate, { ...props, children: _jsx(MultipleChoiceFieldValue, { inputProps: inputProps, name: name, style: style, values: allValues }) }));
20
23
  };
@@ -7,8 +7,8 @@ export const setTitleValues = (name, title, defaultTitle, values) => {
7
7
  }
8
8
  const index = match ? parseInt(match[2]) : -1;
9
9
  const itemValues = match ? getIn(values, name)[index] : getIn(values, name);
10
- return title.replace(/\$\{([\w$.]+)\}/g, (match) => {
11
- const keyMatch = match.match(/^\$\{([\w$.]+)\}$/);
10
+ return title.replace(/\$\{([\w$.:]+)\}/g, (match) => {
11
+ const keyMatch = match.match(/^\$\{([\w$.:]+)\}$/);
12
12
  const key = keyMatch ? keyMatch[1] : "";
13
13
  return key === "index"
14
14
  ? (index + 1).toString()
@@ -19,14 +19,11 @@ type BooleanValueFieldProps = {
19
19
  };
20
20
  export declare const BooleanFieldValue: React.FC<BooleanValueFieldProps>;
21
21
  type MultipleChoiceFieldValueProps = {
22
- selected: {
22
+ values: {
23
23
  const: string | number;
24
24
  title: string;
25
- }[] | undefined;
26
- notSelected: {
27
- const: string | number;
28
- title: string;
29
- }[] | undefined;
25
+ selected: boolean;
26
+ }[];
30
27
  inputProps?: any;
31
28
  name?: React.ReactNode;
32
29
  className?: string;
@@ -23,14 +23,11 @@ export declare const components: {
23
23
  label?: string;
24
24
  }>;
25
25
  MultipleChoiceFieldValue: React.FC<{
26
- selected: {
26
+ values: {
27
27
  const: string | number;
28
28
  title: string;
29
- }[] | undefined;
30
- notSelected: {
31
- const: string | number;
32
- title: string;
33
- }[] | undefined;
29
+ selected: boolean;
30
+ }[];
34
31
  inputProps?: any;
35
32
  name?: React.ReactNode;
36
33
  className?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nolwenture/formik-jsonschema-form",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Formik form generation from json schema",
5
5
  "author": "anttivikman <antti.vikman@nolwenture.com>",
6
6
  "contributors": [
@@ -125,4 +125,4 @@
125
125
  "last 1 safari version"
126
126
  ]
127
127
  }
128
- }
128
+ }