Skip to content

Commit

Permalink
pkp/pkp-lib#9996 prev attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
jardakotesovec committed Jun 6, 2024
1 parent 80b6f64 commit b05393b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 52 deletions.
6 changes: 5 additions & 1 deletion src/components/Form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<script>
import FormLocales from './FormLocales.vue';
import FormPage from './FormPage.vue';
import {shouldShowField} from './formHelpers';
export default {
name: 'PkpForm',
Expand Down Expand Up @@ -332,7 +333,10 @@ export default {
validateRequired() {
let errors = {};
this.fields.forEach((field) => {
if (!field.isRequired) {
if (
!field.isRequired ||
!shouldShowField(field, this.fields, this.groups)
) {
return;
}
let missingValue = false;
Expand Down
30 changes: 5 additions & 25 deletions src/components/Form/FormGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import FieldUpload from './fields/FieldUpload.vue';
import FieldSlider from './fields/FieldSlider.vue';
import FieldUploadImage from './fields/FieldUploadImage.vue';
import {shouldShowFieldWithinGroup} from './formHelpers';
export default {
name: 'FormGroup',
components: {
Expand Down Expand Up @@ -127,7 +129,9 @@ export default {
*/
fieldsInGroup() {
return this.fields.filter(
(field) => field.groupId === this.id && this.shouldShowField(field),
(field) =>
field.groupId === this.id &&
shouldShowFieldWithinGroup(field, this.fields),
);
},
Expand All @@ -153,30 +157,6 @@ export default {
this.$emit('change', name, prop, value, localeKey);
},
/**
* Should a field be shown?
*
* @param {Object} field One of this.fields
* @return {Boolean}
*/
shouldShowField: function (field) {
if (typeof field.showWhen === 'undefined') {
return true;
}
const whenFieldName =
typeof field.showWhen === 'string' ? field.showWhen : field.showWhen[0];
const whenField = this.fields.find(
(field) => field.name === whenFieldName,
);
if (!whenField) {
return false;
}
if (typeof field.showWhen === 'string') {
return !!whenField.value;
}
return whenField.value === field.showWhen[1];
},
/**
* Respond to a field changing its errors
*
Expand Down
29 changes: 3 additions & 26 deletions src/components/Form/FormPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
import ButtonRow from '@/components/ButtonRow/ButtonRow.vue';
import FormErrors from '@/components/Form/FormErrors.vue';
import FormGroup from '@/components/Form/FormGroup.vue';
import {shouldShowGroup} from './formHelpers';
export default {
name: 'FormPage',
components: {
Expand Down Expand Up @@ -107,7 +107,8 @@ export default {
*/
groupsInPage() {
return this.groups.filter(
(group) => group.pageId === this.id && this.shouldShowGroup(group),
(group) =>
group.pageId === this.id && shouldShowGroup(group, this.fields),
);
},
Expand Down Expand Up @@ -186,30 +187,6 @@ export default {
this.$emit('showLocale', localeKey);
},
/**
* Should a group be shown?
*
* @param {Object} group One of this.groups
* @return {Boolean}
*/
shouldShowGroup: function (group) {
if (typeof group.showWhen === 'undefined') {
return true;
}
const whenFieldName =
typeof group.showWhen === 'string' ? group.showWhen : group.showWhen[0];
const whenField = this.fields.find(
(field) => field.name === whenFieldName,
);
if (!whenField) {
return false;
}
if (typeof group.showWhen === 'string') {
return !!whenField.value;
}
return whenField.value === group.showWhen[1];
},
/**
* Pass an event up to the form to set the errors object
*
Expand Down
50 changes: 50 additions & 0 deletions src/components/Form/formHelpers.js
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)
);
}

0 comments on commit b05393b

Please sign in to comment.