From 8665edd98b0fb694af5b3021d8d731cc331b4c3d Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 30 Dec 2024 15:58:34 +0100 Subject: [PATCH] bump: 3.5.0-rc.3 --- package.json | 2 +- src/index.ts | 2 +- .../articulation/articulation.stories.tsx | 13 +++-- .../getArticulation.ts} | 49 ++++++++----------- 4 files changed, 31 insertions(+), 35 deletions(-) rename src/{hooks/useArticulation.ts => utils/getArticulation.ts} (64%) diff --git a/package.json b/package.json index 92dcc7db6..283beb0db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@inseefr/lunatic", - "version": "3.5.0-rc.2", + "version": "3.5.0-rc.3", "description": "Library of questionnaire components", "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index 2004cc925..11c9cbea3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ export { Button } from './components/shared/Button/Button'; export { LunaticComponents } from './components/LunaticComponents'; export { useLunatic } from './use-lunatic/use-lunatic'; -export { useArticulation } from './hooks/useArticulation'; +export { getArticulation } from './utils/getArticulation'; export type { LunaticComponentDefinition, diff --git a/src/stories/behaviour/articulation/articulation.stories.tsx b/src/stories/behaviour/articulation/articulation.stories.tsx index bced0d172..75d1dcb11 100644 --- a/src/stories/behaviour/articulation/articulation.stories.tsx +++ b/src/stories/behaviour/articulation/articulation.stories.tsx @@ -1,11 +1,11 @@ import Orchestrator from '../../utils/orchestrator'; import source from './roundabout.json'; import type { Meta, StoryObj } from '@storybook/react'; -import { useState } from 'react'; -import { useArticulation } from '../../../hooks/useArticulation'; +import { useMemo, useState } from 'react'; +import { getArticulation } from '../../../utils/getArticulation'; -type Source = Parameters[0]; -type Data = Parameters[1]; +type Source = Parameters[0]; +type Data = Parameters[1]; type Props = { source: Source; @@ -15,7 +15,10 @@ type Props = { function StoryComponent({ source, data }: Props) { const [page, setPage] = useState(null as null | string); const gotoNav = () => setPage(null); - const { items } = useArticulation(source, data); + const { items } = useMemo( + () => getArticulation(source, data), + [source, data] + ); if (page) { return ( diff --git a/src/hooks/useArticulation.ts b/src/utils/getArticulation.ts similarity index 64% rename from src/hooks/useArticulation.ts rename to src/utils/getArticulation.ts index ec2323d36..dbefba3cb 100644 --- a/src/hooks/useArticulation.ts +++ b/src/utils/getArticulation.ts @@ -3,11 +3,11 @@ import type { ComponentRoundaboutDefinition, LunaticSource, } from '../type.source'; -import type { LunaticData } from '../use-lunatic/type'; +import type { LunaticData, PageTag } from '../use-lunatic/type'; import { reducerInitializer } from '../use-lunatic/reducer/reducerInitializer'; -import { type ReactNode, useMemo } from 'react'; -import { times } from '../utils/array'; -import { forceInt } from '../utils/number'; +import { type ReactNode } from 'react'; +import { times } from './array'; +import { forceInt } from './number'; type ArticulationItem = { label: string; @@ -23,13 +23,13 @@ type Item = { cells: { label: string; value: ReactNode; - page?: string; }[]; progress: number; // -1: not completed, 0: started, 1: finished + page: PageTag; }; /** - * Hook to get articulation state + * Retrieve the articulation state * * ## Why this hook * @@ -61,35 +61,26 @@ type Item = { * - source is the ID of the roundabout component * - items define the field to extract from the roundabout data */ -export function useArticulation( +export function getArticulation( source: LunaticSource & { articulation: Articulation }, data: LunaticData ): { items: Item[] } { - const roundabout = useMemo( - () => findComponentById(source.components, source.articulation.source), - [source] + const roundabout = findComponentById( + source.components, + source.articulation.source ); - const { variables } = useMemo( - () => reducerInitializer({ source, data }), - [source, data] + const { variables } = reducerInitializer({ source, data }); + const iterations = forceInt( + variables.run(roundabout?.iterations.value ?? '0') ); - const iterations = useMemo( - () => forceInt(variables.run(roundabout?.iterations.value ?? '0')), - // eslint-disable-next-line react-hooks/exhaustive-deps - [source, data] + const rows = times(iterations, (k) => + source.articulation.items.map((item) => ({ + label: item.label, + value: variables.run(item.value, { iteration: [k] }) as ReactNode, + })) ); - const rows = useMemo(() => { - return times(iterations, (k) => - source.articulation.items.map((item) => ({ - label: item.label, - value: variables.run(item.value, { iteration: [k] }) as ReactNode, - })) - ); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [source, data, iterations, roundabout?.progressVariable]); - if (!roundabout) { return { items: [], @@ -100,7 +91,9 @@ export function useArticulation( items: rows.map((row, k) => ({ cells: row, progress: forceInt(variables.get(roundabout.progressVariable, [k]) ?? -1), - page: roundabout.page ? `${roundabout.page}.1#${k + 1}` : '1', + page: (roundabout.page + ? `${roundabout.page}.1#${k + 1}` + : '1') as PageTag, })), }; }