|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { createRoot } from 'react-dom/client' |
| 3 | +import Checkbox from '../../../../vendor/filament/support/resources/js/react/Checkbox' |
| 4 | + |
| 5 | +function CheckboxReact() { |
| 6 | + const [checked, setChecked] = useState(true) |
| 7 | + const [disabled, setDisabled] = useState(false) |
| 8 | + const [invalid, setInvalid] = useState(false) |
| 9 | + const [required, setRequired] = useState(false) |
| 10 | + const [submitted, setSubmitted] = useState('Not submitted') |
| 11 | + return ( |
| 12 | + <div className="checkbox-demo" data-framework="react"> |
| 13 | + <h2 className="fi-section-header-heading">React · JavaScript</h2> |
| 14 | + <form |
| 15 | + onSubmit={(event) => { |
| 16 | + event.preventDefault() |
| 17 | + setSubmitted( |
| 18 | + JSON.stringify([...new FormData(event.currentTarget)]), |
| 19 | + ) |
| 20 | + }} |
| 21 | + > |
| 22 | + <label> |
| 23 | + <Checkbox |
| 24 | + name="digest" |
| 25 | + value="weekly" |
| 26 | + checked={checked} |
| 27 | + onChange={(event) => setChecked(event.target.checked)} |
| 28 | + disabled={disabled} |
| 29 | + valid={!invalid} |
| 30 | + required={required} |
| 31 | + aria-invalid={invalid} |
| 32 | + />{' '} |
| 33 | + Send me the weekly digest |
| 34 | + </label> |
| 35 | + <button type="submit" className="fi-btn fi-size-sm"> |
| 36 | + Preview submission |
| 37 | + </button> |
| 38 | + </form> |
| 39 | + <div className="checkbox-demo-controls"> |
| 40 | + <label> |
| 41 | + <input |
| 42 | + type="checkbox" |
| 43 | + checked={disabled} |
| 44 | + onChange={(event) => setDisabled(event.target.checked)} |
| 45 | + />{' '} |
| 46 | + Disabled |
| 47 | + </label> |
| 48 | + <label> |
| 49 | + <input |
| 50 | + type="checkbox" |
| 51 | + checked={invalid} |
| 52 | + onChange={(event) => setInvalid(event.target.checked)} |
| 53 | + />{' '} |
| 54 | + Invalid |
| 55 | + </label> |
| 56 | + <label> |
| 57 | + <input |
| 58 | + type="checkbox" |
| 59 | + checked={required} |
| 60 | + onChange={(event) => setRequired(event.target.checked)} |
| 61 | + />{' '} |
| 62 | + Required |
| 63 | + </label> |
| 64 | + </div> |
| 65 | + <output> |
| 66 | + {checked ? 'Subscribed' : 'Not subscribed'} · {submitted} |
| 67 | + </output> |
| 68 | + <button |
| 69 | + type="button" |
| 70 | + className="fi-btn fi-size-sm" |
| 71 | + onClick={() => { |
| 72 | + setChecked(true) |
| 73 | + setDisabled(false) |
| 74 | + setInvalid(false) |
| 75 | + setRequired(false) |
| 76 | + setSubmitted('Not submitted') |
| 77 | + }} |
| 78 | + > |
| 79 | + Reset |
| 80 | + </button> |
| 81 | + </div> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +export default function mountCheckboxReact({ host, props: initialProps }) { |
| 86 | + const root = createRoot(host) |
| 87 | + const update = (props) => root.render(<CheckboxReact {...props} />) |
| 88 | + update(initialProps) |
| 89 | + |
| 90 | + return { update, destroy: () => root.unmount() } |
| 91 | +} |
0 commit comments