From 10960fad84b570b832a4e5c8d8b0ad40710a6f9b Mon Sep 17 00:00:00 2001 From: Pablo Date: Mon, 2 Jun 2025 16:21:33 +0200 Subject: [PATCH 1/4] add char counter to fields --- src/components/forms/fields/CharCounter.tsx | 14 +++++++++ src/components/forms/fields/Field.tsx | 30 ++++++++++++++----- src/components/forms/fields/TextAreaField.tsx | 26 +++++++++++++--- src/components/forms/fields/TextField.tsx | 24 +++++++++++++-- 4 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 src/components/forms/fields/CharCounter.tsx diff --git a/src/components/forms/fields/CharCounter.tsx b/src/components/forms/fields/CharCounter.tsx new file mode 100644 index 00000000..4b3df4f2 --- /dev/null +++ b/src/components/forms/fields/CharCounter.tsx @@ -0,0 +1,14 @@ +import { Text, TextProps } from '@chakra-ui/react'; + +interface CharCounterProps extends TextProps { + current: number; + max: number; +} + +export const CharCounter = ({ current, max, ...rest }: CharCounterProps) => { + return ( + max ? 'red.500' : 'gray.500'} {...rest}> + {current}/{max} + + ); +}; diff --git a/src/components/forms/fields/Field.tsx b/src/components/forms/fields/Field.tsx index ff5c18fd..d6de8e6b 100644 --- a/src/components/forms/fields/Field.tsx +++ b/src/components/forms/fields/Field.tsx @@ -1,5 +1,5 @@ import React, { FC, ReactNode } from 'react'; -import { Box, FormControl, FormControlProps, FormLabel } from '@chakra-ui/react'; +import { Box, Flex, FormControl, FormControlProps, FormLabel } from '@chakra-ui/react'; import type { FieldError } from 'react-hook-form'; import { PageText } from '../../UI'; @@ -10,9 +10,18 @@ export interface Props extends FormControlProps { label: string; helpText?: ReactNode; error?: FieldError; + helpIndicator?: ReactNode; } -export const Field: FC = ({ children, id, label, helpText, error, ...rest }) => { +export const Field: FC = ({ + children, + id, + label, + helpText, + error, + helpIndicator, + ...rest +}) => { return ( @@ -29,12 +38,17 @@ export const Field: FC = ({ children, id, label, helpText, error, ...rest {children} - {error && ( - - - {error.message} - - + {(error || helpIndicator) && ( + + + {error && ( + + {error.message} + + )} + + {helpIndicator && {helpIndicator}} + )} ); diff --git a/src/components/forms/fields/TextAreaField.tsx b/src/components/forms/fields/TextAreaField.tsx index 4929742b..73ee04ac 100644 --- a/src/components/forms/fields/TextAreaField.tsx +++ b/src/components/forms/fields/TextAreaField.tsx @@ -3,17 +3,35 @@ import { Textarea } from '@chakra-ui/react'; import { useFormContext } from 'react-hook-form'; import { Field, type Props as FieldProps } from './Field'; +import { CharCounter } from './CharCounter'; +import { MAX_TEXT_AREA_LENGTH } from '../../../constants'; -interface Props extends Omit {} +interface Props extends Omit { + maxLength?: number; +} -export const TextAreaField: FC = ({ id, isDisabled, ...rest }) => { +export const TextAreaField: FC = ({ + id, + isDisabled, + maxLength = MAX_TEXT_AREA_LENGTH, + ...rest +}) => { const { register, - formState: { errors } + formState: { errors }, + watch } = useFormContext(); + const currentValue = watch(id) || ''; + const currentLength = currentValue.length; + return ( - + } + {...rest} + >