Skip to content

Commit 0c6bfc7

Browse files
committed
Feature filters color helper
1 parent a75e9a2 commit 0c6bfc7

File tree

2 files changed

+117
-16
lines changed

2 files changed

+117
-16
lines changed

stories/ColorHelper/ColorHelper.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useConst } from "powerhooks/useConst";
1111
import { Evt } from "evt";
1212
import { useStyles } from "./makeStyles";
1313
import { ColorDecisionCard } from "./ColorDecisionCard";
14+
import type { Props as SearchProps } from "./Search";
1415

1516
const { useDebounce } = createUseDebounce({ "delay": 400 });
1617

@@ -40,8 +41,12 @@ export function ColorHelper() {
4041
colorDecisionAndCorrespondingOption
4142
);
4243

44+
const [context, setContext] = useState<SearchProps["context"]>(undefined);
45+
const [color, setColor] = useState<SearchProps["color"]>(undefined);
46+
const [usage, setUsage] = useState<SearchProps["usage"]>(undefined);
47+
4348
useDebounce({
44-
"query": search,
49+
"query": search + (context ?? "") + (color ?? "") + (usage ?? ""),
4550
"onDebounced": () =>
4651
setFilteredColorDecisionAndCorrespondingOption(
4752
fzf
@@ -50,6 +55,15 @@ export function ColorHelper() {
5055
({ item: colorDecisionAndCorrespondingOption }) =>
5156
colorDecisionAndCorrespondingOption
5257
)
58+
.filter(({ parsedColorDecisionName }) =>
59+
context === undefined ? true : parsedColorDecisionName.context === context
60+
)
61+
.filter(({ parsedColorDecisionName }) =>
62+
color === undefined ? true : parsedColorDecisionName.colorName === color
63+
)
64+
.filter(({ parsedColorDecisionName }) =>
65+
usage === undefined ? true : parsedColorDecisionName.usage === usage
66+
)
5367
)
5468
});
5569

