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

Add ability to define filters as an array of Inputs #6368

Merged
merged 6 commits into from Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 7 additions & 15 deletions docs/Authentication.md
Expand Up @@ -995,25 +995,17 @@ export const UserEdit = ({ permissions, ...props }) =>
```
{% endraw %}

What about the `List` view, the `Datagrid`, `SimpleList` and `Filter` components? It works there, too. And in the next example, the `permissions` prop is passed down to a custom `filters` component.
What about the `List` view, the `Datagrid`, `SimpleList`? It works there, too. And in the next example, the `permissions` prop is passed down to a custom `filters` selector.

```jsx
const UserFilter = ({ permissions, ...props }) =>
<Filter {...props}>
<TextInput
label="user.list.search"
source="q"
alwaysOn
/>
<TextInput source="name" />
{permissions === 'admin' && <TextInput source="role" />}
</Filter>;
const getUserFilters = (permissions) => ([
<TextInput label="user.list.search" source="q" alwaysOn />,
<TextInput source="name" />,
permissions === 'admin' ? <TextInput source="role" /> : null,
].filter(filter => filter !== null)));

export const UserList = ({ permissions, ...props }) =>
<List
{...props}
filters={props => <UserFilter permissions={permissions} {...props} />}
>
<List {...props} filters={getUserFilters(permissions)}>
<Datagrid>
<TextField source="id" />
<TextField source="name" />
Expand Down
2 changes: 1 addition & 1 deletion docs/Buttons.md
Expand Up @@ -257,7 +257,7 @@ export const PostList = (props) => (

### `<FilterButton>`

This button is an internal component used by react-admin in [the `<Filter>` button/form combo](./List.md#the-filter-buttonform-combo).
This button is an internal component used by react-admin in [the Filter button/form combo](./List.md#the-filter-buttonform-combo).

![List Filters](./img/list_filter.gif)

Expand Down
20 changes: 9 additions & 11 deletions docs/Inputs.md
Expand Up @@ -5,7 +5,7 @@ title: "Input Components"

# Input Components

An `Input` component displays an input, or a dropdown list, a list of radio buttons, etc. Such components allow to edit a record property, and are common in the `<Edit>`, `<Create>`, and `<Filter>` views.
An `Input` component displays an input, or a dropdown list, a list of radio buttons, etc. Such components allow to edit a record property, and are common in the `<Edit>`, `<Create>` component, and in the List Filters.
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved

```jsx
// in src/posts.js
Expand Down Expand Up @@ -534,7 +534,7 @@ import { AutocompleteInput } from 'react-admin';
| `optionText` | Optional | `string` &#124; `Function` &#124; `Component` | `name` | Field name of record to display in the suggestion item or function which accepts the correct record as argument (`(record)=> {string}`) |
| `optionValue` | Optional | `string` | `id` | Field name of record containing the value to use as input value |
| `inputText` | Optional | `Function` | `-` | If `optionText` is a custom Component, this function is needed to determine the text displayed for the current selection. |
| `resettable` | Optional | `boolean` | `false` | Display a button to reset the text filter. Useful when using `<AutocompleteInput>` inside `<Filter>` |
| `resettable` | Optional | `boolean` | `false` | Display a button to reset the text filter. Useful when using `<AutocompleteInput>` inside the list filters |
| `setFilter` | Optional | `Function` | `null` | A callback to inform the `searchText` has changed and new `choices` can be retrieved based on this `searchText`. Signature `searchText => void`. This function is automatically setup when using `ReferenceInput`. |
| `shouldRenderSuggestions` | Optional | `Function` | `() => true` | A function that returns a `boolean` to determine whether or not suggestions are rendered. Use this when working with large collections of data to improve performance and user experience. This function is passed into the underlying react-autosuggest component. Ex.`(value) => value.trim() > 2` |
| `suggestionLimit` | Optional | `number` | `null` | Limits the numbers of suggestions that are shown in the dropdown list |
Expand Down Expand Up @@ -1930,7 +1930,7 @@ import { ReferenceArrayInput, SelectArrayInput } from 'react-admin';
</ReferenceArrayInput>
```

**Tip**: `allowEmpty` is set by default for all Input components children of the `<Filter>` component
**Tip**: `allowEmpty` is set by default for all Input components passed as `<List filters>`.

You can tweak how this component fetches the possible values using the `perPage`, `sort`, and `filter` props.

Expand Down Expand Up @@ -2045,16 +2045,14 @@ import { ReferenceInput, SelectInput } from 'react-admin';
</ReferenceInput>
```

**Tip**: `allowEmpty` is set by default for all Input components children of the `<Filter>` component:
**Tip**: `allowEmpty` is set by default for all Input components passed as `<List filters>`:

```jsx
const CommentFilter = (props) => (
<Filter {...props}>
<ReferenceInput label="Post" source="post_id" reference="posts"> // no need for allowEmpty
<SelectInput optionText="title" />
</ReferenceInput>
</Filter>
);
const commentFilters = [
<ReferenceInput label="Post" source="post_id" reference="posts"> // no need for allowEmpty
<SelectInput optionText="title" />
</ReferenceInput>
];
```

You can tweak how this component fetches the possible values using the `perPage`, `sort`, and `filter` props.
Expand Down