I'm trying to use multiselect dropdown for a certain list of objects in a form that is using formik and MUI. the array contains objects of type:
export interface IdTitlePair { id: number; title: string; }
declared field object like this:
const fields = { text: 'title', value: 'id' }
This is how I initialized the component
and this is what I see as result
What is it I'm doing wrong?
also how do I specify the selected items from the list of all items i.e. selected items should be displayed in chips and others should only appear in the dropdow list
Ok i was able to fix the issue I changed this line
so all items must be in string only. numbers or other types are not acceptable.
however the items are not select-able. they only appear in the dropdown. they are not select-able. I checked your examples and sample code. you do not set any extra properties in the multiselect dropdown.
what am I missing?
my data source is a list of objects. here are the details:
and this is the interface I use
rest of the code is already present in the question. there is no other manipulation being done in the code. Currenty I'm able to see the items in the list
but clicking on any item does not put them in the card form. I looked at your sample code and it is working flawlessly. Tried comparing it with my code to identify what's different that you did in demo. here is the complete code that I'm using. I have removed the unnecessary form items not relevant to the issue at hand
export function Editor(props: EditorPropsType) {
const { data } = props
const classes = useStyles()
const f = useTeamFormik(props.id??0, props.data, props.save, props.id)
const { initialErrors,
values, errors,
status, touched, isValid, isSubmitting, dirty,
handleSubmit, handleBlur, handleChange } = f
const fields = { text: 'title', value: 'id' }
return (
)
}
In the original code, value, onChange and onBlur are not commented out. but even if you comment them out, it makes no difference
this is the error I noticed in console
util.js:69 Uncaught TypeError: Cannot read properties of null (reading 'id')
at getValue (util.js:69:1)
at MultiSelectComponent.DropDownBase.getFormattedValue (drop-down-base.js:284:1)
at MultiSelectComponent.MultiSelect.updateListSelection (multi-select.js:2476:1)
at MultiSelectComponent.MultiSelect.onMouseClick (multi-select.js:2648:1)
getValue @ util.js:69
DropDownBase.getFormattedValue @ drop-down-base.js:284
MultiSelect.updateListSelection @ multi-select.js:2476
MultiSelect.onMouseClick @ multi-select.js:2648
every time I click any item, this error appears in console. here is the code i was able to browse to
I made the changes as per your sample code and now it does display the chips and removing or adding them. however as soon as focus is lost it crashes
I think the problem is with Formik and Multiselect onChange and onBlur. I was able to find two events select and remove. I'm not sure if formik needs to be managed manually in this case by using setFieldTouched(), setFieldError() etc
Finally i was able to solve the issues and made it work with Formik perfectly.
the problem was being that data source format was {id, title} while value was number[]
and I incorrectly set the value to be {id, title}[] which caused the crash in onChange and onBlur in formik.
Once the value was set to number[] everything worked fine.
extremely thankful for the help and support.
this issue may be closed now.