Hello everyone!
I am trying to implement single choice toggle. A bit of context
I have product edit page.
On product page, there is a tab with all media assigned to the product (repeater)
Each row has FileUpload (media itself)
and few toggles. One of them is "is_cover".
The idea behind it is that only one media can be selected as "Cover"
currently it's implemented as:
Toggle::make('is_cover')
->live()
->afterStateUpdated(function ($state, callable $set, callable $get) {
if ($state) {
// get all rows
$rows = $get('../../media') ?? [];
foreach ($rows as $index => $row) {
// turn off all other toggles
if ($row['is_cover'] ?? false) {
if (($row['id'] ?? null) != $get('id')) {
$set("../../media.{$index}.is_cover", false);
}
}
}
}
}),
That works. But it's painfully and unnecessary slow...
I was thinking to use radio, but radio can not be split in repeater. All options must be provided at once in one place. Therefore not suitable in my use - case 🙁
All my attempts to move logic on front-end failed.
I was able to subscribe for event via:
'x-init' and using $watch("state", value => { ... } (maybe there is more efficient way of doing that)
But after than, i was not able to receive items from repeater to toggle off existed records ...
Any documentation or tips, please?
Thanks!