F
Filament7mo ago
lmtc

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:
Ended up customising the clone action: ``` ->cloneAction( fn(Action $action) => $action->action(function ( array $arguments,...
Jump to solution
1 Reply
Solution
lmtc
lmtc7mo ago
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();
})
)
Want results from more Discord servers?
Add your server