ol-form-switch.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { FormCheck, FormCheckProps, FormLabel } from 'react-bootstrap-5'
  2. import BootstrapVersionSwitcher from '@/features/ui/components/bootstrap-5/bootstrap-version-switcher'
  3. import { getAriaAndDataProps } from '@/features/utils/bootstrap-5'
  4. type OLFormSwitchProps = FormCheckProps & {
  5. inputRef?: React.MutableRefObject<HTMLInputElement | null>
  6. bs3Props?: React.InputHTMLAttributes<HTMLInputElement>
  7. }
  8. function OLFormSwitch(props: OLFormSwitchProps) {
  9. const { bs3Props, inputRef, label, id, ...rest } = props
  10. const bs3FormSwitchProps: React.InputHTMLAttributes<HTMLInputElement> = {
  11. id,
  12. checked: rest.checked,
  13. required: rest.required,
  14. readOnly: rest.readOnly,
  15. disabled: rest.disabled,
  16. autoComplete: rest.autoComplete,
  17. defaultChecked: rest.defaultChecked,
  18. onChange: rest.onChange as (e: React.ChangeEvent<unknown>) => void,
  19. ...getAriaAndDataProps(rest),
  20. ...bs3Props,
  21. }
  22. return (
  23. <BootstrapVersionSwitcher
  24. bs3={
  25. <div className="input-switch">
  26. <input
  27. id={id}
  28. type="checkbox"
  29. className="input-switch-hidden-input"
  30. ref={inputRef}
  31. {...bs3FormSwitchProps}
  32. />
  33. <label htmlFor={id} className="input-switch-btn">
  34. <span className="sr-only">{label}</span>
  35. </label>
  36. </div>
  37. }
  38. bs5={
  39. <>
  40. <FormCheck type="switch" ref={inputRef} id={id} {...rest} />
  41. <FormLabel htmlFor={id} visuallyHidden>
  42. {label}
  43. </FormLabel>
  44. </>
  45. }
  46. />
  47. )
  48. }
  49. export default OLFormSwitch