From 9fd0a033b430d59c73d3c974098e328fdb256c14 Mon Sep 17 00:00:00 2001 From: Rory Doak Date: Wed, 9 Oct 2024 16:37:06 +0100 Subject: [PATCH] add switch and data field add whole number validation refactor: semantically simpler validation --- .../@planx/components/NumberInput/Editor.tsx | 23 +++++++++++++++---- .../@planx/components/NumberInput/model.ts | 17 +++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/editor.planx.uk/src/@planx/components/NumberInput/Editor.tsx b/editor.planx.uk/src/@planx/components/NumberInput/Editor.tsx index 7155859abf..b60e006562 100644 --- a/editor.planx.uk/src/@planx/components/NumberInput/Editor.tsx +++ b/editor.planx.uk/src/@planx/components/NumberInput/Editor.tsx @@ -3,10 +3,7 @@ import Switch from "@mui/material/Switch"; import { ComponentType as TYPES } from "@opensystemslab/planx-core/types"; import type { NumberInput } from "@planx/components/NumberInput/model"; import { parseNumberInput } from "@planx/components/NumberInput/model"; -import { - EditorProps, - ICONS, -} from "@planx/components/ui"; +import { EditorProps, ICONS } from "@planx/components/ui"; import { useFormik } from "formik"; import React from "react"; import { ModalFooter } from "ui/editor/ModalFooter"; @@ -83,7 +80,7 @@ export default function NumberInputComponent(props: Props): FCReturn { onChange={() => formik.setFieldValue( "allowNegatives", - !formik.values.allowNegatives, + !formik.values.allowNegatives ) } /> @@ -91,6 +88,22 @@ export default function NumberInputComponent(props: Props): FCReturn { label="Allow negative numbers to be input" /> + + + formik.setFieldValue( + "onlyWholeNumbers", + !formik.values.onlyWholeNumbers + ) + } + /> + } + label="Only allow whole numbers" + /> + diff --git a/editor.planx.uk/src/@planx/components/NumberInput/model.ts b/editor.planx.uk/src/@planx/components/NumberInput/model.ts index 0c98f276d8..828ab831ab 100644 --- a/editor.planx.uk/src/@planx/components/NumberInput/model.ts +++ b/editor.planx.uk/src/@planx/components/NumberInput/model.ts @@ -8,6 +8,7 @@ export interface NumberInput extends BaseNodeData { fn?: string; units?: string; allowNegatives?: boolean; + onlyWholeNumbers?: boolean; } export type UserData = number; @@ -21,13 +22,14 @@ export const parseNumber = (raw: string): number | null => { }; export const parseNumberInput = ( - data: Record | undefined, + data: Record | undefined ): NumberInput => ({ title: data?.title || "", description: data?.description, fn: data?.fn || "", units: data?.units, allowNegatives: data?.allowNegatives || false, + onlyWholeNumbers: data?.onlyWholeNumbers || false, ...parseBaseNodeData(data), }); @@ -52,6 +54,19 @@ export const numberInputValidationSchema = (input: NumberInput) => } return value === "0" ? true : Boolean(parseNumber(value)); }, + }) + .test({ + name: "check for a whole number", + message: "Enter a whole number", + test: (value: string | undefined) => { + if (!value) { + return false; + } + if (!Number.isInteger(Number(value))) { + return false; + } + return true; + }, }); export const validationSchema = (input: NumberInput) =>