|
| 1 | +import React from 'react'; |
| 2 | +import arrayMove from 'array-move'; |
| 3 | + |
| 4 | +import SortableList from './List'; |
| 5 | + |
| 6 | +const items = { |
| 7 | + input: <input placeholder="Regular text input" />, |
| 8 | + textarea: <textarea placeholder="Textarea input" />, |
| 9 | + select: ( |
| 10 | + <select> |
| 11 | + <option>Option 1</option> |
| 12 | + <option>Option 2</option> |
| 13 | + <option>Option 3</option> |
| 14 | + </select> |
| 15 | + ), |
| 16 | + checkbox: ( |
| 17 | + <> |
| 18 | + <label> |
| 19 | + <input type="checkbox" name="checkbox" /> |
| 20 | + Checkbox 1 |
| 21 | + </label> |
| 22 | + <label> |
| 23 | + <input type="checkbox" name="checkbox" /> |
| 24 | + Checkbox 2 |
| 25 | + </label> |
| 26 | + </> |
| 27 | + ), |
| 28 | + radio: ( |
| 29 | + <> |
| 30 | + <label> |
| 31 | + <input type="radio" name="option" /> |
| 32 | + Option 1 |
| 33 | + </label> |
| 34 | + <label> |
| 35 | + <input type="radio" name="option" /> |
| 36 | + Option 2 |
| 37 | + </label> |
| 38 | + </> |
| 39 | + ), |
| 40 | + range: <input type="range" min="1" max="100" />, |
| 41 | + contentEditable: ( |
| 42 | + <div |
| 43 | + contentEditable |
| 44 | + dangerouslySetInnerHTML={{ |
| 45 | + __html: 'Lorem ipsum <strong>dolor sit</strong> amet', |
| 46 | + }} |
| 47 | + /> |
| 48 | + ), |
| 49 | +}; |
| 50 | + |
| 51 | +export default class InteractiveElements extends React.Component { |
| 52 | + state = { |
| 53 | + items: Object.entries(items), |
| 54 | + }; |
| 55 | + |
| 56 | + render() { |
| 57 | + return ( |
| 58 | + <SortableList |
| 59 | + // The distance prop isn't strictly required for this example, but it is recommended |
| 60 | + // to set it to a low value for sortable items with nested interactive elements |
| 61 | + // such as clickable labels for checkbox / radio inputs |
| 62 | + distance={2} |
| 63 | + items={this.state.items} |
| 64 | + onSortEnd={this.onSortEnd} |
| 65 | + /> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + onSortEnd = ({oldIndex, newIndex}) => { |
| 70 | + if (oldIndex === newIndex) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + this.setState(({items}) => ({ |
| 75 | + items: arrayMove(items, oldIndex, newIndex), |
| 76 | + })); |
| 77 | + }; |
| 78 | +} |
0 commit comments