Skip to content

Commit 9e930e8

Browse files
authored
Merge pull request #14 from fullstacklabs/GDCM-1053
Prevent SelectField to accept unknown options
2 parents 74af9b3 + a0fc8c4 commit 9e930e8

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/DForm.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,17 @@ const DForm = ({
6666
validationSchema,
6767
validateOnMount: true,
6868
validateOnChange: true,
69-
});
69+
})
7070
const { isValid, setFieldTouched, setFieldValue, values, errors } = formik
7171

7272
// Formik reinitialize is not working when the initialValues change. This is just happening once
7373
// the first time you move from a section to another.
74-
let sectionValues = values;
74+
let sectionValues = values
7575
if (values && initialValues) {
76-
sectionValues = difference(Object.keys(values), Object.keys(initialValues)).length ? initialValues : values
76+
sectionValues = difference(Object.keys(values), Object.keys(initialValues))
77+
.length
78+
? initialValues
79+
: values
7780
}
7881

7982
useEffect(() => {
@@ -96,7 +99,6 @@ const DForm = ({
9699
if (currentSectionIndex > 0) setCurrentSectionIndex(currentSectionIndex - 1)
97100
}
98101

99-
100102
const renderFields = () => (
101103
<FormikProvider value={formik}>
102104
<FieldsList

src/fields/SelectField.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@ const SelectField = ({
2929
removeItem,
3030
index,
3131
}) => {
32-
const onFieldChange = (value) => {
33-
let newValue = value // selected item(s)
32+
const isMultiple = MultipleTypeFormats.includes(field.schema.format)
3433

35-
if (
36-
MultipleTypeFormats.includes(field.schema.format) &&
37-
!Array.isArray(newValue)
38-
) {
39-
newValue = newValue ? [newValue] : []
34+
const onFieldChange = (newValue) => {
35+
let arrValue = castArray(newValue) // selected item(s)
36+
37+
if (!isMultiple && value && arrValue[0] === value[0]) {
38+
// to de-select already selected option
39+
arrValue = []
4040
}
4141

4242
setFieldTouched(field.id, true)
43-
setFieldValue(field.id, newValue)
43+
// we check that all new selected options are present in the field schema options definition
44+
// to prevent saving unkown options
45+
if (
46+
intersection(arrValue, field.schema.options).length === arrValue.length
47+
) {
48+
setFieldValue(field.id, arrValue)
49+
}
4450
}
4551

4652
const activeSubform = useMemo(() => {
47-
if (MultipleTypeFormats.includes(field.schema.format)) return null
53+
if (isMultiple) return null
4854
if (!field.subforms?.length || !value?.length) return null
4955

5056
return field.subforms.find(

src/helpers/formMapper.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ export const mapFieldAnswersToFormValues = (
4545

4646
const fieldAnswers = answersByFieldId[field.id] || []
4747

48-
if (field.fieldType === 'select' && field.schema.multiple) {
48+
if (field.fieldType === 'select' && field.schema.multiple)
4949
return fieldAnswers.map((answer) => answer.value)
50-
}
5150

5251
const fieldAnswer = fieldAnswers[0]
5352

0 commit comments

Comments
 (0)