Skip to content

Commit d2487c6

Browse files
bruiztorresBorja Ruiz Torres
andauthored
Handle unnamed properties with more than 1 digit (#118)
Fix logic to handle unname fields with more than 1 digit Co-authored-by: Borja Ruiz Torres <[email protected]>
1 parent ecbfe5e commit d2487c6

File tree

2 files changed

+90
-28
lines changed

2 files changed

+90
-28
lines changed

src/formBuilder/utils.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,31 @@ export function updateSchemas(
867867
onChange(newSchema, newUiSchema);
868868
}
869869

870+
export const DEFAULT_INPUT_NAME = 'newInput';
871+
872+
// ensure that each added block has a unique name
873+
function getIdFromElementsBlock(elements: Array<ElementProps>) {
874+
const names = elements.map((element) => element.name);
875+
const defaultNameLength = DEFAULT_INPUT_NAME.length;
876+
877+
return names.length > 0
878+
? Math.max(
879+
...names.map((name) => {
880+
if (name.startsWith(DEFAULT_INPUT_NAME)) {
881+
const index = name.substring(defaultNameLength, name.length);
882+
const value = Number.parseInt(index);
883+
884+
if (!isNaN(value)) {
885+
return value;
886+
}
887+
}
888+
889+
return 0;
890+
}),
891+
) + 1
892+
: 1;
893+
}
894+
870895
// given an initial schema, update with a new card appended
871896
export function addCardObj(parameters: {
872897
schema: { [string]: any },
@@ -896,24 +921,11 @@ export function addCardObj(parameters: {
896921
categoryHash,
897922
});
898923

899-
// ensure that each added block has a unique name
900-
901-
const names = newElementObjArr.map((element) => element.name);
902-
const i =
903-
names.length > 0
904-
? Math.max(
905-
...names.map((name) =>
906-
name.startsWith('newInput')
907-
? Number.parseInt(name.charAt(8), 10)
908-
: 0,
909-
),
910-
) + 1
911-
: 1;
912-
924+
const i = getIdFromElementsBlock(newElementObjArr);
913925
const dataOptions = getNewElementDefaultDataOptions(i, mods);
914926

915927
const newElement = ({
916-
name: `newInput${i}`,
928+
name: `${DEFAULT_INPUT_NAME}${i}`,
917929
required: false,
918930
dataOptions: dataOptions,
919931
uiOptions: (mods && mods.newElementDefaultUiSchema) || {},
@@ -964,21 +976,10 @@ export function addSectionObj(parameters: {
964976
categoryHash,
965977
});
966978

967-
// ensure that each added block has a unique name
968-
const names = newElementObjArr.map((element) => element.name);
969-
const i =
970-
names.length > 0
971-
? Math.max(
972-
...names.map((name) =>
973-
name.startsWith('newInput')
974-
? Number.parseInt(name.charAt(8), 10)
975-
: 0,
976-
),
977-
) + 1
978-
: 1;
979+
const i = getIdFromElementsBlock(newElementObjArr);
979980

980981
const newElement = ({
981-
name: `newInput${i}`,
982+
name: `${DEFAULT_INPUT_NAME}${i}`,
982983
required: false,
983984
dataOptions: {
984985
title: `New Input ${i}`,

src/formBuilder/utils.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import {
1515
subtractArray,
1616
excludeKeys,
1717
getNewElementDefaultDataOptions,
18+
addCardObj,
19+
addSectionObj,
20+
DEFAULT_INPUT_NAME,
1821
} from './utils';
1922
import DEFAULT_FORM_INPUTS from './defaults/defaultFormInputs';
2023

@@ -72,6 +75,20 @@ const elementPropArr = [
7275
},
7376
];
7477

78+
function generateSchemaWithUnnamedProperties(amount) {
79+
const properties = [...Array(10).keys()].reduce((acc, id) => {
80+
return { ...acc, [`${DEFAULT_INPUT_NAME}${id + 1}`]: { type: 'string' } };
81+
}, {});
82+
83+
return {
84+
$id: 'https://example.com/person.schema.json',
85+
$schema: 'https://json-schema.org/draft/2020-12/schema',
86+
title: 'Test',
87+
type: 'object',
88+
properties: properties,
89+
};
90+
}
91+
7592
describe('parse', () => {
7693
it('parses valid JSON into a JS object', () => {
7794
expect(
@@ -711,3 +728,47 @@ describe('getNewElementDefaultDataOptions', () => {
711728
expect(actualDataOptions).toEqual(expectedDataOptions);
712729
});
713730
});
731+
732+
describe('addCardObj', () => {
733+
it('should be able to add more than 10 unnamed CardObj', () => {
734+
const mockEvent = jest.fn(() => {});
735+
const defaultUiSchema = {};
736+
const props = {
737+
schema: generateSchemaWithUnnamedProperties(10),
738+
uischema: defaultUiSchema,
739+
onChange: (schema, uischema) => mockEvent(schema, uischema),
740+
definitionData: {},
741+
definitionUi: {},
742+
categoryHash: {},
743+
};
744+
745+
addCardObj(props);
746+
747+
const currentSchema = mockEvent.mock.calls[0][0];
748+
const inputElementsCount = Object.keys(currentSchema.properties).length;
749+
750+
expect(inputElementsCount).toEqual(11);
751+
});
752+
});
753+
754+
describe('addSectionObj', () => {
755+
it('should be able to add more than 10 unnamed SectionObj', () => {
756+
const mockEvent = jest.fn(() => {});
757+
const defaultUiSchema = {};
758+
const props = {
759+
schema: generateSchemaWithUnnamedProperties(10),
760+
uischema: defaultUiSchema,
761+
onChange: (schema, uischema) => mockEvent(schema, uischema),
762+
definitionData: {},
763+
definitionUi: {},
764+
categoryHash: {},
765+
};
766+
767+
addSectionObj(props);
768+
769+
const currentSchema = mockEvent.mock.calls[0][0];
770+
const inputElementsCount = Object.keys(currentSchema.properties).length;
771+
772+
expect(inputElementsCount).toEqual(11);
773+
});
774+
});

0 commit comments

Comments
 (0)