Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fine row deletion #1193

Open
wants to merge 17 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4.11",
"version": "3.4.12-rc.0",
"description": "Library of questionnaire components",
"repository": {
"type": "git",
Expand Down
60 changes: 45 additions & 15 deletions src/components/Loop/Loop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,61 @@ export function Loop({
}
}, [nbRows, handleChanges, value]);

const removeRowWithIndex = useCallback(
(indexToRemove: number) => {
if (nbRows <= min) {
return;
}
const newResponses = Object.entries(value).map(([k, v]) => {
return {
name: k,
value: v?.filter((_, i) => i !== indexToRemove),
removedIndex: indexToRemove,
};
});
handleChanges(newResponses);
setNbRows((n) => n - 1);
},
[nbRows, min, value, handleChanges]
);

if (nbRows <= 0) {
return null;
}

const canControlRows = min !== max && Number.isFinite(max);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

il faut aussi que nbRows > min non ?


return (
<CustomLoop
{...props}
errors={getComponentErrors(errors, props.id)}
addRow={nbRows === max ? undefined : addRow}
removeRow={nbRows === 1 ? undefined : removeRow}
canControlRows={min !== max && Number.isFinite(max)}
canControlRows={canControlRows}
>
{times(nbRows, (n) => (
<LunaticComponents
blocklist={blockedInLoopComponents}
key={n}
components={getComponents(n)}
componentProps={(c) => ({
...props,
...c,
iteration: n,
id: `${c.id}-${n}`,
errors,
})}
/>
<>
<LunaticComponents
blocklist={blockedInLoopComponents}
key={n}
components={getComponents(n)}
componentProps={(c) => ({
...props,
...c,
iteration: n,
id: `${c.id}-${n}`,
errors,
})}
/>
{canControlRows && (
<Button
onClick={() => removeRowWithIndex(n)}
id={`delete-action-button-${n}`}
label={D.DEFAULT_BUTTON_REMOVE_THIS_ROW}
disabled={nbRows === 1}
/>
)}
</>
))}
</CustomLoop>
);
Expand Down Expand Up @@ -123,14 +153,14 @@ export const CustomLoop = slottableComponent<CustomProps>('Loop', (props) => {
{children}
<ComponentErrors errors={errors} />
{canControlRows && (
<>
<div className="button-loop">
<Button onClick={addRow} disabled={!addRow}>
{D.DEFAULT_BUTTON_ADD}
</Button>
<Button onClick={removeRow} disabled={!removeRow}>
{D.DEFAULT_BUTTON_REMOVE}
</Button>
</>
</div>
)}
</>
);
Expand Down
40 changes: 38 additions & 2 deletions src/components/RosterForLoop/RosterForLoop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Fragment, useCallback, useState } from 'react';
import type { LunaticComponentProps } from '../type';
import { Table, Tbody, Td, Tr, TableHeader } from '../shared/Table';
import { times } from '../../utils/array';
import D from '../../i18n';
import { LunaticComponents } from '../LunaticComponents';
import { blockedInLoopComponents } from '../Loop/constant';
import {
ComponentErrors,
getComponentErrors,
} from '../shared/ComponentErrors/ComponentErrors';
import { CustomLoop } from '../Loop/Loop';
import { Button } from '../shared/Button/Button';

const DEFAULT_MIN_ROWS = 1;
const DEFAULT_MAX_ROWS = 12;
Expand Down Expand Up @@ -44,6 +46,8 @@ export const RosterForLoop = (
}
}, [max, nbRows]);

const cantRemove = nbRows === min;

