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

fix(autocomplete): réutilisation du composant autocomplete sur la home, accentuation du contraste quand on parcours les suggestions au clavier #6429

Merged
merged 9 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
"use client";
import { ApiGeoResult, searchCities } from "./searchCities";
import {
Autocomplete,
AutocompleteProps,
} from "../common/Autocomplete/Autocomplete";
import { Autocomplete, AutocompleteProps } from "../common/Autocomplete";
import { useState } from "react";

type Props = Pick<AutocompleteProps<ApiGeoResult>, "classes"> & {
className?: string;
onLocationChange?: (location: ApiGeoResult | undefined) => void;
defaultValue?: ApiGeoResult;
};
Expand All @@ -20,9 +16,7 @@ const detectIfPostalCode = (postalCodeOrName: string): boolean => {
};

export const LocationSearchInput = ({
className,
onLocationChange,
classes,
defaultValue,
}: Props) => {
const [postalCode, setPostalCode] = useState<string | undefined>();
Expand All @@ -34,7 +28,6 @@ export const LocationSearchInput = ({

return (
<Autocomplete<ApiGeoResult>
className={className}
onChange={(value) => {
if (onLocationChange) onLocationChange(value);
}}
Expand All @@ -47,7 +40,6 @@ export const LocationSearchInput = ({
label={<>Code postal ou Ville (optionnel)</>}
state={"default"}
dataTestId={"locationSearchAutocomplete"}
classes={classes}
displayNoResult
defaultValue={defaultValue}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useState } from "react";
import Spinner from "../Spinner.svg";
import { css } from "@styled-system/css";
import { redirect } from "next/navigation";
import Link from "../Link";

export type AutocompleteProps<K> = InputProps & {
onChange?: (value: K | undefined) => void;
Expand All @@ -23,7 +24,6 @@ export type AutocompleteProps<K> = InputProps & {
};

export const Autocomplete = <K,>({
className,
onChange,
onSearch,
onError,
Expand All @@ -35,7 +35,6 @@ export const Autocomplete = <K,>({
state,
stateRelatedMessage,
hintText,
classes,
dataTestId,
displayNoResult,
defaultValue,
Expand Down Expand Up @@ -69,7 +68,7 @@ export const Autocomplete = <K,>({
});
return (
<>
<div className={`${autocompleteContainer} ${className}`}>
<div className={`${fr.cx("fr-input-group")}`}>
<Input
{...getInputProps({
id: "location-search",
Expand Down Expand Up @@ -143,7 +142,6 @@ export const Autocomplete = <K,>({
label={label}
state={state}
stateRelatedMessage={stateRelatedMessage}
classes={classes}
/>
<ul
{...getMenuProps()}
Expand All @@ -159,35 +157,23 @@ export const Autocomplete = <K,>({
index,
key: `${displayLabel(item)}${index}`,
})}
className={`${fr.cx("fr-sidemenu__item", "fr-p-0", "fr-grid-row")} ${autocompleteContainer}${highlightedIndex === index ? ` ${buttonActive}` : ""}`}
className={`${fr.cx("fr-p-3v")} ${suggestion} ${highlightedIndex === index ? isHighlighted : ""}`}
>
<Button
{...(lineAsLink
? {
linkProps: {
href: lineAsLink(item),
},
}
: {
linkProps: undefined,
})}
priority="tertiary no outline"
className={`${fr.cx("fr-col-12")} ${autocompleteButton}`}
>
{displayLabel(item)}
</Button>
{lineAsLink ? (
<Link href={lineAsLink(item)} className={link}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

j'ai transformé le bouton en Link directement pour que le style soit uniforme partout

{displayLabel(item)}
</Link>
) : (
<>{displayLabel(item)}</>
)}
</li>
</>
))
: displayNoResult &&
!selectedResult && (
<>
<li
className={`${fr.cx("fr-sidemenu__item", "fr-p-0", "fr-grid-row")} ${autocompleteContainer}`}
>
<span className={fr.cx("fr-py-1w", "fr-px-2w")}>
Aucun résultat
</span>
<li className={`${fr.cx("fr-p-3v")} }`}>
<>Aucun résultat</>
carolineBda marked this conversation as resolved.
Show resolved Hide resolved
</li>
</>
))}
Expand All @@ -197,23 +183,17 @@ export const Autocomplete = <K,>({
);
};

const autocompleteContainer = css({
position: "relative",
});

const autocompleteListContainer = css({
export const autocompleteListContainer = css({
position: "absolute",
w: "calc(100% - 1rem)",
zIndex: 100,
w: "100%",
zIndex: 10,
bg: "var(--background-default-grey)",
listStyleType: "none!",
});

const autocompleteButton = css({
textAlign: "left",
});

const buttonActive = css({
backgroundColor: "rgb(246, 246, 246)",
export const suggestion = css({
cursor: "pointer",
color: "var(--text-action-high-blue-france)",
});

const addonBlock = css({
Expand All @@ -225,10 +205,19 @@ const addonBlock = css({

const buttonClose = css({
_before: {
width: "18px !important",
height: "18px !important",
width: "18px!",
height: "18px!",
},
_hover: {
backgroundColor: "unset !important",
backgroundColor: "unset!",
},
});

export const isHighlighted = css({
bg: "var(--background-default-grey-hover)",
fontWeight: "bold",
});

const link = css({
backgroundImage: "none!",
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Alert from "@codegouvfr/react-dsfr/Alert";
import { getRouteBySource, SOURCES } from "@socialgouv/cdtn-utils";
import { useState } from "react";

import { Autocomplete } from "../../common/Autocomplete/Autocomplete";
import { Autocomplete } from "../../common/Autocomplete";
import { Agreement } from "../../../outils/types";
import { searchAgreement } from "../search";
import { useAgreementSearchTracking } from "../tracking";
Expand Down Expand Up @@ -56,50 +56,51 @@ export const AgreementSearchInput = ({ onSearch }: Props) => {
Précisez et sélectionnez votre convention collective
</h2>
<div className={fr.cx("fr-mt-2w")}>
<Autocomplete<Agreement>
dataTestId="AgreementSearchAutocomplete"
className={fr.cx("fr-col-12", "fr-mb-0")}
hintText="Ex : transport routier ou 1486"
label={
<>
Nom de la convention collective ou son numéro
d’identification IDCC (4 chiffres)
</>
}
state={getInputState()}
stateRelatedMessage={getStateMessage()}
displayLabel={(item) => {
return item ? `${item.shortTitle} (IDCC ${item.num})` : "";
}}
lineAsLink={(item) => {
return `/${getRouteBySource(SOURCES.CCN)}/${item.slug}`;
}}
onChange={(agreement) => {
if (agreement) {
emitSelectEvent(`idcc${agreement.id}`);
}
}}
search={searchAgreement}
onSearch={(query, agreements) => {
if (query) {
emitAgreementSearchInputEvent(query);
}
if (onSearch) onSearch(query, agreements);
if (!query) {
setSearchState("noSearch");
} else if (!agreements.length && query.length <= 2) {
setSearchState("lowSearch");
} else if (!agreements.length && query.length > 2) {
setSearchState("notFoundSearch");
} else {
setSearchState("fullSearch");
<div className={fr.cx("fr-col-12", "fr-mb-0")}>
<Autocomplete<Agreement>
dataTestId="AgreementSearchAutocomplete"
hintText="Ex : transport routier ou 1486"
label={
<>
Nom de la convention collective ou son numéro d’identification
IDCC (4 chiffres)
</>
}
}}
onError={(message) => {
setSearchState("errorSearch");
setError(message);
}}
/>
state={getInputState()}
stateRelatedMessage={getStateMessage()}
displayLabel={(item) => {
return item ? `${item.shortTitle} (IDCC ${item.num})` : "";
}}
lineAsLink={(item) => {
return `/${getRouteBySource(SOURCES.CCN)}/${item.slug}`;
}}
onChange={(agreement) => {
if (agreement) {
emitSelectEvent(`idcc${agreement.id}`);
}
}}
search={searchAgreement}
onSearch={(query, agreements) => {
if (query) {
emitAgreementSearchInputEvent(query);
}
if (onSearch) onSearch(query, agreements);
if (!query) {
setSearchState("noSearch");
} else if (!agreements.length && query.length <= 2) {
setSearchState("lowSearch");
} else if (!agreements.length && query.length > 2) {
setSearchState("notFoundSearch");
} else {
setSearchState("fullSearch");
}
}}
onError={(message) => {
setSearchState("errorSearch");
setError(message);
}}
/>
</div>
{searchState === "notFoundSearch" && (
<Alert
className={fr.cx("fr-mt-2w")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,20 @@ export const EnterpriseAgreementSearchInput = ({
}),
}}
/>
<LocationSearchInput
onLocationChange={setLocation}
defaultValue={location}
<div
className={fr.cx(
"fr-col-12",
"fr-col-md",
getStateMargin(),
"fr-mt-2w",
"fr-mt-md-0"
)}
/>
>
<LocationSearchInput
onLocationChange={setLocation}
defaultValue={location}
/>
</div>
<div
className={`${fr.cx("fr-col-xl", getStateMargin())} ${ButtonContainer}`}
>
Expand Down
Loading
Loading