oneOf not producing select field or multiple choice checklist in vanilla renderers

oneOf is not producing the desired select field and multiple choice checklist, only a basic input field where there should be a checklist and nothing for the multiple choice.

"@jsonforms/core": "^3.5.1"
"@jsonforms/react": "^3.5.1"
"@jsonforms/vanilla-renderers": "^3.5.1"

const schema = {
  type: "object",
  properties: {
    department: {
      type: "string",
      oneOf: [
        {
          const: "hr",
          title: "Human Resources",
        },
        {
          const: "finance",
          title: "Financial Department",
        },
        {
          const: "it",
          title: "Information Technology",
        },
      ],
    },
    savingsOptions: {
      type: "array",
      uniqueItems: true,
      items: {
        oneOf: [
          {
            const: "checking",
            title: "Checking account",
          },
          {
            const: "savings",
            title: "Savings account",
          },
          {
            const: "roth",
            title: "Roth IRA",
          },
        ],
      },
    },
  },
};

const uischema = {
  type: "VerticalLayout",
  elements: [
    {
      type: "Control",
      scope: "#/properties/department",
      label: "Select Department",
      options: {
        placeholder: "Choose a department",
      },
    },
    {
      type: "Control",
      scope: "#/properties/savingsOptions",
      label: "Select savings options",
    },
  ],
};

Here’s a codesandbox

Hi @edkahara,

As indicated here, we currently do not include a oneOf/Enum renderer for React Vanilla. However we actually include radio group, to enable it, use

"options": {
  "format": "radio",
}

or

"options": {
  "format": "radio",
  "orientation": "vertical"
}

However supporting oneOf-enum should be straightforward in React Vanilla. Basically copy this cell, and then bind it against oneOf-enums instead of enum. Feel free to contribute this enhancement :wink:

I tried patching the package locally to see if creating a OneOfEnumCell would work but it didn’t. Here’s my code for it.

cells/OneOfEnumCell.tsx

import React, { useMemo } from 'react';
import {
  EnumCellProps,
  isOneOfEnumControl,
  RankedTester,
  rankWith,
} from '@jsonforms/core';
import {
  TranslateProps,
  withJsonFormsOneOfEnumCellProps,
  withTranslateProps,
} from '@jsonforms/react';
import { i18nDefaults, withVanillaEnumCellProps } from '../util';
import type { VanillaRendererProps } from '../index';

export const OneOfEnumCell = (
  props: EnumCellProps & VanillaRendererProps & TranslateProps
) => {
  const {
    data,
    className,
    id,
    enabled,
    schema,
    uischema,
    path,
    handleChange,
    options,
    t,
  } = props;
  const noneOptionLabel = useMemo(
    () => t('enum.none', i18nDefaults['enum.none'], { schema, uischema, path }),
    [t, schema, uischema, path]
  );

  return (
    <select
      className={className}
      id={id}
      disabled={!enabled}
      autoFocus={uischema.options && uischema.options.focus}
      value={data || ''}
      onChange={(ev) =>
        handleChange(
          path,
          ev.target.selectedIndex === 0 ? undefined : ev.target.value
        )
      }
    >
      {[
        <option value={''} key={'jsonforms.enum.none'}>
          {noneOptionLabel}
        </option>,
      ].concat(
        options.map((optionValue) => (
          <option
            value={optionValue.value}
            label={optionValue.label}
            key={optionValue.value}
          />
        ))
      )}
    </select>
  );
};
/**
 * Default tester for enum controls.
 * @type {RankedTester}
 */
export const oneOfEnumCellTester: RankedTester = rankWith(2, isOneOfEnumControl);

export default withJsonFormsOneOfEnumCellProps(
  withTranslateProps(withVanillaEnumCellProps(OneOfEnumCell))
);

cells/index.ts

import BooleanCell, { booleanCellTester } from './BooleanCell';
import DateCell, { dateCellTester } from './DateCell';
import DateTimeCell, { dateTimeCellTester } from './DateTimeCell';
import EnumCell, { enumCellTester } from './EnumCell';
import OneOfEnumCell, { oneOfEnumCellTester } from './OneOfEnumCell';
import IntegerCell, { integerCellTester } from './IntegerCell';
import NumberCell, { numberCellTester } from './NumberCell';
import NumberFormatCell, { numberFormatCellTester } from './NumberFormatCell';
import SliderCell, { sliderCellTester } from './SliderCell';
import TextCell, { textCellTester } from './TextCell';
import TextAreaCell, { textAreaCellTester } from './TextAreaCell';
import TimeCell, { timeCellTester } from './TimeCell';
import * as Customizable from './CustomizableCells';

