I am trying to integrate a custom select component (tried with Downshift/React-Select/Mantine with Redwood to use as custom multiselect component instead of native select
.
I have a problem with sending the data from the form and it’s a very similar question to one posted here:
Sadly it does not fix all my issues but at least NOT using separate controller helped. Code below is adapted (naming - Garden) to keep same context as code posted in the link above.
The issues I have are:
- When I try to submit data the ‘plants’ from here are sent as array of objects instead of array of ID’s (as I think it should be sent)
- If I change the
onChange
method to return ID’s thereact-select
breaks with undefined values (astitle
andid
fields do not exist - In either case the GraphQL schema for my update mutation is not expecting that type to be present on the update input and throws an error.
So here’s a question:
A. what can I do to make GraphQL aware that when I update main object (say Garden) I want to assign multiple Plants to it? Since the schema is generated I should pick up changes I made to EditGardenCell
or am I missing something?
B. Assuming I will need to send only IDs to the GraphQL endpoint (like with connect) how can I do this without breaking react-select
?
input UpdateGardenInput {
description: String
title: String
}
import Select from 'react-select';
import { Controller } from '@redwoodjs/forms';
// use this as data for the Select field
// const options = [
// { id: 'chocolate', title: 'Chocolate' },
// { id: 'strawberry', title: 'Strawberry' },
// { id: 'vanilla', title: 'Vanilla' },
// ];
const SelectMultipleChoice = ({
name = 'plants',
options = [],
defaultValues,
control,
}) => {
return (
<Controller
name={name}
render={({ field }) => {
return (
<Select
{...field}
options={options}
defaultValue={defaultValues}
getOptionValue={(option) => option?.id}
getOptionLabel={(option) => option?.title}
isMulti
onChange={(val) => {
field.onChange(val);
}}
// this will return the IDs to GraphQL (but it will break react-select) with name of form input - here `plants`
// onChange={(val) => {
// return field.onChange(
// val.map((element) => {
// return element.id;
// })
// );
// }}
/>
);
}}
/>
);
};
export default SelectMultipleChoice;
BTW. it all works fine if I use SelectField with multiple option but it’s unusable when selecting elements from larger sets.