-
Notifications
You must be signed in to change notification settings - Fork 0
DS-1264: feat(checkbox, checkbox-group): add required state for checkboxes #1035
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,10 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ChangeEvent, VoidFunctionComponent } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ChangeEvent, useState, VoidFunctionComponent } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import styled from 'styled-components'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useDataAttributes } from '../../hooks/use-data-attributes'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Checkbox } from '../checkbox/checkbox'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useTranslation } from '../../i18n/use-translation'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useId } from '../../hooks/use-id'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { InvalidField } from '../feedbacks/invalid-field'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const Legend = styled.legend` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| font-size: 0.75rem; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -10,9 +13,18 @@ const Legend = styled.legend` | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding: 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const InvalidFieldContainer = styled.div` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| margin: calc(var(--spacing-1x) * -1) 0 0 var(--spacing-1x); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| padding-bottom: var(--spacing-1x); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface CheckboxProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkedValues?: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| valid?: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validationErrorMessage?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxGroup: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,25 +37,53 @@ interface CheckboxProps { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export const CheckboxGroup: VoidFunctionComponent<CheckboxProps> = ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: providedId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkedValues, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkboxGroup, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| valid = true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| validationErrorMessage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...props | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { t } = useTranslation('checkbox-group'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const id = useId(providedId); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dataAttributes = useDataAttributes(props); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const dataTestId = dataAttributes['data-testid'] ?? 'checkboxGroup'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const [checkedState, setCheckedState] = useState( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| new Array(checkboxGroup.length).fill(false), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const areAllCheckboxUnchecked = checkedState.every((e) => e === false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handleOnChange = (position: number): void => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const updatedCheckedState = checkedState.map((item, index) => (index === position ? !item : item)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setCheckedState(updatedCheckedState); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+55
to
+64
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Peut-être
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {label && <Legend>{label}</Legend>} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| required && !valid && areAllCheckboxUnchecked | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. En fait je sais pas si le valid est pertinent, si on a la prop, ça devrait probablement prendre le dessus sur le state |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <InvalidFieldContainer> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <InvalidField | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| controlId={id} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| feedbackMsg={validationErrorMessage || t('validationErrorMessage')} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </InvalidFieldContainer> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {checkboxGroup.map(({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defaultChecked, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| disabled, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label: checkboxLabel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, pos) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Checkbox | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| key={`${name}-${value}`} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checked={checkedValues?.includes(value)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -52,8 +92,12 @@ export const CheckboxGroup: VoidFunctionComponent<CheckboxProps> = ({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| disabled={disabled} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| label={checkboxLabel} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name={name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| valid={required ? valid || !areAllCheckboxUnchecked : valid} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value={value} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange={onChange} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange={(event: ChangeEvent<HTMLInputElement>) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOnChange(pos); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Et de changer ici pour |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange?.(event); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je suis pas certain qu'on doive gérer le state. Ça devrait être synchronisé avec les
checkedValues. Ou sinon il faudrait gérer le mode controlled et uncontrolled. Là on est un peu entre les deux.Vous en pensez quoi @pylafleur @savutsang ? Est-ce qu'on se sert tant que ça du mode uncontrolled?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Le message d'invalidité et l'outline rouge des checkboxes doivent disparaître lorsqu'un checkbox est selectionné. Comment est-ce que je peux m'assurer que ce comporement là se fasse sans utiliser le state?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
La gestion du state du checkbox group c'est un peu le bordel si on veut supporter le mode uncontrolled 🤯. Et en fait je pense qu'on devrait peut-être pas le supporter, parce que de toute façon je sais pas comment on pourrait faire fonctionner le
requiredsans gérer le state complètement.