Skip to content

Commit 15f5aa2

Browse files
scriptorianSimon Rogers
andauthored
feat: Add support for a multiline integer array form (#1476)
Co-authored-by: Simon Rogers <[email protected]>
1 parent 3ff013d commit 15f5aa2

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

packages/webui/src/client/lib/forms/SchemaFormWithOverrides.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { SchemaFormObjectTable } from './SchemaFormTable/ObjectTable.js'
3131
import { getSchemaUIField, SchemaFormUIField } from '@sofie-automation/blueprints-integration'
3232
import { SchemaFormSectionHeader } from './SchemaFormSectionHeader.js'
3333
import { Base64ImageInputControl } from '../Components/Base64ImageInput.js'
34+
import { MultiLineIntInputControl } from '../Components/MultiLineIntInput.js'
3435
import { ToggleSwitchControl } from '../Components/ToggleSwitch.js'
3536

3637
interface SchemaFormWithOverridesProps extends SchemaFormCommonProps {
@@ -167,6 +168,9 @@ const ArrayFormWithOverrides = (props: Readonly<SchemaFormWithOverridesProps>) =
167168
} else {
168169
return <StringArrayFormWithOverrides {...childProps} />
169170
}
171+
case TypeName.Number:
172+
case TypeName.Integer:
173+
return <IntArrayFormWithOverrides {...childProps} />
170174
case TypeName.Object:
171175
if (props.allowTables) {
172176
return <SchemaFormArrayTable {...props} />
@@ -385,6 +389,22 @@ const StringArrayFormWithOverrides = ({ schema, commonAttrs }: Readonly<FormComp
385389
)
386390
}
387391

392+
const IntArrayFormWithOverrides = ({ schema, commonAttrs }: Readonly<FormComponentProps>) => {
393+
return (
394+
<LabelAndOverrides {...commonAttrs}>
395+
{(values, handleUpdate) => (
396+
<MultiLineIntInputControl
397+
placeholder={schema.default}
398+
values={values || []}
399+
handleUpdate={handleUpdate}
400+
min={schema.minimum}
401+
max={schema.maximum}
402+
/>
403+
)}
404+
</LabelAndOverrides>
405+
)
406+
}
407+
388408
const JsonFormWithOverrides = ({ schema, commonAttrs }: Readonly<FormComponentProps>) => {
389409
return (
390410
<LabelAndOverrides {...commonAttrs}>

0 commit comments

Comments
 (0)