|
| 1 | +import React, { useRef, useState } from 'react' |
| 2 | +import { createRoot } from 'react-dom/client' |
| 3 | +import { PoundSterling } from 'lucide-react' |
| 4 | +import InputWrapper from '../../../../vendor/filament/support/resources/js/react/InputWrapper' |
| 5 | +import Icon from '../../../../vendor/filament/support/resources/js/react/Icon' |
| 6 | + |
| 7 | +function InputWrapperReact() { |
| 8 | + const [amount, setAmount] = useState('125') |
| 9 | + const [disabled, setDisabled] = useState(false) |
| 10 | + const [invalid, setInvalid] = useState(false) |
| 11 | + const [affixes, setAffixes] = useState(true) |
| 12 | + const [icons, setIcons] = useState(false) |
| 13 | + const [inline, setInline] = useState(false) |
| 14 | + const [actions, setActions] = useState(false) |
| 15 | + const input = useRef(null) |
| 16 | + return ( |
| 17 | + <div className="icon-demo" data-framework="react"> |
| 18 | + <h2 className="fi-section-header-heading">React · JavaScript</h2> |
| 19 | + <label htmlFor="react-price">Workshop price</label> |
| 20 | + <InputWrapper |
| 21 | + disabled={disabled} |
| 22 | + valid={!invalid} |
| 23 | + prefix={affixes ? '£' : null} |
| 24 | + suffix={affixes ? 'GBP' : null} |
| 25 | + inlinePrefix={inline} |
| 26 | + inlineSuffix={inline} |
| 27 | + prefixIcon={ |
| 28 | + icons ? ( |
| 29 | + <Icon aria-hidden="true"> |
| 30 | + <PoundSterling /> |
| 31 | + </Icon> |
| 32 | + ) : null |
| 33 | + } |
| 34 | + suffixActions={ |
| 35 | + actions ? ( |
| 36 | + <button |
| 37 | + type="button" |
| 38 | + disabled={disabled} |
| 39 | + onClick={() => setAmount('')} |
| 40 | + > |
| 41 | + Clear |
| 42 | + </button> |
| 43 | + ) : null |
| 44 | + } |
| 45 | + > |
| 46 | + <input |
| 47 | + ref={input} |
| 48 | + id="react-price" |
| 49 | + className="fi-input" |
| 50 | + type="number" |
| 51 | + min="1" |
| 52 | + required |
| 53 | + value={amount} |
| 54 | + onChange={(event) => setAmount(event.target.value)} |
| 55 | + disabled={disabled} |
| 56 | + aria-invalid={invalid} |
| 57 | + aria-describedby={invalid ? 'react-price-error' : undefined} |
| 58 | + /> |
| 59 | + </InputWrapper> |
| 60 | + {invalid && ( |
| 61 | + <p id="react-price-error" role="status"> |
| 62 | + Enter a workshop price of at least £1. |
| 63 | + </p> |
| 64 | + )} |
| 65 | + <div className="icon-demo-controls"> |
| 66 | + {[ |
| 67 | + [disabled, setDisabled, 'Disabled'], |
| 68 | + [invalid, setInvalid, 'Invalid'], |
| 69 | + [affixes, setAffixes, 'Text affixes'], |
| 70 | + [icons, setIcons, 'Icon'], |
| 71 | + [inline, setInline, 'Inline'], |
| 72 | + [actions, setActions, 'Clear action'], |
| 73 | + ].map(([checked, update, label]) => ( |
| 74 | + <label key={label}> |
| 75 | + <input |
| 76 | + type="checkbox" |
| 77 | + checked={checked} |
| 78 | + onChange={(event) => update(event.target.checked)} |
| 79 | + />{' '} |
| 80 | + {label} |
| 81 | + </label> |
| 82 | + ))} |
| 83 | + </div> |
| 84 | + <div className="icon-demo-controls"> |
| 85 | + <button |
| 86 | + type="button" |
| 87 | + className="fi-btn fi-size-sm" |
| 88 | + onClick={() => setInvalid(!input.current.checkValidity())} |
| 89 | + > |
| 90 | + Validate |
| 91 | + </button> |
| 92 | + <button |
| 93 | + type="button" |
| 94 | + className="fi-btn fi-size-sm" |
| 95 | + onClick={() => { |
| 96 | + setAmount('125') |
| 97 | + setDisabled(false) |
| 98 | + setInvalid(false) |
| 99 | + setAffixes(true) |
| 100 | + setIcons(false) |
| 101 | + setInline(false) |
| 102 | + setActions(false) |
| 103 | + }} |
| 104 | + > |
| 105 | + Reset |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + <p> |
| 109 | + Native input value: {amount || 'empty'}. Wrapper styling and |
| 110 | + input semantics are supplied separately. |
| 111 | + </p> |
| 112 | + </div> |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +export default function mountInputWrapperReact({ host, props: initialProps }) { |
| 117 | + const root = createRoot(host) |
| 118 | + const update = (props) => root.render(<InputWrapperReact {...props} />) |
| 119 | + update(initialProps) |
| 120 | + return { update, destroy: () => root.unmount() } |
| 121 | +} |
0 commit comments