From be953e3960940e854a66f4d0344a6844c5609fd4 Mon Sep 17 00:00:00 2001 From: jonathan Date: Mon, 25 Sep 2023 11:08:42 +0200 Subject: [PATCH] fix: fill iterations for loop --- .../loop/roster-for-loop/roster-for-loop.tsx | 4 ++-- src/components/type.ts | 2 +- .../fill-component-expressions.ts | 7 +----- .../fill-components/fill-iterations.ts | 22 ++++++++++++++++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/components/loop/roster-for-loop/roster-for-loop.tsx b/src/components/loop/roster-for-loop/roster-for-loop.tsx index bd71e1b7c..99f7e4caf 100644 --- a/src/components/loop/roster-for-loop/roster-for-loop.tsx +++ b/src/components/loop/roster-for-loop/roster-for-loop.tsx @@ -7,7 +7,6 @@ import { import { createCustomizableLunaticField } from '../../commons'; import { LoopButton } from '../loop-button'; import D from '../../../i18n'; -import { getInitialNbRows } from '../utils/get-initial-nb-rows'; import type { LunaticComponentProps } from '../../type'; import { Table, Tbody, Td, Tr } from '../../commons/components/html-table'; import Header from '../../table/header'; @@ -30,11 +29,12 @@ export const RosterForLoop = createCustomizableLunaticField< declarations, label, headers, + iterations, id, } = props; const min = lines?.min || DEFAULT_MIN_ROWS; const max = lines?.max || DEFAULT_MAX_ROWS; - const [nbRows, setNbRows] = useState(() => getInitialNbRows(valueMap)); + const [nbRows, setNbRows] = useState(iterations); const showButtons = min && max && min !== max; const addRow = useCallback(() => { diff --git a/src/components/type.ts b/src/components/type.ts index 822e3c509..dc9ac14ac 100644 --- a/src/components/type.ts +++ b/src/components/type.ts @@ -91,7 +91,7 @@ type ComponentPropsByType = { }; RosterForLoop: LunaticBaseProps & { lines: { min: number; max: number }; - iterations?: number; + iterations: number; getComponents: (n: number) => FilledLunaticComponentProps[]; executeExpression: LunaticState['executeExpression']; value: Record; diff --git a/src/use-lunatic/commons/fill-components/fill-component-expressions.ts b/src/use-lunatic/commons/fill-components/fill-component-expressions.ts index 98648d027..61ad04aed 100644 --- a/src/use-lunatic/commons/fill-components/fill-component-expressions.ts +++ b/src/use-lunatic/commons/fill-components/fill-component-expressions.ts @@ -23,7 +23,6 @@ const VTL_ATTRIBUTES = [ 'controls.iterations', 'lines.min', 'lines.max', - 'iterations', 'xAxisIterations', 'yAxisIterations', 'conditionFilter', @@ -45,11 +44,7 @@ export type DeepTranslateExpression = T extends LunaticExpression } : T; -function createCrawl({ - executeExpression, - iteration, - linksIterations, -}: CrawlArgs) { +function createCrawl({ executeExpression, iteration }: CrawlArgs) { /** * Translate the expression for the property */ diff --git a/src/use-lunatic/commons/fill-components/fill-iterations.ts b/src/use-lunatic/commons/fill-components/fill-iterations.ts index 4164eaa17..8ced89eba 100644 --- a/src/use-lunatic/commons/fill-components/fill-iterations.ts +++ b/src/use-lunatic/commons/fill-components/fill-iterations.ts @@ -1,5 +1,6 @@ import type { LunaticComponentDefinition, LunaticState } from '../../type'; import { hasResponse } from '../component'; +import { forceInt } from '../../../utils/number'; /** * Fill the number of iterations for loop components without "iterations" expression @@ -8,11 +9,26 @@ export function fillIterations( component: LunaticComponentDefinition, state: LunaticState ) { - // Iterations expression is not present on the component definition - if (!('components' in component && !('iterations' in component))) { + if ('iterations' in component && component.iterations) { + return { + ...component, + iterations: forceInt( + state.executeExpression(component.iterations, { + iteration: state.pager.iteration, + }) + ), + }; + } + + if ( + component.componentType !== 'RosterForLoop' && + component.componentType !== 'Loop' + ) { return component; } - // Infer the number of iterations from the value of child components + + // Iterations expression is not present on the component definition + // infer it from the value of child components const iterations = component.components.reduce((acc, component) => { if (!hasResponse(component)) { return acc;