Skip to content

Commit efad0fb

Browse files
authored
Merge pull request #8011 from marmelab/recordrepresentation
Add default Record representation
2 parents d4a31aa + 59a4a2a commit efad0fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1818
-779
lines changed

cypress/integration/create.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('Create Page', () => {
9090
value: 'Annamarie Mayer',
9191
},
9292
]);
93-
cy.get('[role="option"]').trigger('click');
93+
cy.get('[role="option"]:first').trigger('click');
9494
cy.get(CreatePage.elements.input('authors.0.role')).should(
9595
el => expect(el).to.exist
9696
);

docs/AutocompleteInput.md

+30-24
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ import { AutocompleteInput } from 'react-admin';
2222
]} />
2323
```
2424

25+
**Tip**: If you want to populate the `choices` attribute with a list of related records, you should decorate `<AutocompleteInput>` with [`<ReferenceInput>`](./ReferenceInput.md), and leave the `choices` empty:
26+
27+
```jsx
28+
import { AutocompleteInput, ReferenceInput } from 'react-admin';
29+
30+
<ReferenceInput label="Post" source="post_id" reference="posts">
31+
<AutocompleteInput />
32+
</ReferenceInput>
33+
```
34+
35+
**Tip**: `<AutocompleteInput>` is a stateless component, so it only allows to *filter* the list of choices, not to *extend* it. If you need to populate the list of choices based on the result from a `fetch` call (and if [`<ReferenceInput>`](./ReferenceInput.md) doesn't cover your need), you'll have to [write your own Input component](./Inputs.md#writing-your-own-input-component) based on MUI `<AutoComplete>` component.
36+
2537
## Properties
2638

2739
| Prop | Required | Type | Default | Description |
2840
|---------------------------|----------|-----------------------------------------------|-----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
29-
| `choices` | Required | `Object[]` | `-` | List of items to autosuggest |
41+
| `choices` | Optional | `Object[]` | `-` | List of items to autosuggest. Required if not inside a ReferenceInput. |
3042
| `create` | Optional | `Element` | `-` | A React Element to render when users want to create a new choice |
3143
| `createItemLabel` | Optional | `string` | `ra.action.create_item` | The label for the menu item allowing users to create a new choice. Used when the filter is not empty |
3244
| `emptyValue` | Optional | `any` | `''` | The value to use for the empty element |
@@ -37,13 +49,13 @@ import { AutocompleteInput } from 'react-admin';
3749
| `optionValue` | Optional | `string` | `id` | Field name of record containing the value to use as input value |
3850
| `inputText` | Optional | `Function` | `-` | Required if `optionText` is a custom Component, this function must return the text displayed for the current selection. |
3951
| `filterToQuery` | Optional | `string` => `Object` | `searchText => ({ q: [searchText] })` | How to transform the searchText into a parameter for the data provider |
40-
| `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`. |
52+
| `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 set up when using `ReferenceInput`. |
4153
| `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().length > 2` |
4254
| `suggestionLimit` | Optional | `number` | `null` | Limits the numbers of suggestions that are shown in the dropdown list |
4355

