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

fix: align with guidance on input type for numbers (NumberField) #1298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion runner/src/server/plugins/engine/components/NumberField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class NumberField extends FormComponent {
const viewModelSuffix = { suffix: { text: suffix } };
const viewModel = {
...super.getViewModel(formData, errors),
type: "number",
type: "text",
inputmode: "numeric",
// ...False returns nothing, so only adds content when
// the given options are present.
...(options.prefix && viewModelPrefix),
Expand All @@ -80,6 +81,11 @@ export class NumberField extends FormComponent {

if (this.schemaOptions.precision) {
viewModel.attributes.step = "0." + "1".padStart(schema.precision, "0");

// the `step` attribute will not work with input type "text"
// only set to "number" if we need to support existing `precision` config
// https://design-system.service.gov.uk/components/text-input/#avoid-using-inputs-with-a-type-of-number
viewModel.type = "number";
}

return viewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ function dateComponent(name, width) {
name: `myComponent__${name.toLowerCase()}`,
value: undefined,
classes: `govuk-input--width-${width}`,
type: "number",

// note that the DatePartsField creates NumberFields which are mapped to `items` and passed to the
// govukdateinput macro, the type and inputmode are therefore not used
type: "text",
inputmode: "numeric",
attributes: {},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ function dateComponent(name, width) {
name: `myComponent__${name.toLowerCase()}`,
value: undefined,
classes: `govuk-input--width-${width}`,
type: "number",

// note that the DateTimePartsField creates NumberFields which are mapped to `items` and passed to the
// govukdateinput macro, the type and inputmode are therefore not used
type: "text",
inputmode: "numeric",
attributes: {},
};
}
24 changes: 24 additions & 0 deletions runner/test/cases/server/plugins/engine/numberField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,28 @@ test("Prefix and suffix are passed to view model", () => {
prefix: { text: "@£%" },
suffix: { text: "&^%%" },
});

test("Text type and numeric inputmode are used by default", () => {
const numberFieldPrefixSuffix = new NumberField(baseDef);

expect(numberFieldPrefixSuffix.getViewModel({})).to.contain({
type: "text",
inputmode: "numeric",
});
});

test("Number type is used is precision is specified", () => {
const def = {
...baseDef,
schema: { precision: "0" },
};
const numberFieldPrefixSuffix = new NumberField(def);

expect(numberFieldPrefixSuffix.getViewModel({})).to.contain({
type: "number",
attributes: {
step: "0.1",
},
});
});
});
Loading