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

Revert conditional question #197

Merged
merged 3 commits into from
Jun 25, 2024
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
8 changes: 0 additions & 8 deletions runner/src/client/sass/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,3 @@
// .govuk-phase-banner__content__tag {
// text-transform: capitalize;
// }

.govuk-radios__conditional--hidden {
display: none;
}

.govuk-checkboxes__conditional--hidden {
display: none;
}
62 changes: 17 additions & 45 deletions runner/src/server/forms/ReportAnOutbreak.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@
"nameHasError": false,
"hint": "Please provide the postcode of the specific setting affected (i.e. not the overarching management company if applicable). If there is no one specific setting affected e.g. Domiciliary or Home Care, then please provide the postcode of the care provider company"
},
{
"name": "S0Q2",
"options": {
"required": false
},
"type": "TextField",
"title": "Please enter your CQC Location ID",
"nameHasError": false,
"schema": {}
},
{
"name": "VMOkqi",
"options": {},
"type": "Para",
"content": "If you don’t know your CQC Location ID then please visit <a id=\"cqc number\" target=\"_blank\" href=\"https://www.cqc.org.uk/about-us/transparency/using-cqc-data\">Using CQC data - Care Quality Commission</a> and scroll about half way down the page to <b style=\"color:#1d70b8\">CQC care directory - csv</b>. You will find your CQC Location ID in this look up table.\n</br></br>",
"schema": {}
},
{
"name": "S0Q3",
"options": {
Expand All @@ -64,32 +81,6 @@
"type": "Para",
"content": "Click on the link to <a target=\"_blank\" id=\"local-health-protection-team\" href=\"https://www.gov.uk/health-protection-team\">Find your local health protection team in England - GOV.UK</a> (www.gov.uk) [Enter the postcode of your care setting]",
"schema": {}
},
{
"name": "S0Q2a",
"type": "RadiosField",
"title": "Is your setting CQC registered?",
"list": "YesNoDk",
"nameHasError": false,
"schema": {},
"options": {
"conditionallyRevealedComponents": {
"yes": {
"type": "TextField",
"name": "yes",
"title": "Please enter your CQC Location ID (Mandatory)",
"options": {},
"schema": {}
}
}
}
},
{
"name": "VMOkqi",
"options": {},
"type": "Para",
"content": "If you don’t know your CQC Location ID then please visit <a id=\"cqc number\" target=\"_blank\" href=\"https://www.cqc.org.uk/about-us/transparency/using-cqc-data\">Using CQC data - Care Quality Commission</a> and scroll about half way down the page to <b style=\"color:#1d70b8\">CQC care directory - csv</b>. You will find your CQC Location ID in this look up table.\n</br></br>",
"schema": {}
}
],
"next": [
Expand Down Expand Up @@ -2179,25 +2170,6 @@
"value": "5 or more"
}
]
},
{
"title": "s0q2 - cqc registered",
"name": "YesNoDk",
"type": "string",
"items": [
{
"text": "Yes",
"value": "yes"
},
{
"text": "No",
"value": "no"
},
{
"text": "Don't know",
"value": "Don't know"
}
]
}
],
"sections": [],
Expand Down
104 changes: 5 additions & 99 deletions runner/src/server/plugins/engine/components/SelectionControlField.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import joi from "joi";
import nunjucks from "nunjucks";
import { ListFormComponent } from "server/plugins/engine/components/ListFormComponent";
import { FormData, FormSubmissionErrors } from "server/plugins/engine/types";
import { ListItem, ViewModel } from "server/plugins/engine/components/types";
import { ComponentCollection } from "./ComponentCollection";
import { ListItem } from "server/plugins/engine/components/types";

/**
* "Selection controls" are checkboxes and radios (and switches), as per Material UI nomenclature.
*/

