#multiselect

15 messages · Page 1 of 1 (latest)

terse quartz
#

I am trying to include a Mantine MultiSelect in an react-hook-form controlled form but i struggle with the correct implication of the onCreate property. The MultiSelect should be creatable.

The code of the component is:

import type { SelectItem } from '@mantine/core'
import { MultiSelect } from '@mantine/core'
import { Controller, useFormContext } from 'react-hook-form'

export default function ManMultiSelect({
name = '',
label = '',
data = [''],
placeholder = '',
disabled = false,
searchable = false,
nothingFound = 'Leider nichts gefunden',
creatable = false,
}) {
const { control } = useFormContext()

return (
<Controller
name={name}
control={control}
render={({ field: { value, onChange } }) => (
<MultiSelect
placeholder={placeholder}
label={label}
value={value as string[]}
onChange={onChange}
data={data as (string | SelectItem)[]}
disabled={disabled}
clearable
creatable={creatable}
getCreateLabel={(query) => + Füge ${query} hinzu}
// onCreate={}
searchable={searchable}
nothingFound={nothingFound}
className="w-full"
/>
)}
/>
)
}

the component is called via:

<ManMultiSelect
name="trainings"
label="Fortbildungen der Einrichtung"
creatable={true}
searchable={true}
placeholder="Hier die erfolgreich absolvierten Fortbildungen eintragen"
data={facilityDetails?.trainings || []}
/>

How can i create a new value?

untold edge
#

You would need to add custom functionaity to your own wrapper component that handles adding it through onChange from Controller

terse quartz
#

when i try to add a value, in the devtools: control - _formvalues - trainigs

#

that value is not added

#

i triecd it again: the value is added after 2-3 seconds ???

#

i see, if a new value is created the form is not updated and hence does not show the value. When the submit is triggered and the value is written to the db on the next render the value is shown

#

can i trigger a rerender after a new vallue is created?

untold edge
terse quartz
#

i never did that before, i can try

untold edge
terse quartz
#

ts is complaining: Type '(query: string) => void' is not assignable to type '(query: string) => string | SelectItem | null | undefined'.
Type 'void' is not assignable to type 'string | SelectItem | null | undefined'.ts(2322)
MultiSelect.d.ts(40, 5): The expected type comes from property 'onCreate' which is declared here on type 'IntrinsicAttributes & MultiSelectProps & RefAttributes<HTMLInputElement>'

terse quartz
#

thanks for ur support. ist working 🙂