Skip to content

Commit 2e3dfa1

Browse files
authored
Merge pull request #6034 from marmelab/FilterListItem-element-label
Add ability to use an element as label in `<FilterListItem>`
2 parents 048d211 + 2948dce commit 2e3dfa1

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

packages/ra-ui-materialui/src/list/filter/FilterListItem.spec.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ import FilterListItem from './FilterListItem';
88
describe('<FilterListItem/>', () => {
99
afterEach(cleanup);
1010

11+
it("should display the item label when it's a string", () => {
12+
const { queryByText } = render(
13+
<ListContextProvider value={{ hideFilter: true }}>
14+
<FilterListItem label="Foo" value={{ foo: 'bar' }} />
15+
</ListContextProvider>
16+
);
17+
expect(queryByText('Foo')).not.toBeNull();
18+
});
19+
20+
it("should display the item label when it's an element", () => {
21+
const { queryByTestId } = render(
22+
<ListContextProvider value={{ hideFilter: true }}>
23+
<FilterListItem
24+
label={<span data-testid="123">Foo</span>}
25+
value={{ foo: 'bar' }}
26+
/>
27+
</ListContextProvider>
28+
);
29+
expect(queryByTestId('123')).not.toBeNull();
30+
});
31+
1132
it('should not appear selected if filterValues is empty', () => {
1233
const { getByText } = render(
1334
<ListContextProvider value={{ hideFilter: true }}>

packages/ra-ui-materialui/src/list/filter/FilterListItem.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { FC, memo } from 'react';
2+
import { memo, isValidElement, ReactElement } from 'react';
33
import {
44
IconButton,
55
ListItem,
@@ -27,9 +27,10 @@ const useStyles = makeStyles(theme => ({
2727
*
2828
* Expects 2 props:
2929
*
30-
* - label: The text to be displayed for this item. Will be translated.
30+
* - label: The text (or React element) to be displayed for this item.
31+
* If it's a string, the component will translate it.
3132
* - value: An object to be merged into the filter value when enabling the filter
32-
* (e.g. { is_published: true, published_at_gte: '2020-07-08' })
33+
* (e.g. { is_published: true, published_at_gte: '2020-07-08' })
3334
*
3435
* @example
3536
*
@@ -143,7 +144,10 @@ const useStyles = makeStyles(theme => ({
143144
* </Card>
144145
* );
145146
*/
146-
const FilterListItem: FC<{ label: string; value: any }> = props => {
147+
const FilterListItem = (props: {
148+
label: string | ReactElement;
149+
value: any;
150+
}) => {
147151
const { label, value } = props;
148152
const { filterValues, setFilters } = useListFilterContext();
149153
const translate = useTranslate();
@@ -180,7 +184,11 @@ const FilterListItem: FC<{ label: string; value: any }> = props => {
180184
className={classes.listItem}
181185
>
182186
<ListItemText
183-
primary={translate(label, { _: label })}
187+
primary={
188+
isValidElement(label)
189+
? label
190+
: translate(label, { _: label })
191+
}
184192
className={classes.listItemText}
185193
data-selected={isSelected ? 'true' : 'false'}
186194
/>

0 commit comments

Comments
 (0)