|
| 1 | +import { IntInputControl } from './IntInput' |
| 2 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 3 | +import { faPlus, faTrash } from '@fortawesome/free-solid-svg-icons' |
| 4 | +import { useMemo } from 'react' |
| 5 | +import { getRandomString } from '@sofie-automation/corelib/dist/lib' |
| 6 | + |
| 7 | +interface IMultiLineIntInputControlProps { |
| 8 | + disabled?: boolean |
| 9 | + placeholder?: number[] |
| 10 | + |
| 11 | + /** Call handleUpdate on every change, before focus is lost */ |
| 12 | + updateOnKey?: boolean |
| 13 | + |
| 14 | + zeroBased?: boolean |
| 15 | + values: number[] |
| 16 | + handleUpdate: (values: number[]) => void |
| 17 | + |
| 18 | + min?: number |
| 19 | + max?: number |
| 20 | + step?: number |
| 21 | +} |
| 22 | +export function MultiLineIntInputControl({ |
| 23 | + values, |
| 24 | + disabled, |
| 25 | + placeholder, |
| 26 | + handleUpdate, |
| 27 | + updateOnKey, |
| 28 | + zeroBased, |
| 29 | + min, |
| 30 | + max, |
| 31 | + step, |
| 32 | +}: Readonly<IMultiLineIntInputControlProps>): JSX.Element { |
| 33 | + const handleUpdateArrayItem = (value: number, index: number) => { |
| 34 | + const clonedArray = Array.from(values) |
| 35 | + clonedArray[index] = value |
| 36 | + handleUpdate(clonedArray) |
| 37 | + } |
| 38 | + |
| 39 | + const handleDelete = (index: number) => { |
| 40 | + const clonedArray = Array.from(values) |
| 41 | + clonedArray.splice(index, 1) |
| 42 | + handleUpdate(clonedArray) |
| 43 | + } |
| 44 | + |
| 45 | + const handleAdd = () => { |
| 46 | + const clonedArray = Array.from(values) |
| 47 | + let defaultValue = 0 |
| 48 | + if (placeholder?.length) |
| 49 | + defaultValue = placeholder[placeholder.length > values.length ? values.length : placeholder.length - 1] |
| 50 | + clonedArray.push(defaultValue) |
| 51 | + handleUpdate(clonedArray) |
| 52 | + } |
| 53 | + |
| 54 | + const keys: string[] = useMemo(() => values.map(() => getRandomString()), [values.length]) |
| 55 | + |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + {values.map((value, index) => { |
| 59 | + return ( |
| 60 | + <div key={keys[index]} style={{ display: 'flex', alignItems: 'center', marginBottom: '2px' }}> |
| 61 | + <IntInputControl |
| 62 | + value={value} |
| 63 | + disabled={disabled} |
| 64 | + updateOnKey={updateOnKey} |
| 65 | + zeroBased={zeroBased} |
| 66 | + handleUpdate={(value) => { |
| 67 | + handleUpdateArrayItem(value, index) |
| 68 | + }} |
| 69 | + step={step} |
| 70 | + min={min} |
| 71 | + max={max} |
| 72 | + /> |
| 73 | + <button |
| 74 | + className="action-btn" |
| 75 | + onClick={() => handleDelete(index)} |
| 76 | + style={{ marginLeft: '10px' }} |
| 77 | + disabled={disabled} |
| 78 | + > |
| 79 | + <FontAwesomeIcon icon={faTrash} /> |
| 80 | + </button> |
| 81 | + </div> |
| 82 | + ) |
| 83 | + })} |
| 84 | + <button className="btn btn-primary" onClick={handleAdd} disabled={disabled}> |
| 85 | + <FontAwesomeIcon icon={faPlus} /> |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + ) |
| 89 | +} |
0 commit comments