Skip to content

Commit

Permalink
bump: 3.5.0-rc.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Grafikart committed Dec 30, 2024
1 parent 3662a47 commit 8665edd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions src/stories/behaviour/articulation/articulation.stories.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof useArticulation>[0];
type Data = Parameters<typeof useArticulation>[1];
type Source = Parameters<typeof getArticulation>[0];
type Data = Parameters<typeof getArticulation>[1];

type Props = {
source: Source;
Expand All @@ -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 (
Expand Down
49 changes: 21 additions & 28 deletions src/hooks/useArticulation.ts → src/utils/getArticulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand Down Expand Up @@ -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: [],
Expand All @@ -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,
})),
};
}
Expand Down

0 comments on commit 8665edd

Please sign in to comment.