Skip to content
Open
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
3 changes: 2 additions & 1 deletion designer/server/src/common/constants/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export const QuestionAdvancedSettings =
InstructionText: 'instructionText',
MinChecks: 'minChecks',
MaxChecks: 'maxChecks',
ExactChecks: 'exactChecks'
ExactChecks: 'exactChecks',
Countries: 'countries'
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export const advancedSettingsPerComponentType =
QuestionAdvancedSettings.InstructionText,
QuestionAdvancedSettings.Classes
],
HiddenField: []
HiddenField: [],
GeospatialField: [QuestionAdvancedSettings.Countries]
})

/**
Expand Down Expand Up @@ -327,6 +328,44 @@ export const allAdvancedSettingsFields =
text: 'The maximum number of checkboxes a user can select'
},
classes: GOVUK_INPUT_WIDTH_3
},
[QuestionAdvancedSettings.Countries]: {
name: 'countries',
id: 'countries',
classes: 'govuk-radios--small',
fieldset: {
legend: {
text: 'Which country must the features be in?',
isPageHeading: false,
classes: 'govuk-fieldset__legend--m'
}
},
formGroup: { classes: 'app-settings-radios' },
items: [
{
value: 'england',
text: 'England'
},
{
value: 'wales',
text: 'Wales'
},
{
value: 'northern-ireland',
text: 'Northern Ireland'
},
{
value: 'scotland',
text: 'Scotland'
},
{
divider: 'or'
},
{
value: 'any',
text: 'Any'
}
]
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe('editor-v2 - advanced settings fields model', () => {
ComponentType.TextField
)
})

test('should return RadiosField for countries', () => {
expect(getFieldComponentType({ name: 'countries' })).toBe(
ComponentType.RadiosField
)
})
})

describe('mapQuestionDetails', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ export function getAdditionalOptions(payload) {
key: 'description',
getValue: () => payload.paymentDescription,
shouldInclude: () => payload.paymentDescription !== undefined
},
{
key: 'countries',
getValue: () => payload.countries,
shouldInclude: () =>
Array.isArray(payload.countries) &&
payload.countries.length === 1 &&
payload.countries[0] !== 'any'
}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('advanced-settings-helpers', () => {
expect(result).toEqual({ suffix: ' per item' })
})

it('should include countries when provided', () => {
const result = getAdditionalOptions({ countries: ['scotland'] })
expect(result).toEqual({ countries: ['scotland'] })
})

it('should map maxFuture to maxDaysInFuture', () => {
const result = getAdditionalOptions({ maxFuture: '30' })
expect(result).toEqual({ maxDaysInFuture: '30' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,6 @@ export const allSpecificSchemas = Joi.object().keys({
'*': 'Enter instructions to help users answer this question'
}),
otherwise: Joi.string().optional().allow('')
})
}),
countries: questionDetailsFullSchema.countriesSchema
})
6 changes: 6 additions & 0 deletions designer/server/src/models/forms/editor-v2/page-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const checkBoxFieldQuestions = [

const fileUploadFields = [QuestionBaseSettings.FileTypes]

const radiosFieldQuestions = [QuestionAdvancedSettings.Countries]

/**
* @param {GovukField} field
*/
Expand All @@ -76,6 +78,10 @@ export function getFieldComponentType(field) {
return ComponentType.FileUploadField
}

if (radiosFieldQuestions.includes(fieldName)) {
return ComponentType.RadiosField
}

throw new Error(
`Invalid or not implemented field name setting (${field.name})`
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ export function mapToQuestionOptions(question) {
.options.instructionText
}
: {}
const geospatialExtras =
question.type === ComponentType.GeospatialField
? {
countries: /** @type {GeospatialFieldComponent} */ (question).options
.countries ?? ['any']
}
: {}

return {
classes: /** @type {FormComponentsDef} */ (question).options.classes,
Expand All @@ -152,7 +159,8 @@ export function mapToQuestionOptions(question) {
...minMaxExtras,
...multilineExtras,
...regexExtras,
...locationExtras
...locationExtras,
...geospatialExtras
}
}

Expand Down Expand Up @@ -244,6 +252,6 @@ export function enhancedFields(options, question, validation) {
*/

/**
* @import { ComponentDef, DatePartsFieldComponent, EastingNorthingFieldComponent, FileUploadFieldComponent, CheckboxesFieldComponent, FormComponentsDef, FormEditor, GovukField, LatLongFieldComponent, MonthYearFieldComponent, MultilineTextFieldComponent, NationalGridFieldNumberFieldComponent, NumberFieldComponent, OsGridRefFieldComponent, TextFieldComponent } from '@defra/forms-model'
* @import { ComponentDef, DatePartsFieldComponent, EastingNorthingFieldComponent, FileUploadFieldComponent, CheckboxesFieldComponent, FormComponentsDef, FormEditor, GovukField, LatLongFieldComponent, MonthYearFieldComponent, MultilineTextFieldComponent, NationalGridFieldNumberFieldComponent, NumberFieldComponent, OsGridRefFieldComponent, TextFieldComponent, GeospatialFieldComponent } from '@defra/forms-model'
* @import { ValidationFailure } from '~/src/common/helpers/types.js'
*/
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ describe('editor-v2 - question details advanced settings model', () => {
classes: 'month-class'
})
})