export {
  BooleanCell,
  booleanCellTester,
  DateCell,
  dateCellTester,
  DateTimeCell,
  dateTimeCellTester,
  EnumCell,
  enumCellTester,
  OneOfEnumCell,
  oneOfEnumCellTester,
  IntegerCell,
  integerCellTester,
  NumberCell,
  numberCellTester,
  NumberFormatCell,
  numberFormatCellTester,
  SliderCell,
  sliderCellTester,
  TextCell,
  textCellTester,
  TextAreaCell,
  textAreaCellTester,
  TimeCell,
  timeCellTester,
};
export { Customizable };

Is there a change I’m missing? Here’s the patch

diff --git a/src/cells/OneOfEnumCell.tsx b/src/cells/OneOfEnumCell.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..3c1e8ae67475604f6a8f5c25b2610805da2ff9a5
--- /dev/null
+++ b/src/cells/OneOfEnumCell.tsx
@@ -0,0 +1,98 @@
+import React, { useMemo } from 'react';
+import {
+  EnumCellProps,
+  isOneOfEnumControl,
+  RankedTester,
+  rankWith,
+} from '@jsonforms/core';
+import {
+  TranslateProps,
+  withJsonFormsOneOfEnumCellProps,
+  withTranslateProps,
+} from '@jsonforms/react';
+import { i18nDefaults, withVanillaEnumCellProps } from '../util';
+import type { VanillaRendererProps } from '../index';
+
+export const OneOfEnumCell = (
+  props: EnumCellProps & VanillaRendererProps & TranslateProps
+) => {
+  const {
+    data,
+    className,
+    id,
+    enabled,
+    schema,
+    uischema,
+    path,
+    handleChange,
+    options,
+    t,
+  } = props;
+  const noneOptionLabel = useMemo(
+    () => t('enum.none', i18nDefaults['enum.none'], { schema, uischema, path }),
+    [t, schema, uischema, path]
+  );
+
+  return (
+    <select
+      className={className}
+      id={id}
+      disabled={!enabled}
+      autoFocus={uischema.options && uischema.options.focus}
+      value={data || ''}
+      onChange={(ev) =>
+        handleChange(
+          path,
+          ev.target.selectedIndex === 0 ? undefined : ev.target.value
+        )
+      }
+    >
+      {[
+        <option value={''} key={'jsonforms.enum.none'}>
+          {noneOptionLabel}
+        </option>,
+      ].concat(
+        options.map((optionValue) => (
+          <option
+            value={optionValue.value}
+            label={optionValue.label}
+            key={optionValue.value}
+          />
+        ))
+      )}
+    </select>
+  );
+};
+/**
+ * Default tester for enum controls.
+ * @type {RankedTester}
+ */
+export const oneOfEnumCellTester: RankedTester = rankWith(2, isOneOfEnumControl);
+
+export default withJsonFormsOneOfEnumCellProps(
+  withTranslateProps(withVanillaEnumCellProps(OneOfEnumCell))
+);
diff --git a/src/cells/index.ts b/src/cells/index.ts
index d098afd0c6c4cbba5325772b2a5211635e1b18c3..5abd2e30ead78792aa0876951c7417f1392bf92a 100644
--- a/src/cells/index.ts
+++ b/src/cells/index.ts
@@ -26,6 +26,7 @@ import BooleanCell, { booleanCellTester } from './BooleanCell';
 import DateCell, { dateCellTester } from './DateCell';
 import DateTimeCell, { dateTimeCellTester } from './DateTimeCell';
 import EnumCell, { enumCellTester } from './EnumCell';
+import OneOfEnumCell, { oneOfEnumCellTester } from './OneOfEnumCell';
 import IntegerCell, { integerCellTester } from './IntegerCell';
 import NumberCell, { numberCellTester } from './NumberCell';
 import NumberFormatCell, { numberFormatCellTester } from './NumberFormatCell';
