generating a unique id for each builder block
I'm a little stuck on this, trying to have a hidden field (ideally) that generates a UUID, which is fine when I create a new field but not when I clone using the builder clone action
TextInput::make('styling_id')
->default(function (
Get $get,
Set $set,
?string $state,
) {
if (($state ?? '') == ($get('../../styling_id') ?? '')) {
$state = Str::uuid()
->toString();
$set('styling_id', $state);
return $state;
}
}),
TextInput::make('styling_id')
->default(function (
Get $get,
Set $set,
?string $state,
) {
if (($state ?? '') == ($get('../../styling_id') ?? '')) {
$state = Str::uuid()
->toString();
$set('styling_id', $state);
return $state;
}
}),
Solution:Jump to solution
Ended up customising the clone action:
```
->cloneAction(
fn(Action $action) => $action->action(function (
array $arguments,...
1 Reply
Solution
Ended up customising the clone action:
->cloneAction(
fn(Action $action) => $action->action(function (
array $arguments,
BuilderComponent $component
): void {
$newUuid = $component->generateUuid();
$items = $component->getState();
$items[$newUuid] = $items[$arguments['item']];
$items[$newUuid]['data']['form_styling_id'] = Str::uuid()
->toString();
$component->state($items);
$component->collapsed(false,
shouldMakeComponentCollapsible: false);
$component->callAfterStateUpdated();
})
)
->cloneAction(
fn(Action $action) => $action->action(function (
array $arguments,
BuilderComponent $component
): void {
$newUuid = $component->generateUuid();
$items = $component->getState();
$items[$newUuid] = $items[$arguments['item']];
$items[$newUuid]['data']['form_styling_id'] = Str::uuid()
->toString();
$component->state($items);
$component->collapsed(false,
shouldMakeComponentCollapsible: false);
$component->callAfterStateUpdated();
})
)