-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #515 from zendesk/gianluca/GG-3785-conditional-fie…
…lds-multi-level Fixed conditional fields
- Loading branch information
Showing
9 changed files
with
423 additions
and
61 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
import type { EndUserCondition, Field } from "./data-types"; | ||
import { getVisibleFields } from "./getVisibleFields"; | ||
|
||
const dropdownField: Field = { | ||
id: 123, | ||
name: "request[custom_fields][123]", | ||
required: false, | ||
error: null, | ||
label: "Dropdown Field", | ||
description: "", | ||
type: "tagger", | ||
options: [ | ||
{ name: "One", value: "one" }, | ||
{ name: "Two", value: "two" }, | ||
], | ||
}; | ||
|
||
const secondDropdownField: Field = { | ||
id: 456, | ||
name: "request[custom_fields][456]", | ||
required: false, | ||
error: null, | ||
label: "Second Dropdown Field", | ||
description: "", | ||
type: "tagger", | ||
options: [ | ||
{ name: "Three", value: "three" }, | ||
{ name: "Four", value: "four" }, | ||
], | ||
}; | ||
|
||
const textField: Field = { | ||
id: 789, | ||
name: "request[custom_fields][789]", | ||
required: false, | ||
error: null, | ||
label: "Text Field", | ||
description: "", | ||
type: "text", | ||
options: [], | ||
}; | ||
|
||
const integerField: Field = { | ||
id: 101, | ||
name: "request[custom_fields][101]", | ||
required: false, | ||
error: null, | ||
label: "Number Field", | ||
description: "", | ||
type: "number", | ||
options: [], | ||
}; | ||
|
||
describe("getVisibleFields", () => { | ||
it("should return the same fields if no end user conditions are provided", () => { | ||
const fields = [ | ||
dropdownField, | ||
secondDropdownField, | ||
textField, | ||
integerField, | ||
]; | ||
const endUserConditions: EndUserCondition[] = []; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual(fields); | ||
}); | ||
|
||
it("should show fields and set required if the value matches", () => { | ||
const fields = [ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...secondDropdownField, value: "three" }, | ||
{ ...textField, required: true }, | ||
integerField, | ||
]; | ||
const endUserConditions: EndUserCondition[] = [ | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "one", | ||
child_fields: [{ id: 456, is_required: true }], | ||
}, | ||
{ | ||
parent_field_id: 456, | ||
parent_field_type: "tagger", | ||
value: "three", | ||
child_fields: [{ id: 789, is_required: false }], | ||
}, | ||
]; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual([ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...secondDropdownField, value: "three", required: true }, | ||
textField, | ||
integerField, | ||
]); | ||
}); | ||
|
||
it("should hide fields if the value does not match", () => { | ||
const fields = [ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...secondDropdownField, value: "three" }, | ||
textField, | ||
integerField, | ||
]; | ||
const endUserConditions: EndUserCondition[] = [ | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "two", | ||
child_fields: [{ id: 456, is_required: true }], | ||
}, | ||
{ | ||
parent_field_id: 456, | ||
parent_field_type: "tagger", | ||
value: "four", | ||
child_fields: [{ id: 789, is_required: false }], | ||
}, | ||
]; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual([{ ...dropdownField, value: "one" }, integerField]); | ||
}); | ||
|
||
it("should not change required if the field is not part of some condition", () => { | ||
const fields = [ | ||
{ ...dropdownField, value: "one", required: true }, | ||
{ ...secondDropdownField, value: "three" }, | ||
{ ...textField, required: true }, | ||
{ ...integerField, required: false }, | ||
]; | ||
const endUserConditions: EndUserCondition[] = [ | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "one", | ||
child_fields: [{ id: 456, is_required: true }], | ||
}, | ||
]; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual([ | ||
{ ...dropdownField, value: "one", required: true }, | ||
{ ...secondDropdownField, value: "three", required: true }, | ||
{ ...textField, required: true }, | ||
{ ...integerField, required: false }, | ||
]); | ||
}); | ||
|
||
it("should hide fields with multi level conditions when the value on parent doesn't match", () => { | ||
const fields = [ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...secondDropdownField, value: "three" }, | ||
{ ...textField, value: "text" }, | ||
integerField, | ||
]; | ||
const endUserConditions: EndUserCondition[] = [ | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "two", | ||
child_fields: [ | ||
{ | ||
id: 456, | ||
is_required: true, | ||
}, | ||
], | ||
}, | ||
{ | ||
parent_field_id: 456, | ||
parent_field_type: "tagger", | ||
value: "three", | ||
child_fields: [ | ||
{ | ||
id: 789, | ||
is_required: false, | ||
}, | ||
], | ||
}, | ||
{ | ||
parent_field_id: 789, | ||
parent_field_type: "text", | ||
value: "text", | ||
child_fields: [ | ||
{ | ||
id: 101, | ||
is_required: true, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual([{ ...dropdownField, value: "one" }]); | ||
}); | ||
|
||
it("should not hide fields with multi level conditions when a second condition is met", () => { | ||
const fields = [ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...secondDropdownField, value: "three" }, | ||
{ ...textField, value: "text" }, | ||
integerField, | ||
]; | ||
|
||
const endUserConditions: EndUserCondition[] = [ | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "two", | ||
child_fields: [ | ||
{ | ||
id: 456, | ||
is_required: true, | ||
}, | ||
], | ||
}, | ||
{ | ||
parent_field_id: 456, | ||
parent_field_type: "tagger", | ||
value: "three", | ||
child_fields: [ | ||
{ | ||
id: 789, | ||
is_required: false, | ||
}, | ||
], | ||
}, | ||
{ | ||
parent_field_id: 789, | ||
parent_field_type: "text", | ||
value: "text", | ||
child_fields: [ | ||
{ | ||
id: 101, | ||
is_required: true, | ||
}, | ||
], | ||
}, | ||
{ | ||
parent_field_id: 123, | ||
parent_field_type: "tagger", | ||
value: "one", | ||
child_fields: [ | ||
{ | ||
id: 789, | ||
is_required: false, | ||
}, | ||
{ | ||
id: 101, | ||
is_required: true, | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const result = getVisibleFields(fields, endUserConditions); | ||
|
||
expect(result).toEqual([ | ||
{ ...dropdownField, value: "one" }, | ||
{ ...textField, value: "text" }, | ||
{ ...integerField, required: true }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import type { EndUserCondition, Field } from "./data-types"; | ||
|
||
function getFieldConditions( | ||
fieldId: number, | ||
endUserConditions: EndUserCondition[] | ||
): EndUserCondition[] { | ||
return endUserConditions.filter((condition) => { | ||
return condition.child_fields.some((child) => child.id === fieldId); | ||
}); | ||
} | ||
|
||
function getAppliedConditions( | ||
fieldConditions: EndUserCondition[], | ||
allConditions: EndUserCondition[], | ||
fields: Field[] | ||
): EndUserCondition[] { | ||
return fieldConditions.filter((condition) => { | ||
const parentField = fields.find( | ||
(field) => field.id === condition.parent_field_id | ||
); | ||
|
||
if (!parentField) { | ||
return false; | ||
} | ||
|
||
const parentFieldConditions = getFieldConditions( | ||
parentField.id, | ||
allConditions | ||
); | ||
|
||
// the condition is applied if the parent field value matches the condition value | ||
// and if the parent field has no conditions or if the parent field conditions are met | ||
return ( | ||
parentField.value === condition.value && | ||
(parentFieldConditions.length === 0 || | ||
getAppliedConditions(parentFieldConditions, allConditions, fields) | ||
.length > 0) | ||
); | ||
}); | ||
} | ||
|
||
export function getVisibleFields( | ||
fields: Field[], | ||
endUserConditions: EndUserCondition[] | ||
): Field[] { | ||
if (endUserConditions.length === 0) { | ||
return fields; | ||
} | ||
|
||
return fields.reduce((acc: Field[], field) => { | ||
const fieldConditions = getFieldConditions(field.id, endUserConditions); | ||
|
||
if (fieldConditions.length === 0) { | ||
return [...acc, field]; | ||
} | ||
|
||
const appliedConditions = getAppliedConditions( | ||
fieldConditions, | ||
endUserConditions, | ||
fields | ||
); | ||
|
||
if (appliedConditions.length > 0) { | ||
return [ | ||
...acc, | ||
{ | ||
...field, | ||
required: appliedConditions.some((condition) => | ||
condition.child_fields.some( | ||
(child) => child.id == field.id && child.is_required | ||
) | ||
), | ||
}, | ||
]; | ||
} | ||
|
||
return acc; | ||
}, []); | ||
} |
Oops, something went wrong.