@@ -76,15 +90,20 @@ export function ColorHelper() {
7690
belong to the DSFR palette you can use the filter to find to witch decision it
7791
correspond.
7892
</CallOut>
79-
8093
<Search
8194
evtAction={evtSearchAction}
8295
onSearchChange={search => setSearch(search)}
8396
search={search}
97+
context={context}
98+
onContextChange={setContext}
99+
color={color}
100+
onColorChange={setColor}
101+
usage={usage}
102+
onUsageChange={setUsage}
84103
/>
85104
<h3 style={{ "marginTop": fr.spacing("6v") }}>
86105
{search === ""
87-
? `${colorDecisionAndCorrespondingOption.length} color decisions`
106+
? `${filteredColorDecisionAndCorrespondingOption.length} color decisions`
88107
: `Found ${filteredColorDecisionAndCorrespondingOption.length} decisions matching your query`}
89108
</h3>
90109
{filteredColorDecisionAndCorrespondingOption.map(

stories/ColorHelper/Search.tsx

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,57 @@ import { Button } from "../../dist/Button";
55
import { fr } from "../../dist/fr";
66
import { NonPostableEvt } from "evt";
77
import { useEvt } from "evt/hooks";
8+
import { Select } from "../../dist/Select";
9+
import { colorDecisionAndCorrespondingOption } from "../../dist/fr/generatedFromCss/colorDecisionAndCorrespondingOptions";
810

9-
type Props = {
11+
const colors = Array.from(
12+
new Set(
13+
colorDecisionAndCorrespondingOption.map(
14+
({ parsedColorDecisionName }) => parsedColorDecisionName.colorName
15+
)
16+
)
17+
);
18+
const contextes = Array.from(
19+
new Set(
20+
colorDecisionAndCorrespondingOption.map(
21+
({ parsedColorDecisionName }) => parsedColorDecisionName.context
22+
)
23+
)
24+
);
25+
const usages = Array.from(
26+
new Set(
27+
colorDecisionAndCorrespondingOption.map(
28+
({ parsedColorDecisionName }) => parsedColorDecisionName.usage
29+
)
30+
)
31+
);
32+
33+
export type Props = {
1034
className?: string;
1135
search: string;
1236
onSearchChange: (search: string) => void;
1337
evtAction: NonPostableEvt<"scroll to">;
38+
context: typeof contextes[number] | undefined;
39+
onContextChange: (context: typeof contextes[number] | undefined) => void;
40+
color: typeof colors[number] | undefined;
41+
onColorChange: (color: typeof colors[number] | undefined) => void;
42+
usage: typeof usages[number] | undefined;
43+
onUsageChange: (usage: typeof usages[number] | undefined) => void;
1444
};
1545

1646
export function Search(props: Props) {
17-
const { className, search, onSearchChange, evtAction } = props;
47+
const {
48+
className,
49+
search,
50+
onSearchChange,
51+
evtAction,
52+
context,
53+
onContextChange,
54+
color,
55+
onColorChange,
56+
usage,
57+
onUsageChange
58+
} = props;
1859

1960
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null);
2061
const [searchBarWrapperElement, setSearchBarWrapperElement] = useState<HTMLDivElement | null>(
@@ -62,7 +103,6 @@ export function Search(props: Props) {
62103
"onChange": event => onSearchChange(event.target.value)
63104
}}
64105
/>
65-
66106
<Button
67107
className={classes.filterButton}
68108
iconId={areFiltersOpen ? "ri-arrow-down-s-fill" : "ri-arrow-up-s-fill"}
@@ -78,13 +118,48 @@ export function Search(props: Props) {
78118
}
79119
className={classes.filtersWrapper}
80120
>
81-
{/*
82-
<p>Ok</p>
83-
<p>Ok</p>
84-
<p>Ok</p>
85-
<p>Ok</p>
86-
<p>Ok</p>
87-
*/}
121+
<Select
122+
label="Filter by context"
123+
nativeSelectProps={{
124+
"onChange": event =>
125+
onContextChange(event.target.value || (undefined as any)),
126+
"defaultValue": context ?? ""
127+
}}
128+
>
129+
{[undefined, ...contextes].map(context => (
130+
<option value={context ?? ""} key={context ?? 0}>
131+
{context ?? "No no context selected..."}
132+
</option>
133+
))}
134+
</Select>
135+
<Select
136+
label="Filter by color name"
137+
nativeSelectProps={{
138+
"onChange": event =>
139+
onColorChange(event.target.value || (undefined as any)),
140+
"defaultValue": color ?? ""
141+
}}
142+
>
143+
{[undefined, ...colors].map(color => (
144+
<option value={color ?? ""} key={color ?? 0}>
145+
{color ?? "No no color selected..."}
146+
</option>
147+
))}
148+
</Select>
149+
<Select
150+
label="Filter by usage"
151+
nativeSelectProps={{
152+
"onChange": event =>
153+
onUsageChange(event.target.value || (undefined as any)),
154+
"defaultValue": usage ?? ""
155+
}}
156+
>
157+
{[undefined, ...usages].map(usage => (
158+
<option value={usage ?? ""} key={usage ?? 0}>
159+
{usage ?? "No usage selected..."}
160+
</option>
161+
))}
162+
</Select>
88163
</div>
89164
</>
90165
);
@@ -105,13 +180,20 @@ const useStyles = makeStyles<{ filterWrapperMaxHeight: number }>({ "name": { Sea
105180
"backgroundColor": theme.decisions.background.actionLow.blueFrance.hover
106181
},
107182
"color": theme.decisions.text.actionHigh.blueFrance.default,
108-
"marginLeft": fr.spacing("4v"),
109-
"display": "none"
183+
"marginLeft": fr.spacing("4v")
110184
},
111185
"filtersWrapper": {
112186
"transition": "max-height 0.2s ease-out",
113187
"maxHeight": filterWrapperMaxHeight,
114-
"overflow": "hidden"
188+
"overflow": "hidden",
189+
"display": "flex",
190+
"marginTop": fr.spacing("3v"),
191+
"& > *": {
192+
"flex": 1,
193+
...fr.spacing("padding", {
194+
"rightLeft": "4v"
195+
})
196+
}
115197
}
116198
})
117199
);

0 commit comments

Comments
 (0)