const removeRow = useCallback(() => {
if (nbRows <= min) {
return;
Expand All @@ -60,6 +64,28 @@ export const RosterForLoop = (
handleChanges(newResponses);
}, [nbRows, min, valueMap, handleChanges]);

const removeRowWithIndex = useCallback(
(indexToRemove: number) => {
if (nbRows <= min) {
return;
}
// trying to delete with indexToRemove out of array index
if (indexToRemove >= nbRows || indexToRemove < 0) {
return;
}
const newResponses = Object.entries(valueMap).map(([k, v]) => {
return {
name: k,
value: v?.filter((_, i) => i !== indexToRemove),
removedIndex: indexToRemove,
};
});
handleChanges(newResponses);
setNbRows((n) => n - 1);
},
[nbRows, min, valueMap, handleChanges]
);

if (nbRows === 0) {
return null;
}
Expand All @@ -71,11 +97,13 @@ export const RosterForLoop = (
{...props}
errors={getComponentErrors(errors, props.id)}
addRow={nbRows === max ? undefined : addRow}
removeRow={nbRows === min ? undefined : removeRow}
removeRow={cantRemove ? undefined : removeRow}
canControlRows={!!(min && max && min !== max)}
>
<Table id={id}>
{header && <TableHeader header={header} />}
{header && (
<TableHeader header={[...header, { label: D.ACTION_HEADER }]} />
)}
<Tbody>
{times(nbRows, (n) => {
const components = getComponents(n);
Expand Down Expand Up @@ -104,6 +132,14 @@ export const RosterForLoop = (
})}
wrapper={(props) => <Td {...props} />}
/>
<Td id={`delete-action-${n}`}>
<Button
onClick={() => removeRowWithIndex(n)}
id={`delete-action-button-${n}`}
label={D.DEFAULT_BUTTON_REMOVE_THIS_ROW}
disabled={cantRemove}
/>
</Td>
</Tr>
{hasLineErrors && (
<Tr className="lunatic-errors">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
</div>
</div>
</td>
<td
class="lunatic-table-td"
>
<input
class="button-lunatic"
id="delete-action-button-0"
type="button"
value="Remove this row"
/>
</td>
</tr>
<tr
class="lunatic-table-tr"
Expand Down Expand Up @@ -75,18 +85,32 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
</div>
</div>
</td>
<td
class="lunatic-table-td"
>
<input
class="button-lunatic"
id="delete-action-button-1"
type="button"
value="Remove this row"
/>
</td>
</tr>
</tbody>
</table>
<input
class="button-lunatic"
type="button"
value="Add row"
/>
<input
class="button-lunatic"
type="button"
value="Remove row"
/>
<div
class="button-loop"
>
<input
class="button-lunatic"
type="button"
value="Add row"
/>
<input
class="button-lunatic"
type="button"
value="Remove row"
/>
</div>
</div>
`;
5 changes: 5 additions & 0 deletions src/i18n/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const dictionary = {
DEFAULT_BUTTON_ADD: { fr: 'Ajouter une ligne', en: 'Add row' },
DEFAULT_BUTTON_REMOVE: { fr: 'Supprimer une ligne', en: 'Remove row' },
DEFAULT_BUTTON_REMOVE_THIS_ROW: {
fr: 'Supprimer cette ligne',
en: 'Remove this row',
},
ACTION_HEADER: { fr: 'Action', en: 'Action' },
MODAL_IGNORE: { fr: 'Poursuivre', en: 'Ignore' },
MODAL_CORRECT: { fr: 'Corriger ma réponse', en: 'Correct' },
DK: { fr: 'Ne sais pas', en: "Don't know" },
Expand Down
2 changes: 1 addition & 1 deletion src/stories/pairwise/data.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"COLLECTED": {
"PRENOM": { "COLLECTED": ["Dad", "Mom", "Unknow"] },
"PRENOM": { "COLLECTED": ["Dad", "Mom", "Daughter"] },
"AGE": { "COLLECTED": [30, 29, 5] },
"LINKS": {
"COLLECTED": [[null]]
Expand Down
4 changes: 3 additions & 1 deletion src/stories/pairwise/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@
"resizing": {
"PRENOM": {
"sizeForLinksVariables": ["count(PRENOM)", "count(PRENOM)"],
"linksVariables": ["LINKS"]
"linksVariables": ["LINKS"],
"size": "count(PRENOM)",
"variables": ["AGE"]
}
}
}
1 change: 0 additions & 1 deletion src/type.source.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
Expand Down
Loading
Loading