({ inputParam, data, disabled, isDocStore = false })
| 11 | import { flowContext } from '@/store/context/ReactFlowContext' |
| 12 | |
| 13 | export const ArrayRenderer = ({ inputParam, data, disabled, isDocStore = false }) => { |
| 14 | const [arrayItems, setArrayItems] = useState([]) // these are the actual values. Ex: [{name: 'John', age: 30}, {name: 'Jane', age: 25}] |
| 15 | const [itemParameters, setItemParameters] = useState([]) // these are the input parameters for each array item. Ex: [{label: 'Name', type: 'string', display: true}, {label: 'age', type: 'number', display: false}] |
| 16 | const theme = useTheme() |
| 17 | const customization = useSelector((state) => state.customization) |
| 18 | const flowContextValue = useContext(flowContext) |
| 19 | const { reactFlowInstance } = flowContextValue || {} |
| 20 | |
| 21 | // Handler for when input values change within array items |
| 22 | const handleItemInputChange = ({ inputParam: changedParam, newValue }, itemIndex) => { |
| 23 | // Create deep copy to avoid mutating state directly |
| 24 | let clonedData = cloneDeep(data) |
| 25 | |
| 26 | // Update the specific array item that changed |
| 27 | const updatedArrayItems = [...arrayItems] |
| 28 | const updatedItem = { ...updatedArrayItems[itemIndex] } |
| 29 | |
| 30 | // Reset the value of fields which has show/hide rules, so the old values don't persist |
| 31 | for (let i = 0; i < inputParam.array.length; i += 1) { |
| 32 | const fieldDef = inputParam.array[i] |
| 33 | if (fieldDef.show || fieldDef.hide) { |
| 34 | updatedItem[fieldDef.name] = fieldDef.default || '' |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Set the new value for the changed field |
| 39 | updatedItem[changedParam.name] = newValue |
| 40 | updatedArrayItems[itemIndex] = updatedItem |
| 41 | |
| 42 | // Update local state and parent data |
| 43 | setArrayItems(updatedArrayItems) |
| 44 | data.inputs[inputParam.name] = updatedArrayItems |
| 45 | clonedData.inputs[inputParam.name] = updatedArrayItems |
| 46 | |
| 47 | // Recalculate display parameters based on new values |
| 48 | const newItemParams = showHideInputs(clonedData, 'inputParams', cloneDeep(inputParam.array), itemIndex) |
| 49 | |
| 50 | if (newItemParams.length) { |
| 51 | const updatedItemParams = [...itemParameters] |
| 52 | updatedItemParams[itemIndex] = newItemParams |
| 53 | setItemParameters(updatedItemParams) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Initialize array items and parameters when component mounts or data changes |
| 58 | useEffect(() => { |
| 59 | const initialArrayItems = data.inputs[inputParam.name] || [] |
| 60 | setArrayItems(initialArrayItems) |
| 61 | |
| 62 | // Calculate initial display parameters for each array item |
| 63 | const initialItemParameters = [] |
| 64 | for (let i = 0; i < initialArrayItems.length; i += 1) { |
| 65 | const itemParams = showHideInputs(data, 'inputParams', cloneDeep(inputParam.array), i) |
| 66 | if (itemParams.length) { |
| 67 | initialItemParameters.push(itemParams) |
| 68 | } |
| 69 | } |
| 70 |
nothing calls this directly
no test coverage detected