mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-22 09:57:00 +01:00
Merge pull request #2379 from Maxime-J/ordered-filter-values
Sort filter values
This commit is contained in:
commit
a7cb0eaa20
@ -1,6 +1,6 @@
|
|||||||
import { useState } from 'react';
|
import { useState, useMemo } from 'react';
|
||||||
import { Form, FormRow, Item, Flexbox, Dropdown, Button } from 'react-basics';
|
import { Form, FormRow, Item, Flexbox, Dropdown, Button } from 'react-basics';
|
||||||
import { useMessages, useFilters, useFormat } from 'components/hooks';
|
import { useMessages, useFilters, useFormat, useLocale } from 'components/hooks';
|
||||||
import styles from './FieldFilterForm.module.css';
|
import styles from './FieldFilterForm.module.css';
|
||||||
|
|
||||||
export default function FieldFilterForm({
|
export default function FieldFilterForm({
|
||||||
@ -16,14 +16,30 @@ export default function FieldFilterForm({
|
|||||||
const [value, setValue] = useState();
|
const [value, setValue] = useState();
|
||||||
const { getFilters } = useFilters();
|
const { getFilters } = useFilters();
|
||||||
const { formatValue } = useFormat();
|
const { formatValue } = useFormat();
|
||||||
|
const { locale } = useLocale();
|
||||||
const filters = getFilters(type);
|
const filters = getFilters(type);
|
||||||
|
|
||||||
|
const formattedValues = useMemo(() => {
|
||||||
|
const formatted = {};
|
||||||
|
const format = val => {
|
||||||
|
formatted[val] = formatValue(val, name);
|
||||||
|
return formatted[val];
|
||||||
|
};
|
||||||
|
if (values.length !== 1) {
|
||||||
|
const { compare } = new Intl.Collator(locale, { numeric: true });
|
||||||
|
values.sort((a, b) => compare(formatted[a] ?? format(a), formatted[b] ?? format(b)));
|
||||||
|
} else {
|
||||||
|
format(values[0]);
|
||||||
|
}
|
||||||
|
return formatted;
|
||||||
|
}, [values]);
|
||||||
|
|
||||||
const renderFilterValue = value => {
|
const renderFilterValue = value => {
|
||||||
return filters.find(f => f.value === value)?.label;
|
return filters.find(f => f.value === value)?.label;
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderValue = value => {
|
const renderValue = value => {
|
||||||
return formatValue(value, name);
|
return formattedValues[value];
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAdd = () => {
|
const handleAdd = () => {
|
||||||
@ -59,7 +75,7 @@ export default function FieldFilterForm({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{value => {
|
{value => {
|
||||||
return <Item key={value}>{formatValue(value, name)}</Item>;
|
return <Item key={value}>{formattedValues[value]}</Item>;
|
||||||
}}
|
}}
|
||||||
</Dropdown>
|
</Dropdown>
|
||||||
</Flexbox>
|
</Flexbox>
|
||||||
|
Loading…
Reference in New Issue
Block a user