I have a form that display a Many-to-Many relationship between two entities Brand and Distribution. I display the distributions through a CheckboxGroupInput component in a brand page. During the form edition (Edit component), I managed to pre-check distributions that have been checked previously during the creation (Create) (this is not specified in the documentation) but I can't check or uncheck any distributions.
Here's the edition form :
export class BrandEdit extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: true
}
}
componentDidMount() {
//Find all distributions
restClient(GET_MANY, 'distribution', {
sort: {field: 'name', order: 'ASC'}
})
.then(response => {
return restClient(GET_ONE, 'brand', {
id: this.props.match.params.id
}).then(brandResult => {
let distributionsIds = []
brandResult.data.distributions.forEach(distribution => {
distributionsIds.push(distribution.id)
})
this.setState({
distributions: response.data,
distribution_checked_ids: distributionsIds,
isLoading: false,
})
});
})
}
render() {
const { isLoading, distributions, distribution_checked } = this.state
return (
<div>
{
<Edit {...this.props}>
<SimpleForm>
<DisabledInput source="id"/>
<TextInput label="Nom" source="name"/>
<CheckboxGroupInput
source="distributions"
input={{
value: this.state.distribution_checked_ids,
onChange: (checkedDistributionIds) => {
this.setState({ distribution_checked_ids: checkedDistributionIds });
}
}}
choices={distributions}
optionValue="id"
optionText="name"
/>
</SimpleForm>
</Edit>
}
</div>
);
}
}
Any ideas ?
Thanks
We need to pass an array of Distribution id to the component and not and array of Distribution objects.
Here's the component :
<CheckboxGroupInput
source="distributions"
choices={distributions}
optionValue="id"
optionText="name"
/>
Data should look like :
{
"id": 2115,
"name": "Test",
"distributions": [12, 13, 14]
}