Skip to content

Commit 1d29b35

Browse files
committed
Allow default shortAnswer form input to be excluded with a different string form input as the default
1 parent c972f75 commit 1d29b35

File tree

5 files changed

+88
-9
lines changed

5 files changed

+88
-9
lines changed

example/src/JsonSchemaFormSuite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class JsonSchemaFormEditor extends React.Component<Props, State> {
318318
<PredefinedGallery
319319
schema={this.props.schema}
320320
uischema={this.props.uischema}
321-
mods={{}}
321+
mods={this.props.mods}
322322
onChange={(newSchema: string, newUiSchema: string) => {
323323
if (this.props.onChange)
324324
this.props.onChange(newSchema, newUiSchema);

src/formBuilder/FormBuilder.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
onDragEnd,
1818
countElementsFromSchema,
1919
generateCategoryHash,
20+
excludeKeys,
2021
} from './utils';
2122
import DEFAULT_FORM_INPUTS from './defaults/defaultFormInputs';
2223
import type { Mods } from './types';
@@ -236,10 +237,14 @@ export default function FormBuilder({
236237
const schemaData = (parse(schema): { [string]: any }) || {};
237238
schemaData.type = 'object';
238239
const uiSchemaData = (parse(uischema): { [string]: any }) || {};
239-
const allFormInputs = {
240-
...DEFAULT_FORM_INPUTS,
241-
...(mods && mods.customFormInputs),
242-
};
240+
const allFormInputs = excludeKeys(
241+
{
242+
...DEFAULT_FORM_INPUTS,
243+
...(mods && mods.customFormInputs),
244+
},
245+
mods && mods.deactivatedFormInputs,
246+
);
247+
243248
const unsupportedFeatures = checkForUnsupportedFeatures(
244249
schemaData,
245250
uiSchemaData,

src/formBuilder/PredefinedGallery.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
stringify,
88
propagateDefinitionChanges,
99
generateCategoryHash,
10+
excludeKeys,
1011
} from './utils';
1112
import { arrows as arrowsStyle } from './styles';
1213
import DEFAULT_FORM_INPUTS from './defaults/defaultFormInputs';
@@ -133,10 +134,13 @@ export default function PredefinedGallery({
133134
const classes = useStyles();
134135
const schemaData = (parse(schema): { [string]: any }) || {};
135136
const uiSchemaData = (parse(uischema): { [string]: any }) || {};
136-
const allFormInputs = {
137-
...DEFAULT_FORM_INPUTS,
138-
...(mods && mods.customFormInputs),
139-
};
137+
const allFormInputs = excludeKeys(
138+
{
139+
...DEFAULT_FORM_INPUTS,
140+
...(mods && mods.customFormInputs),
141+
},
142+
mods && mods.deactivatedFormInputs,
143+
);
140144
const categoryHash = generateCategoryHash(allFormInputs);
141145

142146
React.useEffect(() => {

src/formBuilder/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,23 @@ export function subtractArray(
16141614
return array1.filter((v) => !keys[v]);
16151615
}
16161616

1617+
export function excludeKeys(
1618+
obj: { [string]: any },
1619+
keys: ?(string[]),
1620+
): { [string]: any } {
1621+
if (!keys) return { ...obj };
1622+
1623+
const keysHash = keys.reduce((acc, curr) => {
1624+
acc[curr] = true;
1625+
return acc;
1626+
}, {});
1627+
1628+
return Object.keys(obj).reduce(
1629+
(acc, curr) => (keysHash[curr] ? acc : { ...acc, [curr]: obj[curr] }),
1630+
{},
1631+
);
1632+
}
1633+
16171634
export function getNewElementDefaultDataOptions(i: number, mods?: Mods) {
16181635
if (mods && mods.newElementDefaultDataOptions !== undefined) {
16191636
const title = `${mods.newElementDefaultDataOptions.title} ${i}`;

src/formBuilder/utils.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
generateCategoryHash,
1414
generateElementComponentsFromSchemas,
1515
subtractArray,
16+
excludeKeys,
1617
getNewElementDefaultDataOptions,
1718
} from './utils';
1819
import DEFAULT_FORM_INPUTS from './defaults/defaultFormInputs';
@@ -569,6 +570,58 @@ describe('subtractArray', () => {
569570
});
570571
});
571572

573+
describe('excludeKeys', () => {
574+
it('returns the given object excluding the given keys', () => {
575+
const obj = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
576+
const keys = ['biz', 'boo', 'extraKey'];
577+
578+
const expectedResult = { foo: 'bar', bla: 'bee' };
579+
const actualResult = excludeKeys(obj, keys);
580+
581+
expect(actualResult).toEqual(expectedResult);
582+
});
583+
584+
it('returns the given object if keys is empty', () => {
585+
const obj = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
586+
const keys = [];
587+
588+
const expectedResult = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
589+
const actualResult = excludeKeys(obj, keys);
590+
591+
expect(actualResult).toEqual(expectedResult);
592+
});
593+
594+
it('returns the given object if keys is undefined', () => {
595+
const obj = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
596+
const keys = undefined;
597+
598+
const expectedResult = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
599+
const actualResult = excludeKeys(obj, keys);
600+
601+
expect(actualResult).toEqual(expectedResult);
602+
});
603+
604+
it('returns the given object if keys is null', () => {
605+
const obj = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
606+
const keys = null;
607+
608+
const expectedResult = { foo: 'bar', biz: 'baz', boo: 'baa', bla: 'bee' };
609+
const actualResult = excludeKeys(obj, keys);
610+
611+
expect(actualResult).toEqual(expectedResult);
612+
});
613+
614+
it('returns an empty object if both obj and keys are empty', () => {
615+
const obj = {};
616+
const keys = [];
617+
618+
const expectedResult = {};
619+
const actualResult = excludeKeys(obj, keys);
620+
621+
expect(actualResult).toEqual(expectedResult);
622+
});
623+
});
624+
572625
describe('getNewElementDefaultDataOptions', () => {
573626
it('returns a default dataOptions when undefined mods are passed', () => {
574627
const i = 1;

0 commit comments

Comments
 (0)