Tricks

Quickly fill in the form field (form hints, random passwords, clear input, format values)

Oct 10, 2022
studiowizjo
Admin panel, Form builder

Sometimes, for frequently used forms, it is convenient to have a selection of some of the most commonly used options on hand.

An effect similar to the following:

Form hints

...can be achieved as follows:

DatePicker::make('remind_at')
->hint(function ($component) {
$hint = '<div class="flex flex-wrap space-x-2">';
 
$dates = [
[
'label' => 'Today',
'value' => now()->format('Y-m-d'),
'class' => 'bg-primary-500',
],
[
'label' => 'Tomorrow',
'value' => now()->addDay()->format('Y-m-d'),
'class' => 'bg-primary-700',
],
[
'label' => 'In a month',
'value' => now()->addMonth()->format('Y-m-d'),
'class' => 'bg-orange-600',
],
[
'label' => 'In a year',
'value' => now()->addYear()->format('Y-m-d'),
'class' => 'bg-red-600',
],
];
 
foreach ($dates as $date) {
$hint .= '
<span wire:click="$set(\''.$component->getStatePath().'\', \'' . $date['value'] . '\')" class="font-medium h- px-2 py-0.5 rounded-xl ' . $date['class'] . ' text-white text-xs tracking-tight mt-[10px] cursor-pointer">
' . $date['label'] . '
</span>
';
}
 
$hint .= '</div>';
 
return new HtmlString($hint);
}),

In case of field which accepts multiple values the second argument for $set should be passed as array, eg.:

Select::make('users')
->label('Task for')
->relationship('users', 'full_name')
->multiple()
->hint(function ($component) {
$hint = '<div class="flex flex-wrap space-x-2">';
 
$users = [
[
'label' => 'John',
'value' => User::JOHN,
'class' => 'bg-primary-500',
],
[
'label' => 'Alice',
'value' => User::ALICE,
'class' => 'bg-sky-500',
],
[
'label' => 'Team JA',
'value' => implode(', ', [User::JOHN, User::ALICE]),
'class' => 'bg-violet-700',
],
];
 
foreach ($users as $user) {
$hint .= '
<span wire:click="$set(\''.$component->getStatePath().'\', [' . $user['value'] . '])" class="font-medium h- px-2 py-0.5 rounded-xl ' . $user['class'] . ' text-white text-xs tracking-tight mt-[10px] cursor-pointer">
' . $user['label'] . '
</span>
';
}
 
$hint .= '</div>';
 
return new HtmlString($hint);
})

it can be used also for generating random passwords:

TextInput::make('password')
->hint(function () {
return new HtmlString('
<div class="flex flex-wrap space-x-2">
<span wire:click="$set(\'data.password\', \'' . Str::random(10) . '\')" class="font-medium h- px-2 py-0.5 rounded-xl bg-primary-500 text-white text-xs tracking-tight mt-[10px] cursor-pointer">
Random
</span>
</div>
');
})
->maxLength(50)
->columnSpan(['default' => 2, 'sm' => 1]),

...clearing values:

TextInput::make('comment')
->hint(function ($component) {
return new HtmlString('
<div class="space-x-2">
<span wire:click="$set(\''.$component->getStatePath().'\', null)" class="font-medium h- px-2 py-0.5 rounded-xl bg-primary-500 text-white text-xs tracking-tight mt-[10px] cursor-pointer">
Clear
</span>
</div>
');
})
->maxLength(50)
->columnSpan(['default' => 2, 'sm' => 1]),

...formatting, copying, selecting and so on.

No comments yet…