|
| 1 | +import React, { useState } from 'react' |
| 2 | +import { createRoot } from 'react-dom/client' |
| 3 | +import Actions from '../../../../vendor/filament/support/resources/js/react/Actions' |
| 4 | + |
| 5 | +function ActionsReact() { |
| 6 | + const [alignment, setAlignment] = useState('start') |
| 7 | + const [fullWidth, setFullWidth] = useState(false) |
| 8 | + const [visible, setVisible] = useState(true) |
| 9 | + const [name, setName] = useState('Community garden') |
| 10 | + const [message, setMessage] = useState('No changes saved.') |
| 11 | + return ( |
| 12 | + <section className="actions-demo" data-framework="react"> |
| 13 | + <h2 className="fi-section-header-heading">React · JavaScript</h2> |
| 14 | + <form |
| 15 | + onSubmit={(event) => { |
| 16 | + event.preventDefault() |
| 17 | + setMessage(`Saved ${name} locally.`) |
| 18 | + }} |
| 19 | + > |
| 20 | + <label> |
| 21 | + Project name{' '} |
| 22 | + <input |
| 23 | + data-testid="name" |
| 24 | + value={name} |
| 25 | + onChange={(event) => setName(event.target.value)} |
| 26 | + /> |
| 27 | + </label> |
| 28 | + {visible && ( |
| 29 | + <Actions |
| 30 | + alignment={alignment} |
| 31 | + fullWidth={fullWidth} |
| 32 | + data-testid="actions" |
| 33 | + > |
| 34 | + <button type="submit" data-testid="save"> |
| 35 | + Save |
| 36 | + </button> |
| 37 | + <button |
| 38 | + type="button" |
| 39 | + data-testid="archive" |
| 40 | + onClick={() => |
| 41 | + setMessage(`Archived ${name} locally.`) |
| 42 | + } |
| 43 | + > |
| 44 | + Archive |
| 45 | + </button> |
| 46 | + <a href="#react-project">View project</a> |
| 47 | + </Actions> |
| 48 | + )} |
| 49 | + </form> |
| 50 | + <div className="icon-demo-controls"> |
| 51 | + <label> |
| 52 | + Alignment{' '} |
| 53 | + <select |
| 54 | + data-testid="alignment" |
| 55 | + value={alignment} |
| 56 | + onChange={(event) => setAlignment(event.target.value)} |
| 57 | + > |
| 58 | + {[ |
| 59 | + 'start', |
| 60 | + 'left', |
| 61 | + 'center', |
| 62 | + 'end', |
| 63 | + 'right', |
| 64 | + 'between', |
| 65 | + 'justify', |
| 66 | + ].map((value) => ( |
| 67 | + <option key={value}>{value}</option> |
| 68 | + ))} |
| 69 | + </select> |
| 70 | + </label> |
| 71 | + <label> |
| 72 | + <input |
| 73 | + type="checkbox" |
| 74 | + data-testid="full-width" |
| 75 | + checked={fullWidth} |
| 76 | + onChange={(event) => setFullWidth(event.target.checked)} |
| 77 | + /> |
| 78 | + Full width |
| 79 | + </label> |
| 80 | + <label> |
| 81 | + <input |
| 82 | + type="checkbox" |
| 83 | + data-testid="visible" |
| 84 | + checked={visible} |
| 85 | + onChange={(event) => setVisible(event.target.checked)} |
| 86 | + /> |
| 87 | + Show actions |
| 88 | + </label> |
| 89 | + <button |
| 90 | + type="button" |
| 91 | + data-testid="reset" |
| 92 | + onClick={() => { |
| 93 | + setAlignment('start') |
| 94 | + setFullWidth(false) |
| 95 | + setVisible(true) |
| 96 | + setName('Community garden') |
| 97 | + setMessage('No changes saved.') |
| 98 | + }} |
| 99 | + > |
| 100 | + Reset |
| 101 | + </button> |
| 102 | + </div> |
| 103 | + <p role="status" data-testid="result"> |
| 104 | + {message} |
| 105 | + </p> |
| 106 | + <p id="react-project" tabIndex={-1}> |
| 107 | + Community garden · Project details |
| 108 | + </p> |
| 109 | + </section> |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +export default function mountActionsReact({ host, props }) { |
| 114 | + const root = createRoot(host) |
| 115 | + root.render(<ActionsReact {...props} />) |
| 116 | + return { |
| 117 | + update: (next) => root.render(<ActionsReact {...next} />), |
| 118 | + destroy: () => root.unmount(), |
| 119 | + } |
| 120 | +} |
0 commit comments