This is just a technology testing project based on Create React App and TailwindCSS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.7KB

  1. import React from 'react'
  2. import { Field, ErrorMessage } from 'formik'
  3. import FormikControlErrorMessage from '../FormikControlErrorMessage'
  4. import newId from '../../../utils/newId';
  5. function FormikControlCheckboxGroup(props) {
  6. const { name, label, options, ...rest } = props
  7. return (
  8. <div className="form-control mt-6 mb-4">
  9. <label className="form-control block text-lg text-gray-800">{label}</label>
  10. <Field name={name}>
  11. {({ field }) => {
  12. return options.map(option => {
  13. const htmlId = newId()
  14. return (
  15. <React.Fragment key={option.label}>
  16. <label htmlFor={htmlId} className="text-gray-600 flex items-center cursor-pointer hover:bg-gray-100">
  17. <input
  18. type='checkbox'
  19. id={htmlId}
  20. {...field}
  21. {...rest}
  22. value={option.value}
  23. checked={field.value.includes(option.value)}
  24. className='form-checkbox flex-shrink-0 m-2 text-primary-700'
  25. />
  26. <span>{option.label}</span>
  27. </label>
  28. </React.Fragment>
  29. )
  30. })
  31. }}
  32. </Field>
  33. <ErrorMessage component={FormikControlErrorMessage} name={name} />
  34. </div>
  35. )
  36. }
  37. export default FormikControlCheckboxGroup