forked from pkp/ui-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80b6f64
commit b05393b
Showing
4 changed files
with
63 additions
and
52 deletions.
There are no files selected for viewing
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,50 @@ | ||
/** | ||
* Should a field be shown? | ||
* | ||
* @param {Object} field One of this.fields | ||
* @return {Boolean} | ||
*/ | ||
export function shouldShowFieldWithinGroup(field, allFields) { | ||
if (typeof field.showWhen === 'undefined') { | ||
return true; | ||
} | ||
const whenFieldName = | ||
typeof field.showWhen === 'string' ? field.showWhen : field.showWhen[0]; | ||
const whenField = allFields.find((field) => field.name === whenFieldName); | ||
if (!whenField) { | ||
return false; | ||
} | ||
if (typeof field.showWhen === 'string') { | ||
return !!whenField.value; | ||
} | ||
return whenField.value === field.showWhen[1]; | ||
} | ||
/** | ||
* Should a group be shown? | ||
* | ||
* @param {Object} group One of this.groups | ||
* @return {Boolean} | ||
*/ | ||
export function shouldShowGroup(group, fields) { | ||
if (typeof group.showWhen === 'undefined') { | ||
return true; | ||
} | ||
const whenFieldName = | ||
typeof group.showWhen === 'string' ? group.showWhen : group.showWhen[0]; | ||
const whenField = fields.find((field) => field.name === whenFieldName); | ||
if (!whenField) { | ||
return false; | ||
} | ||
if (typeof group.showWhen === 'string') { | ||
return !!whenField.value; | ||
} | ||
return whenField.value === group.showWhen[1]; | ||
} | ||
|
||
export function shouldShowField(field, fields, groups) { | ||
const group = groups.find((group) => group.id === field.groupId); | ||
|
||
return ( | ||
shouldShowGroup(group, fields) && shouldShowFieldWithinGroup(field, fields) | ||
); | ||
} |