4456
`<AutocompleteInput>` also accepts the [common input props](./Inputs.md#common-input-props).
4557

46-
## Usage
58+
## `optionText`
4759

4860
You can customize the properties to use for the option name and value, thanks to the `optionText` and `optionValue` attributes:
4961

@@ -66,7 +78,7 @@ const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
6678
<AutocompleteInput source="author_id" choices={choices} optionText={optionRenderer} />
6779
```
6880

69-
`optionText` also accepts a custom Component. However, as the underlying Autocomplete component requires that the current selection is a string, if you opt for a Component, you must pass a function as the `inputText` prop. This function should return text representation of the current selection:
81+
`optionText` also accepts a custom Component. However, as the underlying Autocomplete component requires that the current selection is a string, if you opt for a Component, you must pass a function as the `inputText` prop. This function should return a text representation of the current selection:
7082

7183
```jsx
7284
const choices = [
@@ -96,6 +108,8 @@ const matchSuggestion = (filter, choice) => {
96108
/>
97109
```
98110

111+
## `translateChoice`
112+
99113
The choices are translated by default, so you can use translation identifiers as choices:
100114

101115
```jsx
@@ -112,9 +126,17 @@ In that case, set the `translateChoice` prop to `false`.
112126
<AutocompleteInput source="gender" choices={choices} translateChoice={false}/>
113127
```
114128

115-
When dealing with a large amount of `choices` you may need to limit the number of suggestions that are rendered in order to maintain usable performance. The `shouldRenderSuggestions` is an optional prop that allows you to set conditions on when to render suggestions. An easy way to improve performance would be to skip rendering until the user has entered 2 or 3 characters in the search box. This lowers the result set significantly, and might be all you need (depending on your data set).
129+
## `shouldRenderSuggestions`
130+
131+
When dealing with a large amount of `choices` you may need to limit the number of suggestions that are rendered in order to maintain usable performance. The `shouldRenderSuggestions` is an optional prop that allows you to set conditions on when to render suggestions. An easy way to improve performance would be to skip rendering until the user has entered 2 or 3 characters in the search box. This lowers the result set significantly and might be all you need (depending on your data set).
116132
Ex. `<AutocompleteInput shouldRenderSuggestions={(val) => { return val.trim().length > 2 }} />` would not render any suggestions until the 3rd character has been entered. This prop is passed to the underlying `react-autosuggest` component and is documented [here](https://github.com/moroshko/react-autosuggest#should-render-suggestions-prop).
117133

134+
## `sx`: CSS API
135+
136+
This component doesn't apply any custom styles on top of [MUI `<Autocomplete>` component](https://mui.com/components/autocomplete/). Refer to their documentation to know its CSS API.
137+
138+
## Additional Props
139+
118140
`<AutocompleteInput>` renders a [MUI `<Autocomplete>` component](https://mui.com/components/autocomplete/) and it accepts the `<Autocomplete>` props:
119141

120142
{% raw %}
@@ -123,18 +145,6 @@ Ex. `<AutocompleteInput shouldRenderSuggestions={(val) => { return val.trim().le
123145
```
124146
{% endraw %}
125147

126-
**Tip**: If you want to populate the `choices` attribute with a list of related records, you should decorate `<AutocompleteInput>` with [`<ReferenceInput>`](./ReferenceInput.md), and leave the `choices` empty:
127-
128-
```jsx
129-
import { AutocompleteInput, ReferenceInput } from 'react-admin';
130-
131-
<ReferenceInput label="Post" source="post_id" reference="posts">
132-
<AutocompleteInput optionText="title" />
133-
</ReferenceInput>
134-
```
135-
136-
**Tip**: `<AutocompleteInput>` is a stateless component, so it only allows to *filter* the list of choices, not to *extend* it. If you need to populate the list of choices based on the result from a `fetch` call (and if [`<ReferenceInput>`](./ReferenceInput.md) doesn't cover your need), you'll have to [write your own Input component](./Inputs.md#writing-your-own-input-component) based on MUI `<AutoComplete>` component.
137-
138148
## Creating New Choices
139149

140150
The `<AutocompleteInput>` can allow users to create a new choice if either the `create` or `onCreate` prop is provided.
@@ -171,7 +181,7 @@ const PostCreate = () => {
171181
```
172182
{% endraw %}
173183

174-
Use the `create` prop when you want a more polished or complex UI. For example a MUI `<Dialog>` asking for multiple fields because the choices are from a referenced resource.
184+
Use the `create` prop when you want a more polished or complex UI. For example an MUI `<Dialog>` asking for multiple fields because the choices are from a referenced resource.
175185

176186
{% raw %}
177187
```js
@@ -253,9 +263,9 @@ const CreateCategory = () => {
253263
```
254264
{% endraw %}
255265

256-
**Tip:** As showcased in this example, react-admin provides a convenience hook for accessing the filter the user has already input in the `<AutocompleteInput>`: `useCreateSuggestionContext`.
266+
**Tip:** As showcased in this example, react-admin provides a convenient hook for accessing the filter the user has already input in the `<AutocompleteInput>`: `useCreateSuggestionContext`.
257267

258-
The `Create %{item}` option will only be displayed once the user has already set a filter (by typing in some input). If you expect your users to create new items often, you can make this more user friendly by adding a placeholder text like this:
268+
The `Create %{item}` option will only be displayed once the user has already set a filter (by typing in some input). If you expect your users to create new items often, you can make this more user-friendly by adding a placeholder text like this:
259269

260270
{% raw %}
261271
```diff
@@ -287,7 +297,3 @@ const PostCreate = () => {
287297
}
288298
```
289299
{% endraw %}
290-
291-
## `sx`: CSS API
292-
293-
This component doesn't apply any custom styles on top of [MUI `<Autocomplete>` component](https://mui.com/components/autocomplete/). Refer to their documentation to know its CSS API.

0 commit comments

Comments
 (0)