Files
web/packages/design-system/lib/components/Form/Checkbox/index.tsx
Matilda Haneling 6a008ba342 Merged in fix/book-149-incorrect-onfocus-behaviour-booking-widget (pull request #3320)
Fix/book 149 incorrect onfocus behaviour booking widget

* fix(BOOK-149): fixed labels shifting

* fix(BOOK-149): reintroduced sticky position

* fix(BOOK-149): added missing border to "where" text field

* added overflow to datepicker

* comment fixes

* removed separate typography declaration

* changed to onPress

* fix(BOOK-149): moved components to separate files

* fix(BOOK-149): removed desktop & mobile specific css classes

* fix(BOOK-149): new implementation of date and room modals

* dependencies update

* fix(BOOK-149): fixed child age dropdown issue, related error message, and Rooms & Guests container height

* updated info button to new variant

* fix(BOOK-149): prevent scrolling of background when modals are open in Tablet mode

* fixed overlay issue and added focus indicator on mobile

* fixed missing space in css

* rebase and fixed icon buttons after update

* simplified to use explicit boolean

* PR comments fixes

* more PR comment fixes

* PR comment fixes

* fixed setIsOpen((prev) => !prev)

* fixed issues with room error not showing properly on mobile

* fixing pr comments

* fixed flickering on GuestRoomModal


Approved-by: Erik Tiekstra
2026-01-12 14:18:51 +00:00

94 lines
2.3 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 CheckboxComponent = forwardRef<
HTMLInputElement,
React.PropsWithChildren<CheckboxProps>
>(function Checkbox(
{
className = "",
name,
children,
registerOptions,
hideError,
topAlign = false,
errorCodeMessages,
disabled = false,
},
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 || 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>
)
})
const Checkbox = CheckboxComponent as React.ForwardRefExoticComponent<
React.PropsWithChildren<CheckboxProps> & React.RefAttributes<HTMLInputElement>
>
export default Checkbox