Skip to content

Commit

Permalink
Merge pull request #345 from mission-apprentissage/ui/enhance-multise…
Browse files Browse the repository at this point in the history
…lect

[UI] Ajoute un selectAll au multiselect
  • Loading branch information
yohanngab authored Oct 30, 2024
2 parents 58e2c5b + 69a1082 commit ee2ee76
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
32 changes: 29 additions & 3 deletions ui/src/Components/MultiSelect/MultiSelect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-undef */

import Checkbox from "@codegouvfr/react-dsfr/Checkbox";
import Input from "@codegouvfr/react-dsfr/Input";
import { Checkbox } from "@codegouvfr/react-dsfr/Checkbox";
import { Input } from "@codegouvfr/react-dsfr/Input";
import { useEffect, useRef, useState } from "react";

import { isPlural } from "../../campagnes/utils";
Expand All @@ -11,7 +11,7 @@ const MultiSelect = ({ options, name, placeholder = "", label, selected, setSele
const [isOpen, setIsOpen] = useState(false);
const [values, setValues] = useState(selected || []);
const [search, setSearch] = useState("");

const [isAllSelected, setIsAllSelected] = useState(false);
const dropdownRef = useRef(null);

const displayedValues = values
Expand All @@ -31,14 +31,27 @@ const MultiSelect = ({ options, name, placeholder = "", label, selected, setSele
}
setValues(updatedValues);
setSelected(updatedValues);
setIsAllSelected(updatedValues.length === options.length);
};

useEffect(() => {
if (!values?.length && selected?.length) {
setValues(selected);
}
setIsAllSelected(selected?.length === options?.length);
}, [selected]);

const handleSelectAll = (e) => {
if (e.target.checked) {
setValues(options.map((option) => option.value));
setSelected(options.map((option) => option.value));
setIsAllSelected(true);
} else {
setValues([]);
setIsAllSelected(false);
}
};

useEffect(() => {
const handleClickOutside = (event) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
Expand Down Expand Up @@ -98,6 +111,19 @@ const MultiSelect = ({ options, name, placeholder = "", label, selected, setSele
</InputContainer>
{isOpen && (
<MenuContainer>
<Checkbox
options={[
{
label: isAllSelected ? "Tout désélectionner" : "Tout sélectionner",
nativeInputProps: {
name: `multiselect-${name}-select-all`,
checked: isAllSelected,
onChange: handleSelectAll,
},
},
]}
small
/>
{(search && formattedSearchedOptions.length) || (!search && formattedOptions.length) ? (
<Checkbox options={search ? formattedSearchedOptions : formattedOptions} small />
) : (
Expand Down
6 changes: 3 additions & 3 deletions ui/src/campagnes/CreateCampagnes/FormationsSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import BeatLoader from "react-spinners/BeatLoader";
import { DIPLOME_TYPE_MATCHER, USER_ROLES, VIEW_TYPES } from "../../constants";
import { UserContext } from "../../context/UserContext";
import useFetchCampagnes from "../../hooks/useFetchCampagnes";
import useFetchDiplomesWithCampagnesCount from "../../hooks/useFetchDiplomesWithCampagnesCount";
import useFetchDiplomesAndEtablissementsFilter from "../../hooks/useFetchDiplomesAndEtablissementsFilter";
import useFetchRemoteFormations from "../../hooks/useFetchRemoteFormations";
import { remoteEtablissementLabelGetterFromFormation } from "../../utils/etablissement";
import FilterButtons from "../Shared/FilterButtons/FilterButtons";
Expand Down Expand Up @@ -114,7 +114,7 @@ const FormationsSelector = ({ selectedFormations, setSelectedFormations }) => {
diplomesFilter,
etablissementsFilter,
isSuccess: isSuccessDiplomesAndEtablissementsFilter,
} = useFetchDiplomesWithCampagnesCount();
} = useFetchDiplomesAndEtablissementsFilter();

const {
campagnes: campagnes,
Expand Down Expand Up @@ -185,7 +185,7 @@ const FormationsSelector = ({ selectedFormations, setSelectedFormations }) => {
/>
)}
<>
{isSuccessFormations && (
{isSuccessDiplomesAndEtablissementsFilter && (
<>
<FilterButtons
search={search}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "../../../constants";
import { UserContext } from "../../../context/UserContext";
import useFetchCampagnes from "../../../hooks/useFetchCampagnes";
import useFetchDiplomesWithCampagnesCount from "../../../hooks/useFetchDiplomesWithCampagnesCount";
import useFetchDiplomesAndEtablissementsFilter from "../../../hooks/useFetchDiplomesAndEtablissementsFilter";
import { etablissementLabelGetterFromFormation } from "../../../utils/etablissement";
import ActionButtons from "../../ManageCampagne/ActionButtons/ActionButtons";
import { ButtonContainer } from "../../styles/resultsCampagnes.style";
Expand Down Expand Up @@ -62,7 +62,7 @@ const CampagnesSelector = ({
diplomesFilter,
etablissementsFilter,
isSuccess: isSuccessDiplomesAndEtablissementsFilter,
} = useFetchDiplomesWithCampagnesCount();
} = useFetchDiplomesAndEtablissementsFilter();

const currentPageCampagneIds = campagnes?.map((campagne) => campagne.id);

Expand Down
4 changes: 2 additions & 2 deletions ui/src/campagnes/Shared/FilterButtons/FilterButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ const FilterButtons = ({
}, [inputValue, setSearch, setIsOpened]);

useEffect(() => {
if (!selectedEtablissementsSiret) {
if (!selectedEtablissementsSiret?.length) {
setSelectedEtablissementsSiret(etablissementsOptions.map((option) => option.value));
}
}, [selectedEtablissementsSiret, etablissementsOptions]);

useEffect(() => {
if (!selectedDiplomesIntitule) {
if (!selectedDiplomesIntitule?.length) {
setSelectedDiplomesIntitule(diplomesOptions.map((option) => option.value));
}
}, [diplomesOptions]);
Expand Down

0 comments on commit ee2ee76

Please sign in to comment.