Hello! I have been stumped on this for a while. I have a large use-form component and I have a Select that hits a prisma api to get its data. Everything is fine, and onChange works updating the intended nested field. However after clicking a selection, the text clears. passing it a value from the form also does not work. Is there a way to keep that value as the search value and also set that value after onChange? Thanks in advance!
#Use-Form Select Value
29 messages · Page 1 of 1 (latest)
const [search, setSearch] = useState<string>('');
const { data, status, error } = trpc.useQuery(['exercise.searchExercises', { name: search }], {
enabled: search.length > 2,
});
const selectResults = data
? data.map((r) => ({
value: r.id,
label: r.name,
}))
: [];
return (
<>
<Select
placeholder="Select Exercise"
searchable
clearable
allowDeselect
data={selectResults}
maxDropdownHeight={200}
onSearchChange={(query: string) => {
setSearch(query);
}}
onChange={(e) => {
const selected = data?.find((exercise: Exercise) => exercise.id === e);
form.setFieldValue(`blocks.${bi}.weeks.${wi}.days.${di}.exercises.${ei}`, {
name: selected?.name,
...rest of big object thingy
},
],
});
}}
/>
</>
);```
tldr: how to get the Select text to match either the search value or the onChange selected value without resetting
The Select's "value" prop needs to be set to one of the "value" of your data objects.
Here you get the name of the selected entry in the onChange, but their value are the ids.
So on entry selection, you need to save the selected id to the form, and use that as value for the Select.
i think they're trying to implement some form of search, where the controlled select value changes, but whatever they type in the search's text input remains and does not change when the select changes
select does not provide this functionality out the box, the text state is not exposed as props
there are possibly two options here if that's the case
- use refs to modify the text contents of the select during onChange to preserve the text state and have it not be overridden by the select value change
- (more involved) combine TextInput with Popover to create custom search component
https://mantine.dev/core/popover/
I'm pretty sure using the correct value in the controlled Select would be sufficient here. The Select's content would reflect the selected entry, which seems to be the goal.
If indeed the goal is to leave the user's search query completely untouched upon selection, then a TextInput/Menu combo is the correct way to go.
Using Menu instead of Popover allows better out of the box Select-like behavior, with selectable entries and the like
i want the selected name to be the value
ill try both options, thanks for the responses
but do you want the text to be unaltered when selecting an option?
If your names are unique, you can use them as value directly in your data set instead of the id.
Otherwise, you need to sync the id to your form and use that as value
menu is a good idea too
if the option is valid ( which it always will be) , i want that options value as the select value, and also update the value in the form.values
in that case, is there anything preventing the use of {...form.getInputProps(yourFieldNameHere)}?
rather than using value and onChange
or do you need specific validation logic
not gonna lie i did not know that prop works with select
it works with all components in mantine
does that take care of onSearchChange aswell?
no, it is for controlling select's value
it passes value and onChange, and a few other props (like error etc) for you
No, the search input is ephemeral and only used internally by Select, the prop is only there to allow further control if needed.
The search value has no reason to be stored in a form, so getInputProps won't touch it.
ok , passing the custom onChange after the {...form.getInputProps} works. you guys are briliant. thank you very much!
i actually think i do need to keep track of search state for validation purposes. if the seach state deviates from the selected value, then I need to mark that field as not a proper selection. So i will try the uesRef approach
also , would you guys know a reason why when the form is taller than its container, focusing on a select scrolls to the top of the page? very odd behavior
you can probably achieve that with a validate handler