Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formbuilder): fix when new section is added and a new id is assigned #45

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@indec/form-builder",
"version": "1.5.1",
"version": "1.5.2",
"description": "Form builder",
"main": "index.js",
"private": false,
Expand Down
5 changes: 3 additions & 2 deletions src/components/FormBuilder/FormBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useSectionInitialValues from '@/hooks/useSectionInitialValues';
import getWarnings from '@/utils/getWarnings';
import sectionPropTypes from '@/utils/propTypes/section';
import getSchemas from '@/utils/getSchemas';
import getLastId from '@/utils/getLastId';

import Modals from './Modals';

Expand Down Expand Up @@ -52,8 +53,8 @@ function FormBuilder({

const addNewSection = (setValues, values) => {
const newValues = values;
const lastSection = formInitialValues[section.name].sort((firstItem, secondItem) => secondItem.id - firstItem.id)[0];
newValues[section.name].push({...formInitialValues[section.name][0], id: lastSection.id + 1});
const lastSection = getLastId(values[section.name]);
newValues[section.name].push({...formInitialValues[section.name][0], id: lastSection + 1});
return setValues(newValues);
};

Expand Down
9 changes: 5 additions & 4 deletions src/components/FormBuilder/FormBuilder.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const sections = [
userVarName: 'S1P3'
}
],
multiple: false,
multiple: true,
favorite: false,
interruption: {
name: 'S1I1',
Expand Down Expand Up @@ -217,21 +217,22 @@ const sections = [

function Template(args) {
const [page, setPage] = React.useState(0);

React.useEffect(() => {
if (!sections[page]) {
setPage(0);
}
}, [page]);

return (
<FormBuilder
{...args}
section={sections[page]}
page={page}
onSubmit={() => setPage(page + 1)}
onPrevious={() => setPage(page - 1)}
nextSection={sections[page + 1]?.name}
previousSection={sections[page - 1]?.name}
isLastSection={sections.length - 1 === page}
isSurvey
{...args}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/components/QuestionBuilder/Wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Wrapper.propTypes = {
})
),
PropTypes.string,
PropTypes.number,
PropTypes.arrayOf(PropTypes.string)
])
}).isRequired
Expand Down