Skip to content

Commit

Permalink
fix: fill iterations for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Sep 25, 2023
1 parent a7cafc8 commit be953e3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/components/loop/roster-for-loop/roster-for-loop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type ComponentPropsByType = {
};
RosterForLoop: LunaticBaseProps<unknown> & {
lines: { min: number; max: number };
iterations?: number;
iterations: number;
getComponents: (n: number) => FilledLunaticComponentProps[];
executeExpression: LunaticState['executeExpression'];
value: Record<string, unknown[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const VTL_ATTRIBUTES = [
'controls.iterations',
'lines.min',
'lines.max',
'iterations',
'xAxisIterations',
'yAxisIterations',
'conditionFilter',
Expand All @@ -45,11 +44,7 @@ export type DeepTranslateExpression<T> = T extends LunaticExpression
}
: T;

function createCrawl({
executeExpression,
iteration,
linksIterations,
}: CrawlArgs) {
function createCrawl({ executeExpression, iteration }: CrawlArgs) {
/**
* Translate the expression for the property
*/
Expand Down
22 changes: 19 additions & 3 deletions src/use-lunatic/commons/fill-components/fill-iterations.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down

0 comments on commit be953e3

Please sign in to comment.