Fix/BOOK-323 enter details scroll error * fix(BOOK-323): scroll to invalid element on submit on enter details * fix(BOOK-323): update error message design * fix(BOOK-323): clean up * fix(BOOK-323): scroll to fields in room in right order * fix(BOOK-323): add id to translations * fix(BOOK-323): remove undefined * fix(BOOK-323): fix submitting state * fix(BOOK-323): use ref in multiroom for scrolling to right element, add membershipNo * fix(BOOK-323): fix invalid border country * fix(BOOK-323): use error message component * fix(BOOK-323): fix invalid focused styling on mobile * fix(BOOK-323): remove redundant dependency in callback Approved-by: Erik Tiekstra
89 lines
2.1 KiB
TypeScript
89 lines
2.1 KiB
TypeScript
'use client'
|
|
|
|
import { forwardRef } from 'react'
|
|
import { Checkbox as AriaCheckbox } from 'react-aria-components'
|
|
import {
|
|
type RegisterOptions,
|
|
useController,
|
|
useFormContext,
|
|
} from 'react-hook-form'
|
|
|
|
import styles from './checkbox.module.css'
|
|
import { MaterialIcon } from '../../Icons/MaterialIcon'
|
|
import { ErrorMessage } from '../ErrorMessage'
|
|
|
|
interface CheckboxProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
|
name: string
|
|
registerOptions?: RegisterOptions
|
|
hideError?: boolean
|
|
topAlign?: boolean
|
|
errorCodeMessages?: Record<string, string>
|
|
}
|
|
|
|
const Checkbox = forwardRef<
|
|
HTMLInputElement,
|
|
React.PropsWithChildren<CheckboxProps>
|
|
>(function Checkbox(
|
|
{
|
|
className = '',
|
|
name,
|
|
children,
|
|
registerOptions,
|
|
hideError,
|
|
topAlign = false,
|
|
errorCodeMessages,
|
|
},
|
|
ref
|
|
) {
|
|
const { control } = useFormContext()
|
|
const { field, fieldState, formState } = useController({
|
|
control,
|
|
name,
|
|
rules: registerOptions,
|
|
})
|
|
|
|
return (
|
|
<AriaCheckbox
|
|
className={`${styles.container} ${className}`}
|
|
isSelected={field.value}
|
|
onChange={field.onChange}
|
|
data-testid={name}
|
|
name={name}
|
|
isDisabled={registerOptions?.disabled}
|
|
excludeFromTabOrder
|
|
>
|
|
{({ isSelected }) => (
|
|
<>
|
|
<span
|
|
className={`${styles.checkboxContainer} ${topAlign ? styles.topAlign : ''}`}
|
|
>
|
|
<span
|
|
className={styles.checkbox}
|
|
tabIndex={registerOptions?.disabled ? undefined : 0}
|
|
ref={ref}
|
|
>
|
|
{isSelected && (
|
|
<MaterialIcon icon="check" color="Icon/Inverted" />
|
|
)}
|
|
</span>
|
|
{children}
|
|
</span>
|
|
{fieldState.error && !hideError ? (
|
|
<ErrorMessage
|
|
errors={formState.errors}
|
|
name={name}
|
|
messageLabel={
|
|
(fieldState.error.message &&
|
|
errorCodeMessages?.[fieldState.error.message]) ||
|
|
fieldState.error.message
|
|
}
|
|
/>
|
|
) : null}
|
|
</>
|
|
)}
|
|
</AriaCheckbox>
|
|
)
|
|
})
|
|
|
|
export default Checkbox
|