Skip to content

Commit

Permalink
Create a submit form controller
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Oct 11, 2024
1 parent 10501b2 commit 95b06d1
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/components/Process/MultiElectionQuestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ import {
DefaultElectionFormId,

Check failure on line 9 in src/components/Process/MultiElectionQuestions.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Module '"@vocdoni/chakra-components"' has no exported member 'DefaultElectionFormId'.
VoteButtonLogic,

Check failure on line 10 in src/components/Process/MultiElectionQuestions.tsx

View workflow job for this annotation

GitHub Actions / build-stg

'"@vocdoni/chakra-components"' has no exported member named 'VoteButtonLogic'. Did you mean 'VoteButton'?
} from '@vocdoni/chakra-components'
import { Flex } from '@chakra-ui/react'
import { Flex, FormControl, FormErrorMessage, useMultiStyleConfig } from '@chakra-ui/react'
import { Controller, FieldValues, ValidateResult } from 'react-hook-form'

export type MultiElectionQuestionsFormProps = { ConnectButton?: ComponentType } & ElectionQuestionsFormProps
export type SubmitFormValidation = (values: Record<string, FieldValues>) => ValidateResult | Promise<ValidateResult>

export type MultiElectionQuestionsFormProps = {
ConnectButton?: ComponentType
validate?: SubmitFormValidation
} & ElectionQuestionsFormProps

export const MultiElectionVoteButton = (props: ButtonProps) => {
const { isAbleToVote, voting, voted } = useMultiElections()
Expand All @@ -29,12 +35,16 @@ export const MultiElectionQuestionsForm = ({
formId,

Check failure on line 35 in src/components/Process/MultiElectionQuestions.tsx

View workflow job for this annotation

GitHub Actions / build-stg

Property 'formId' does not exist on type '{ ConnectButton?: ComponentType | undefined; validate?: SubmitFormValidation | undefined; } & ChakraProps & { onInvalid?: SubmitErrorHandler<...> | undefined; }'.
onInvalid,
ConnectButton,
validate,
...props
}: MultiElectionQuestionsFormProps) => {
const styles = useMultiStyleConfig('ElectionQuestions')
const { voteAll, fmethods, renderWith } = useMultiElections()

const { handleSubmit, control } = fmethods

return (
<form onSubmit={fmethods.handleSubmit(voteAll, onInvalid)} id={formId ?? DefaultElectionFormId}>
<form onSubmit={handleSubmit(voteAll, onInvalid)} id={formId ?? DefaultElectionFormId}>
{renderWith.length > 0 && (
<Flex direction={'column'} gap={10}>
{renderWith.map(({ id }) => (
Expand All @@ -44,6 +54,27 @@ export const MultiElectionQuestionsForm = ({
))}
</Flex>
)}
{/*This controller is a trick to perform a form additional validation.
Adding the validation on the handleSubmit method caused UX errors when trying to rerun the validation again
because the error was already on error state.
On this way, the validation works as expected.*/}
<Controller
name={'handleSubmit'}
control={control}
rules={{
validate: (field, formFields: Record<string, FieldValues>) => {
if (validate) return validate(formFields)
return true
},
}}
render={({ fieldState: { error: fieldError } }) => {
return (
<FormControl isInvalid={!!fieldError?.message}>
<FormErrorMessage sx={styles.error}>{fieldError?.message as string}</FormErrorMessage>
</FormControl>
)
}}
/>
</form>
)
}
Expand Down

0 comments on commit 95b06d1

Please sign in to comment.