test('should map a geospatial field with country', () => {
const res = mapToQuestionOptions({
type: ComponentType.GeospatialField,
name: 'features',
title: 'features title',
options: {
countries: ['wales']
}
})
expect(res).toEqual({
countries: ['wales']
})
})
})

describe('advancedSettingsFields', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% from "govuk/components/input/macro.njk" import govukInput %}
{% from "govuk/components/textarea/macro.njk" import govukTextarea %}
{% from "govuk/components/checkboxes/macro.njk" import govukCheckboxes %}
{% from "govuk/components/radios/macro.njk" import govukRadios %}

{% macro renderFieldByType(field) %}
{% set fieldType = getFieldType(field) %}
{% if fieldType == 'TextField' %}
Expand All @@ -9,6 +11,8 @@
{{ govukTextarea(field) }}
{% elif fieldType == 'CheckboxesField' %}
{{ govukCheckboxes(field) }}
{% elif fieldType == 'RadiosField' %}
{{ govukRadios(field) }}
{% endif %}
{% endmacro %}

Expand Down
2 changes: 1 addition & 1 deletion model/src/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export interface GeospatialFieldComponent extends FormFieldBase {
type: ComponentType.GeospatialField
options: FormFieldBase['options'] & {
condition?: string
country?: GeospatialFieldOptionsCountry
countries?: GeospatialFieldOptionsCountry[]
}
}

Expand Down
18 changes: 16 additions & 2 deletions model/src/form/form-editor/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Joi, { type ArraySchema, type GetRuleOptions } from 'joi'

import { rtrimOnly } from '~/src/common/rtrim-only.js'
import { ComponentType } from '~/src/components/enums.js'
import {
ComponentType,
GeospatialFieldOptionsCountryEnum
} from '~/src/components/enums.js'
import {
MAX_NUMBER_OF_REPEAT_ITEMS,
MIN_NUMBER_OF_REPEAT_ITEMS
Expand Down Expand Up @@ -447,6 +450,16 @@ export const maxChecksSchema = Joi.number()
.min(2)
.description('Maximum number of items allowed to be selected.')

export const countriesSchema = Joi.array()
.items(
Joi.string().valid(
...Object.values(GeospatialFieldOptionsCountryEnum),
'any'
)
)
.single()
.description('The country to be included in a geospatial field')

type GenericRuleOptions<K extends string, T> = Omit<GetRuleOptions, 'args'> & {
args: Record<K, T>
}
Expand Down Expand Up @@ -638,7 +651,8 @@ export const questionDetailsFullSchema = {
usePostcodeLookupSchema,
minChecksSchema,
maxChecksSchema,
exactChecksSchema
exactChecksSchema,
countriesSchema
}

export const formEditorInputPageKeys = {
Expand Down
11 changes: 10 additions & 1 deletion model/src/form/form-editor/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type ComponentType } from '~/src/components/enums.js'
import { type ComponentDef } from '~/src/components/types.js'
import {
type ComponentDef,
type GeospatialFieldOptionsCountry
} from '~/src/components/types.js'
import { type DateDirections, type DateUnits } from '~/src/conditions/enums.js'
import {
type ConditionWrapperV2,
Expand Down Expand Up @@ -349,6 +352,11 @@ export interface FormEditor {
* Title that user supplies a section
*/
sectionTitle: string

/**
* The country restriction for geospatial questions
*/
countries?: (GeospatialFieldOptionsCountry | 'any')[]
}

export type FormEditorInputPage = Pick<
Expand Down Expand Up @@ -428,6 +436,7 @@ export type FormEditorInputQuestion = Pick<
| 'paymentDescription'
| 'paymentTestApiKey'
| 'paymentLiveApiKey'
| 'countries'
>

export type FormEditorInputPageSettings = Pick<
Expand Down
Loading