This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMatrixProperty.tsx
More file actions
112 lines (105 loc) · 3.08 KB
/
MatrixProperty.tsx
File metadata and controls
112 lines (105 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { FieldArray, useField } from 'formik';
import { useCallback, useEffect, useMemo, useState } from 'react';
import Switch from '../../../icons/ui/Switch';
import { FieldlessInspectorProperty } from './InspectorProperty';
import { FieldlessListProperty, ListPropertyProps } from './ListProperty';
export type MatrixPropertyProps = ListPropertyProps & {
namePrefix?: string;
};
export const MatrixProperty = ({
name,
namePrefix,
values,
label,
...props
}: MatrixPropertyProps) => {
const params = values.parameters;
const matrix = useMemo(
() => (params?.matrix ? params.matrix.parameters[name] : undefined),
[params, name],
);
const matrixField = useField({
...props,
name: namePrefix
? `${namePrefix}.matrix.${name}`
: `matrix.parameters.${name}`,
});
const defaultField = useField({
...props,
name: namePrefix ? `${namePrefix}.${name}` : name,
});
const [inputDef, , helperDef] = defaultField;
const [inputMatrix, , helperMatrix] = matrixField;
const [isMatrix, setMatrix] = useState(matrix !== undefined);
const setValue = helperMatrix.setValue;
useEffect(() => {
if (matrix && matrix !== inputMatrix.value) {
setValue(matrix);
}
}, [matrix, setValue, inputMatrix]);
const callback = useCallback(() => {
if (isMatrix) {
helperDef.setValue(inputMatrix.value ? inputMatrix.value[0] : '');
helperMatrix.setValue(undefined);
} else {
helperMatrix.setValue([inputDef.value ?? '']);
helperDef.setValue(undefined);
}
setMatrix(!isMatrix);
}, [inputDef.value, inputMatrix.value, helperDef, helperMatrix, isMatrix]);
const switchButton = useMemo(
() => (
<button
type="button"
onClick={callback}
className="hover:bg-circle-gray-400 bg-circle-gray-300 p-1 rounded transition-colors w-8 h-8 ml-2"
>
<Switch className="w-5 h-5 m-auto" />
</button>
),
[callback],
);
return (
<>
{isMatrix ? (
<FieldArray
{...matrixField}
name={
namePrefix
? `${namePrefix}.matrix.parameters.${name}`
: `matrix.parameters.${name}`
}
render={(arrayHelper) => (
<FieldlessListProperty
{...props}
description={props.description}
label={label}
titleFont="font-medium text-sm"
name={
namePrefix
? `${namePrefix}.matrix.parameters.${name}`
: `matrix.parameters.${name}`
}
arrayHelper={arrayHelper}
values={values}
expanded
addButton
pinned={switchButton}
field={matrixField}
/>
)}
/>
) : (
<FieldlessInspectorProperty
{...props}
label={label}
labelClassName="h-8"
description={props.description}
name={namePrefix ? `${namePrefix}.${name}` : name}
field={defaultField}
pinned={switchButton}
/>
)}
</>
);
};