const getSchemaKeys = Symbol("getSchemaKeys");
export class SelectionControlField extends ListFormComponent {
conditionallyRevealedComponents?: any;
hasConditionallyRevealedComponents: boolean = false;

constructor(def, model) {
super(def, model);
const { options } = def;

const { items } = this;

if (options.conditionallyRevealedComponents) {
this.conditionallyRevealedComponents =
options.conditionallyRevealedComponents;

items.map((item: any) => {
const conditionallyRevealedComponent = this
.conditionallyRevealedComponents![item.value];
if (conditionallyRevealedComponent) {
item.hasConditionallyRevealedComponents = true;
item.conditionallyRevealedComponents = new ComponentCollection(
[conditionallyRevealedComponent],
item.model
);
}
});
}
}

getViewModel(formData: FormData, errors: FormSubmissionErrors) {
const { name, items } = this;
const options: any = this.options;
Expand All @@ -48,7 +16,7 @@ export class SelectionControlField extends ListFormComponent {
legend: viewModel.label,
};

viewModel.items = items.map((item: any) => {
viewModel.items = items.map((item) => {
const itemModel: ListItem = {
text: item.text,
value: item.value,
Expand All @@ -67,73 +35,11 @@ export class SelectionControlField extends ListFormComponent {
};
}

if (options.conditionallyRevealedComponents) {
if (options.conditionallyRevealedComponents[item.value]) {
// The gov.uk design system Nunjucks examples for conditional reveal reference variables from macros. There does not appear to
// to be a way to do this in JavaScript. As such, render the conditional components with Nunjucks before the main view is rendered.
// The conditional html tag used by the gov.uk design system macro will reference HTML rarther than one or more additional
// gov.uk design system macros.

itemModel.conditional = {
html: nunjucks.render(
"../views/partials/conditional-components.html",
{
components: item.conditionallyRevealedComponents.getViewModel(
formData,
errors
),
}
),
};
}
}

return itemModel;
});

return viewModel;
}

getStateSchemaKeys() {
return this[getSchemaKeys]("state");
}

getFormSchemaKeys() {
return this[getSchemaKeys]("form");
}

[getSchemaKeys](schemaType) {
const schemaName = `${schemaType}Schema`;
const schemaKeysFunctionName = `get${schemaType
.substring(0, 1)
.toUpperCase()}${schemaType.substring(1)}SchemaKeys`;
const filteredItems = this.items.filter(
(item: any) => item.hasConditionallyRevealedComponents
);
const conditionalName = this.name;
const schemaKeys = { [conditionalName]: this[schemaName] };
// const schema = this[schemaName];
// All conditional component values are submitted regardless of their visibilty.
// As such create Joi validation rules such that:
// a) When a conditional component is visible it is required.
// b) When a conditional component is not visible it is optional.
filteredItems?.forEach((item: any) => {
const conditionalSchemaKeys = item.conditionallyRevealedComponents[
schemaKeysFunctionName
]();
// Iterate through the set of components handled by conditional reveal adding Joi validation rules
// based on whether or not the component controlling the conditional reveal is selected.
Object.keys(conditionalSchemaKeys).forEach((key) => {
Object.assign(schemaKeys, {
[key]: joi.alternatives().conditional(joi.ref(conditionalName), {
is: key,
then: conditionalSchemaKeys[key].required(),
otherwise: joi.optional(),
//TODO: Checkbox joi validation
}),
});
});
// FIXME:- add this back when GDS fix accessibility issues involving conditional reveal fields
//return super.addConditionalComponents(item, itemModel, formData, errors);
});
return schemaKeys;
return viewModel;
}
}
3 changes: 0 additions & 3 deletions runner/src/server/plugins/engine/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export type ListItem = {
selected?: boolean;
label?: ListItemLabel;
condition?: string;
conditional?: {
html: string;
};
};

// TODO: Break this down for each component (Same as model/Component).
Expand Down
117 changes: 0 additions & 117 deletions runner/src/server/test-forms/conditionally-revealed-components.json

This file was deleted.

Loading
Loading