-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
[ui-materialui]: Hide <AutoCompleteInput>
"Create" option for empty input
#10228
Changes from all commits
7382c27
85e5088
5648e80
eb0cecf
68bce53
60cbc3b
4514f75
58b724d
0b9d66e
3462379
48eb05a
e61fe08
60630d1
b7fc372
50f24e3
b0a80ed
05b94d9
c81d1be
ddf3549
13342c6
10a1fc3
6d4983b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,33 +357,42 @@ export const CreateLabel = () => ( | |
<Wrapper> | ||
<AutocompleteInput | ||
source="author" | ||
choices={[ | ||
{ id: 1, name: 'Leo Tolstoy' }, | ||
{ id: 2, name: 'Victor Hugo' }, | ||
{ id: 3, name: 'William Shakespeare' }, | ||
{ id: 4, name: 'Charles Baudelaire' }, | ||
{ id: 5, name: 'Marcel Proust' }, | ||
]} | ||
choices={choicesForCreationSupport} | ||
onCreate={filter => { | ||
const newAuthorName = window.prompt( | ||
'Enter a new author', | ||
filter | ||
); | ||
if (!filter) return; | ||
|
||
if (newAuthorName) { | ||
const newAuthor = { | ||
id: choicesForCreationSupport.length + 1, | ||
name: newAuthorName, | ||
}; | ||
choicesForCreationSupport.push(newAuthor); | ||
return newAuthor; | ||
} | ||
const newOption = { | ||
id: choicesForCreationSupport.length + 1, | ||
name: filter, | ||
}; | ||
choicesForCreationSupport.push(newOption); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've had issues while testing stories like this one, or OnCreate, which I think are due to the fact that running In the following recording, the first item I create ('new') does not appear immediately, nor does it appear in the list. But when I create a second item ('new2'), then, suddenly, both items appear in the list. Capture.video.2024-10-08.18.16.39.mp4 |
||
return newOption; | ||
}} | ||
createLabel="Start typing to create a new item" | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
export const CreateItemLabel = () => ( | ||
<Wrapper> | ||
<AutocompleteInput | ||
source="author" | ||
choices={choicesForCreationSupport} | ||
onCreate={filter => { | ||
if (!filter) return; | ||
|
||
const newOption = { | ||
id: choicesForCreationSupport.length + 1, | ||
name: filter, | ||
}; | ||
choicesForCreationSupport.push(newOption); | ||
return newOption; | ||
}} | ||
createItemLabel="Add a new author: %{item}" | ||
/> | ||
</Wrapper> | ||
); | ||
|
||
const authorsWithFirstAndLastName = [ | ||
{ id: 1, first_name: 'Leo', last_name: 'Tolstoy', language: 'Russian' }, | ||
{ id: 2, first_name: 'Victor', last_name: 'Hugo', language: 'French' }, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,7 +133,7 @@ export const AutocompleteInput = < | |
closeText = 'ra.action.close', | ||
create, | ||
createLabel, | ||
createItemLabel, | ||
createItemLabel = 'ra.action.create_item', | ||
createValue, | ||
debounce: debounceDelay = 250, | ||
defaultValue, | ||
|
@@ -465,7 +465,13 @@ If you provided a React element for the optionText prop, you must also provide t | |
event?.type === 'change' || | ||
!doesQueryMatchSelection(newInputValue) | ||
) { | ||
setFilterValue(newInputValue); | ||
const createOptionLabel = translate(createItemLabel, { | ||
item: filterValue, | ||
_: createItemLabel, | ||
}); | ||
const isCreate = newInputValue === createOptionLabel; | ||
const valueToSet = isCreate ? filterValue : newInputValue; | ||
setFilterValue(valueToSet); | ||
debouncedSetFilter(newInputValue); | ||
} | ||
if (reason === 'clear') { | ||
|
@@ -513,7 +519,7 @@ If you provided a React element for the optionText prop, you must also provide t | |
// add create option if necessary | ||
const { inputValue } = params; | ||
if (onCreate || create) { | ||
if (inputValue === '') { | ||
if (inputValue === '' && createLabel) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part of the docs should be updated to reflect this change: https://marmelab.com/react-admin/AutocompleteInput.html#createlabel |
||
// create option with createLabel | ||
filteredOptions = filteredOptions.concat(getCreateItem('')); | ||
} else if (!doesQueryMatchSuggestion(filterValue)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the prompt from this story (which is a good thing IMO because it was redundant with the ability to fill in the filter), reveals a pre-existing odd behavior: if I click on the "Start typing to create a new item" label, then the value is filled with "Start typing to create a new item", which is probably not what I want. This special entry in the list should not be clickable IMO.
But this can be addressed in another PR since it was already existing.
Capture.video.2024-10-08.18.21.45.mp4