@@ -44,6 +45,8 @@ export {
   dateTimeCellTester,
   EnumCell,
   enumCellTester,
+  OneOfEnumCell,
+  oneOfEnumCellTester,
   IntegerCell,
   integerCellTester,
   NumberCell,

Hi @edkahara,

Code looks good, however it’s not sufficient to export the cell/tester, you also need to add them to the renderer set here.

Unfortunately, adding that in my patch doesn’t work either.

diff --git a/src/cells/OneOfEnumCell.tsx b/src/cells/OneOfEnumCell.tsx
new file mode 100644
index 0000000000000000000000000000000000000000..3c1e8ae67475604f6a8f5c25b2610805da2ff9a5
--- /dev/null
+++ b/src/cells/OneOfEnumCell.tsx
@@ -0,0 +1,98 @@
+/*
+  The MIT License
+
+  Copyright (c) 2017-2019 EclipseSource Munich
+  https://github.com/eclipsesource/jsonforms
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+import React, { useMemo } from 'react';
+import {
+  EnumCellProps,
+  isOneOfEnumControl,
+  RankedTester,
+  rankWith,
+} from '@jsonforms/core';
+import {
+  TranslateProps,
+  withJsonFormsOneOfEnumCellProps,
+  withTranslateProps,
+} from '@jsonforms/react';
+import { i18nDefaults, withVanillaEnumCellProps } from '../util';
+import type { VanillaRendererProps } from '../index';
+
+export const OneOfEnumCell = (
+  props: EnumCellProps & VanillaRendererProps & TranslateProps
+) => {
+  const {
+    data,
+    className,
+    id,
+    enabled,
+    schema,
+    uischema,
+    path,
+    handleChange,
+    options,
+    t,
+  } = props;
+  const noneOptionLabel = useMemo(
+    () => t('enum.none', i18nDefaults['enum.none'], { schema, uischema, path }),
+    [t, schema, uischema, path]
+  );
+
+  return (
+    <select
+      className={className}
+      id={id}
+      disabled={!enabled}
+      autoFocus={uischema.options && uischema.options.focus}
+      value={data || ''}
+      onChange={(ev) =>
+        handleChange(
+          path,
+          ev.target.selectedIndex === 0 ? undefined : ev.target.value
+        )
+      }
+    >
+      {[
+        <option value={''} key={'jsonforms.enum.none'}>
+          {noneOptionLabel}
+        </option>,
+      ].concat(
+        options.map((optionValue) => (
+          <option
+            value={optionValue.value}
+            label={optionValue.label}
+            key={optionValue.value}
+          />
+        ))
+      )}
+    </select>
+  );
+};
+/**
+ * Default tester for enum controls.
+ * @type {RankedTester}
+ */
+export const oneOfEnumCellTester: RankedTester = rankWith(3, isOneOfEnumControl);
+
+export default withJsonFormsOneOfEnumCellProps(
+  withTranslateProps(withVanillaEnumCellProps(OneOfEnumCell))
+);
diff --git a/src/cells/index.ts b/src/cells/index.ts
index d098afd0c6c4cbba5325772b2a5211635e1b18c3..2cc59015c5fa6a92539bc464ef7d1f75372a2ef0 100644
--- a/src/cells/index.ts
+++ b/src/cells/index.ts
@@ -29,6 +29,7 @@ import EnumCell, { enumCellTester } from './EnumCell';
 import IntegerCell, { integerCellTester } from './IntegerCell';
 import NumberCell, { numberCellTester } from './NumberCell';
 import NumberFormatCell, { numberFormatCellTester } from './NumberFormatCell';
+import OneOfEnumCell, { oneOfEnumCellTester } from './OneOfEnumCell';
 import SliderCell, { sliderCellTester } from './SliderCell';
 import TextCell, { textCellTester } from './TextCell';
 import TextAreaCell, { textAreaCellTester } from './TextAreaCell';
@@ -50,6 +51,8 @@ export {
   numberCellTester,
   NumberFormatCell,
   numberFormatCellTester,
+  OneOfEnumCell,
+  oneOfEnumCellTester,
   SliderCell,
   sliderCellTester,
   TextCell,
diff --git a/src/renderers.ts b/src/renderers.ts
index a432d5db47145594ef26e7ee1fe7aa9ea39120b9..680c31651e12c6cdff071eb96614f739b20fe254 100644
--- a/src/renderers.ts
+++ b/src/renderers.ts
@@ -38,6 +38,8 @@ import {
   integerCellTester,
   NumberCell,
   numberCellTester,
+  OneOfEnumCell,
+  oneOfEnumCellTester,
   SliderCell,
   sliderCellTester,
   TextAreaCell,
@@ -97,6 +99,7 @@ export const vanillaCells: { tester: RankedTester; cell: any }[] = [
   { tester: enumCellTester, cell: EnumCell },
   { tester: integerCellTester, cell: IntegerCell },
   { tester: numberCellTester, cell: NumberCell },
+  { tester: oneOfEnumCellTester, cell: OneOfEnumCell },
   { tester: sliderCellTester, cell: SliderCell },
   { tester: textAreaCellTester, cell: TextAreaCell },
   { tester: textCellTester, cell: TextCell },

Works for me!

If you only patch the @jsonforms/vanilla-renderers in your node_modules directory, then this of course not sufficient. We only provide the source files for debugging support, the actual code is located in lib/jsonforms-react-vanilla.esm.js (or lib/jsonforms-react-vanilla.cjs.js if you’re consuming the commonjs variant).

@edkahara Will you open a contribution to add these renderers to JSON Forms?

Yes, I’ve been pre-occupied with other tasks. I should be able to open it tomorrow. Is that okay with you?

I also want to add the ability to remove the empty option so that if someone doesn’t want it on the field they don’t have to build anything custom.

Hi @edkahara,

Great!

I also want to add the ability to remove the empty option so that if someone doesn’t want it on the field they don’t have to build anything custom.

Fine with me! We should make it configurable via an uischema option.

1 Like