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 8 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.10",
"version": "3.4.11-rc.0",
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
"description": "Library of questionnaire components",
"repository": {
"type": "git",
Expand Down
54 changes: 42 additions & 12 deletions src/components/Loop/Loop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ export function Loop({
}
}, [nbRows, handleChanges, value]);

const removeRowWithIndex = useCallback(
(indexToRemove: number) => {
if (nbRows <= min) {
return;
}
/**
* Case 0: trying to delete
*/
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
if (indexToRemove >= nbRows || indexToRemove < 0) {
return;
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
Expand All @@ -64,19 +88,25 @@ export function Loop({
canControlRows={min !== max && Number.isFinite(max)}
>
{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,
})}
/>
<Button onClick={() => removeRowWithIndex(n)} disabled={nbRows === 1}>
{D.DEFAULT_BUTTON_REMOVE_THAT_ROW}
</Button>
</>
))}
<br />
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ce <br/> m'étonne

</CustomLoop>
);
}
Expand Down
44 changes: 42 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,22 +64,50 @@ export const RosterForLoop = (
handleChanges(newResponses);
}, [nbRows, min, valueMap, handleChanges]);

const removeRowWithIndex = useCallback(
(indexToRemove: number) => {
if (nbRows <= min) {
return;
}
/**
* Case 0: trying to delete
*/
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;
}

let cols = 0;

const headerWithActions = header
? [...header, { label: D.ACTION_HEADER }]
: undefined;

return (
<CustomLoop
{...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} />}
{headerWithActions && <TableHeader header={headerWithActions} />}
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
<Tbody>
{times(nbRows, (n) => {
const components = getComponents(n);
Expand Down Expand Up @@ -104,6 +136,14 @@ export const RosterForLoop = (
})}
wrapper={(props) => <Td {...props} />}
/>
<Td id={`delete-action-${n}`}>
<Button
onClick={() => removeRowWithIndex(n)}
disabled={cantRemove}
>
{D.DEFAULT_BUTTON_REMOVE_THAT_ROW}
</Button>
</Td>
</Tr>
{hasLineErrors && (
<Tr className="lunatic-errors">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
</div>
</div>
</td>
<td
class="lunatic-table-td"
>
<input
class="button-lunatic"
type="button"
value="Remove that row"
/>
</td>
</tr>
<tr
class="lunatic-table-tr"
Expand Down Expand Up @@ -75,6 +84,15 @@ exports[`RosterForLoop > renders the right number of columns 1`] = `
</div>
</div>
</td>
<td
class="lunatic-table-td"
>
<input
class="button-lunatic"
type="button"
value="Remove that row"
/>
</td>
</tr>
</tbody>
</table>
Expand Down
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_THAT_ROW: {
fr: 'Supprimer cette ligne',
en: 'Remove that row',
laurentC35 marked this conversation as resolved.
Show resolved Hide resolved
},
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