From bfb9cefe7278d3b3e56f6afee4fd98d8a00e7f4d Mon Sep 17 00:00:00 2001 From: Jesse van der Poel Date: Tue, 13 Feb 2024 16:56:22 +0100 Subject: [PATCH 1/7] Focus checkbox if invalid and form is submitted. --- .../components/FormComponents/CheckboxElement.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx index b2188951ac..a00d629fee 100644 --- a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx +++ b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx @@ -17,6 +17,7 @@ import { SxProps, Theme, } from '@mui/material' +import { useRef } from 'react' export type CheckboxElementProps = Omit & { parseError?: (error: FieldError) => string @@ -42,12 +43,15 @@ export function CheckboxElement({ rules.required = i18n._(/* i18n */ 'This field is required') } + const checkboxRef = useRef(null) + return ( { + if (invalid) checkboxRef.current?.focus() const parsedHelperText = error ? typeof parseError === 'function' ? parseError(error) @@ -60,6 +64,7 @@ export function CheckboxElement({ label={label || ''} control={ Date: Tue, 13 Feb 2024 16:57:34 +0100 Subject: [PATCH 2/7] Changed order of props --- .../ecommerce-ui/components/FormComponents/CheckboxElement.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx index a00d629fee..834161236f 100644 --- a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx +++ b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx @@ -64,8 +64,8 @@ export function CheckboxElement({ label={label || ''} control={ Date: Tue, 13 Feb 2024 16:58:51 +0100 Subject: [PATCH 3/7] added changeset --- .changeset/eight-flowers-heal.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eight-flowers-heal.md diff --git a/.changeset/eight-flowers-heal.md b/.changeset/eight-flowers-heal.md new file mode 100644 index 0000000000..dc8db35799 --- /dev/null +++ b/.changeset/eight-flowers-heal.md @@ -0,0 +1,5 @@ +--- +"@graphcommerce/ecommerce-ui": minor +--- + +Invalid checkboxes are now focussed From c07dadb35982dd61dbc9baff42252b0d568fc0b8 Mon Sep 17 00:00:00 2001 From: Jesse van der Poel Date: Wed, 14 Feb 2024 09:33:38 +0100 Subject: [PATCH 4/7] changed the ref --- .../components/FormComponents/CheckboxElement.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx index 834161236f..a9a5bb990b 100644 --- a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx +++ b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx @@ -17,7 +17,6 @@ import { SxProps, Theme, } from '@mui/material' -import { useRef } from 'react' export type CheckboxElementProps = Omit & { parseError?: (error: FieldError) => string @@ -43,15 +42,12 @@ export function CheckboxElement({ rules.required = i18n._(/* i18n */ 'This field is required') } - const checkboxRef = useRef(null) - return ( { - if (invalid) checkboxRef.current?.focus() + render={({ field: { value, onChange, ref }, fieldState: { invalid, error } }) => { const parsedHelperText = error ? typeof parseError === 'function' ? parseError(error) @@ -65,7 +61,7 @@ export function CheckboxElement({ control={ Date: Mon, 19 Feb 2024 13:52:34 +0100 Subject: [PATCH 5/7] updated changeset --- .changeset/eight-flowers-heal.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/eight-flowers-heal.md b/.changeset/eight-flowers-heal.md index dc8db35799..5ca3561501 100644 --- a/.changeset/eight-flowers-heal.md +++ b/.changeset/eight-flowers-heal.md @@ -1,5 +1,5 @@ --- -"@graphcommerce/ecommerce-ui": minor +'@graphcommerce/ecommerce-ui': patch --- Invalid checkboxes are now focussed From 72658999d789cd9089a2b182b7633a326b07aedf Mon Sep 17 00:00:00 2001 From: Paul Hachmang Date: Mon, 19 Feb 2024 14:48:01 +0100 Subject: [PATCH 6/7] CheckboxElement, MultiSelectElement, NumberFieldElement, SelectElement, SliderElement and TextFieldElement have their inputRef passed, allowing focus to be set by the form. --- .changeset/eight-flowers-heal.md | 2 +- .../FormComponents/CheckboxElement.tsx | 8 ++-- .../FormComponents/MultiSelectElement.tsx | 38 +++++++++---------- .../FormComponents/NumberFieldElement.tsx | 8 ++-- .../FormComponents/SelectElement.tsx | 5 +-- .../FormComponents/SliderElement.tsx | 9 +---- .../FormComponents/TextFieldElement.tsx | 8 ++-- 7 files changed, 35 insertions(+), 43 deletions(-) diff --git a/.changeset/eight-flowers-heal.md b/.changeset/eight-flowers-heal.md index 5ca3561501..3d513d836f 100644 --- a/.changeset/eight-flowers-heal.md +++ b/.changeset/eight-flowers-heal.md @@ -2,4 +2,4 @@ '@graphcommerce/ecommerce-ui': patch --- -Invalid checkboxes are now focussed +CheckboxElement, MultiSelectElement, NumberFieldElement, SelectElement, SliderElement and TextFieldElement have their inputRef passed, allowing focus to be set by the form. diff --git a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx index a9a5bb990b..913cb6a85b 100644 --- a/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx +++ b/packages/ecommerce-ui/components/FormComponents/CheckboxElement.tsx @@ -47,7 +47,8 @@ export function CheckboxElement({ name={name} rules={rules} control={control} - render={({ field: { value, onChange, ref }, fieldState: { invalid, error } }) => { + render={({ field: { value, onChange, ref, ...field }, fieldState: { invalid, error } }) => { + // eslint-disable-next-line no-nested-ternary const parsedHelperText = error ? typeof parseError === 'function' ? parseError(error) @@ -61,6 +62,7 @@ export function CheckboxElement({ control={ ({ }} value={value} checked={!!value} - onChange={() => { - onChange(!value) - }} + onChange={() => onChange(!value)} /> } /> diff --git a/packages/ecommerce-ui/components/FormComponents/MultiSelectElement.tsx b/packages/ecommerce-ui/components/FormComponents/MultiSelectElement.tsx index 11998bdd59..4a325b5576 100644 --- a/packages/ecommerce-ui/components/FormComponents/MultiSelectElement.tsx +++ b/packages/ecommerce-ui/components/FormComponents/MultiSelectElement.tsx @@ -72,7 +72,7 @@ export function MultiSelectElement( name={name} rules={rules} control={control} - render={({ field: { value, onChange, onBlur }, fieldState: { invalid, error } }) => { + render={({ field: { value, onChange, ...field }, fieldState: { invalid, error } }) => { helperText = error ? typeof parseError === 'function' ? parseError(error) @@ -102,6 +102,7 @@ export function MultiSelectElement( )}