-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add TextArrayInput to edit arrays of strings #10384
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3c0692e
Add TextArrayInput to edit arrays of strings
fzaninotto 2c821b2
Add unit tests
fzaninotto a84ae28
[doc] Mention the component in other select array components
fzaninotto d27dab2
Apply suggestions from code review
fzaninotto b9be33e
Review
fzaninotto 15dce74
Update examples/simple/src/data.tsx
slax57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| --- | ||
| layout: default | ||
| title: "The TextArrayInput Component" | ||
| --- | ||
|
|
||
| # `<TextArrayInput>` | ||
|
|
||
| `<TextArrayInput>` lets you edit an array of strings, like a list of email addresses or a list of tags. It renders as an input where the current values are represented as chips. Users can add or delete new values. | ||
|
|
||
| <video controls autoplay playsinline muted loop> | ||
| <source src="./img/TextArrayInput.mp4" type="video/mp4"/> | ||
| Your browser does not support the video tag. | ||
| </video> | ||
|
|
||
|
|
||
| ## Usage | ||
|
|
||
| Use `<TextArrayInput>` to edit an array of strings: | ||
|
|
||
| ```jsx | ||
| import { Create, SimpleForm, TextArrayInput, TextInput } from 'react-admin'; | ||
|
|
||
| export const EmailCreate = () => ( | ||
| <Create> | ||
| <SimpleForm> | ||
| <TextArrayInput source="to" /> | ||
| <TextInput source="subject" /> | ||
| <TextInput source="body" multiline minRows={5} /> | ||
| </SimpleForm> | ||
| </Create> | ||
| ); | ||
| ``` | ||
|
|
||
| This form will allow users to input multiple email addresses in the `to` field. The resulting email will look like this: | ||
|
|
||
| ```jsx | ||
| { | ||
| "to": ["[email protected]", "[email protected]"], | ||
| "subject": "Request for a quote", | ||
| "body": "Hi,\n\nI would like to know if you can provide a quote for the following items:\n\n- 100 units of product A\n- 50 units of product B\n- 25 units of product C\n\nBest regards,\n\nJulie\n", | ||
| "id": 123, | ||
| "date": "2024-11-26T11:37:22.564Z", | ||
| "from": "[email protected]", | ||
| } | ||
| ``` | ||
|
|
||
| `<TextArrayInput>` is designed for simple string arrays. For more complex use cases, consider the following alternatives: | ||
|
|
||
| - [`<SelectArrayInput>`](./SelectArrayInput.md) or [`<AutocompleteArrayInput>`](./AutocompleteArrayInput.md) if the possible values are limited to a predefined list. | ||
| - [`<ReferenceArrayInput>`](./ReferenceArrayInput.md) if the possible values are stored in another resource. | ||
| - [`<ArrayInput>`](./ArrayInput.md) if the stored value is an array of *objects* instead of an array of strings. | ||
|
|
||
| ## Props | ||
|
|
||
| | Prop | Required | Type | Default | Description | | ||
| | ------------ | -------- | --------- | ------- | -------------------------------------------------------------------- | | ||
| | `options` | Optional | `string[]` | | Optional list of possible values for the input. If provided, the input will suggest these values as the user types. | | ||
| | `renderTags` | Optional | `(value, getTagProps) => ReactNode` | | A function to render selected value. | | ||
|
|
||
| `<TextArrayInput>` also accepts the [common input props](./Inputs.md#common-input-props). | ||
|
|
||
| Additional props are passed down to the underlying Material UI [`<Autocomplete>`](https://mui.com/material-ui/react-autocomplete/) component. | ||
|
|
||
| ## `options` | ||
|
|
||
| You can make show a list of suggestions to the user by setting the `options` prop: | ||
|
|
||
| ```jsx | ||
| <TextArrayInput | ||
| source="to" | ||
| options={[ | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| '[email protected]', | ||
| ]} | ||
| /> | ||
| ``` | ||
|
|
||
| ## `renderTags` | ||
|
|
||
| To customize the rendering of the chips, use the `renderTags` prop. This prop is a function that takes two arguments: | ||
|
|
||
| - `value`: The input value (an array of strings) | ||
| - `getTagProps`: A props getter for an individual tag. | ||
|
|
||
| ```tsx | ||
| <TextArrayInput | ||
| source="to" | ||
| renderTags={(value: readonly string[], getTagProps) => | ||
| value.map((option: string, index: number) => { | ||
| const { key, ...tagProps } = getTagProps({ index }); | ||
| return ( | ||
| <Chip | ||
| variant="outlined" | ||
| label={option} | ||
| key={key} | ||
| {...tagProps} | ||
| /> | ||
| ); | ||
| }) | ||
| } | ||
| /> | ||
| ``` |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/ra-ui-materialui/src/input/TextArrayInput.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import * as React from 'react'; | ||
| import { render, fireEvent, screen } from '@testing-library/react'; | ||
|
|
||
| import { Basic, HelperText, Label, Required } from './TextArrayInput.stories'; | ||
|
|
||
| describe('<TextArrayInput />', () => { | ||
| it('should render the values as chips', () => { | ||
| render(<Basic />); | ||
| const chip1 = screen.getByText('[email protected]'); | ||
| expect(chip1.classList.contains('MuiChip-label')).toBe(true); | ||
| const chip2 = screen.getByText('[email protected]'); | ||
| expect(chip2.classList.contains('MuiChip-label')).toBe(true); | ||
| }); | ||
| it('should allow to remove a value', async () => { | ||
| render(<Basic />); | ||
| await screen.findByText( | ||
| '["[email protected]","[email protected]"] (object)' | ||
| ); | ||
| const deleteButtons = screen.getAllByTestId('CancelIcon'); | ||
| fireEvent.click(deleteButtons[0]); | ||
| await screen.findByText('["[email protected]"] (object)'); | ||
| }); | ||
| it('should allow to remove all values one by one', async () => { | ||
| render(<Basic />); | ||
| await screen.findByText( | ||
| '["[email protected]","[email protected]"] (object)' | ||
| ); | ||
| const deleteButtons = screen.getAllByTestId('CancelIcon'); | ||
| fireEvent.click(deleteButtons[1]); | ||
| fireEvent.click(deleteButtons[0]); | ||
| await screen.findByText('[] (object)'); | ||
| }); | ||
| it('should allow to remove all values using the reset button', async () => { | ||
| render(<Basic />); | ||
| const input = screen.getByLabelText('resources.emails.fields.to'); | ||
| fireEvent.click(input); | ||
| const clearButton = screen.getByLabelText('Clear'); | ||
| fireEvent.click(clearButton); | ||
| await screen.findByText('[] (object)'); | ||
| }); | ||
| it('should allow to add a value', async () => { | ||
| render(<Basic />); | ||
| const input = screen.getByLabelText('resources.emails.fields.to'); | ||
| fireEvent.change(input, { target: { value: '[email protected]' } }); | ||
| fireEvent.keyDown(input, { key: 'Enter' }); | ||
| await screen.findByText( | ||
| '["[email protected]","[email protected]","[email protected]"] (object)' | ||
| ); | ||
| }); | ||
| it('should render the helper text', () => { | ||
| render(<HelperText />); | ||
| screen.getByText('Email addresses of the recipients'); | ||
| }); | ||
| it('should render the custom label', () => { | ||
| render(<Label />); | ||
| screen.getByText('To'); | ||
| }); | ||
| it('should show required fields as required', () => { | ||
| render(<Required />); | ||
| expect(screen.getAllByText('*').length).toBe(2); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.