Skip to content

Commit 951515a

Browse files
Merge pull request #112 from sebastienheyd/feat/button-tooltip-custom
feat(datatables): add tooltip support and custom button helper
2 parents 5c21945 + fd4d103 commit 951515a

File tree

11 files changed

+247
-6
lines changed

11 files changed

+247
-6
lines changed

docs/docs/8.x/datatables/button.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Button::add()
2020
| [class](#class) | Sets additional class |
2121
| [route](#route) | Sets the button link href by using a route |
2222
| [link](#link) | Sets the button link href |
23+
| [tooltip](#tooltip) | Sets a tooltip for the button |
2324
| [attributes](#attributes) | Sets HTML attributes to the button |
2425
| [make](#make) | Renders the button |
2526

@@ -93,6 +94,27 @@ Sets the button link href.
9394
->link(route('boilerplate.users.edit', $user->id))
9495
```
9596

97+
## tooltip
98+
99+
Sets a tooltip for the button using the HTML `title` attribute.
100+
101+
```php
102+
->tooltip('Edit this user')
103+
```
104+
105+
The tooltip is only rendered when a non-empty string is provided, avoiding unnecessary HTML attributes.
106+
107+
**Example with tooltip:**
108+
109+
```php
110+
Button::add('Edit')
111+
->route('boilerplate.users.edit', $user->id)
112+
->icon('pencil-alt')
113+
->color('primary')
114+
->tooltip('Edit this user')
115+
->make();
116+
```
117+
96118
## attributes
97119

98120
Sets HTML attributes.
@@ -113,6 +135,8 @@ Renders the button.
113135

114136
## Button aliases
115137

138+
### Predefined buttons
139+
116140
```php
117141
Button::show('route.to.resource.show', $resource);
118142
```
@@ -126,3 +150,33 @@ Button::delete('route.to.resource.destroy', $resource);
126150
```
127151

128152
> `Button::delete` will show a modal to confirm the deletion. You can set another confirmation message by using the [`Datatable::locale()` method](options#locale).
153+
154+
### Custom button helper
155+
156+
The `custom()` method provides a convenient way to create custom buttons with all parameters in a single call:
157+
158+
```php
159+
Button::custom(
160+
'route.name', // Route name (required)
161+
$args, // Route arguments (optional, default: [])
162+
'icon-name', // FontAwesome icon (optional, default: '')
163+
'Tooltip text', // Tooltip text (optional, default: '')
164+
'primary', // Button color (optional, default: 'default')
165+
['data-action' => 'x'] // HTML attributes (optional, default: [])
166+
);
167+
```
168+
169+
**Example:**
170+
171+
```php
172+
Button::custom(
173+
'users.export',
174+
['id' => $user->id],
175+
'download',
176+
'Export user data',
177+
'success',
178+
['data-confirm' => 'Export this user?']
179+
);
180+
```
181+
182+
**Note:** The icon parameter comes before the tooltip for better ergonomics. If no icon is provided, no empty HTML markup is generated.

src/Datatables/Button.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Button
1010
protected string $icon = '';
1111
protected string $label = '';
1212
protected array $attributes = [];
13+
protected string $tooltip = '';
1314

1415
/**
1516
* Instanciate a new button.
@@ -32,6 +33,28 @@ public static function add(string $label = ''): self
3233
return new static($label);
3334
}
3435

36+
/**
37+
* Returns a custom button.
38+
*
39+
* @param string $route
40+
* @param mixed $args
41+
* @param string $icon
42+
* @param string $tooltip
43+
* @param string $color
44+
* @param array $attributes
45+
* @return string
46+
*/
47+
public static function custom(string $route, mixed $args = [], string $icon = '', string $tooltip = '', string $color = 'default', array $attributes = []): string
48+
{
49+
$button = self::add()->route($route, $args)->tooltip($tooltip);
50+
51+
if (! empty($icon)) {
52+
$button->icon($icon);
53+
}
54+
55+
return $button->color($color)->attributes($attributes)->make();
56+
}
57+
3558
/**
3659
* Returns an edit button.
3760
*
@@ -41,7 +64,7 @@ public static function add(string $label = ''): self
4164
*/
4265
public static function show(string $route, $args = []): string
4366
{
44-
return self::add()->attributes(['data-action' => 'dt-show-element'])->route($route, $args)->icon('eye')->make();
67+
return self::add()->attributes(['data-action' => 'dt-show-element'])->route($route, $args)->tooltip(__('boilerplate::datatable.show'))->icon('eye')->make();
4568
}
4669

4770
/**
@@ -53,7 +76,7 @@ public static function show(string $route, $args = []): string
5376
*/
5477
public static function edit(string $route, $args = []): string
5578
{
56-
return self::add()->attributes(['data-action' => 'dt-edit-element'])->route($route, $args)->color('primary')->icon('pencil-alt')->make();
79+
return self::add()->attributes(['data-action' => 'dt-edit-element'])->route($route, $args)->tooltip(__('boilerplate::datatable.edit'))->color('primary')->icon('pencil-alt')->make();
5780
}
5881

5982
/**
@@ -68,6 +91,7 @@ public static function delete(string $route, $args = []): string
6891
return self::add()
6992
->route($route, $args)
7093
->attributes(['data-action' => 'dt-delete-element'])
94+
->tooltip(__('boilerplate::datatable.delete'))
7195
->color('danger')
7296
->icon('trash')
7397
->make();
@@ -161,15 +185,26 @@ public function link(string $href): self
161185
return $this;
162186
}
163187

188+
/**
189+
* Sets tooltip of button.
190+
*
191+
* @param string $tooltip
192+
* @return $this
193+
*/
194+
public function tooltip(string $tooltip): self
195+
{
196+
$this->tooltip = $tooltip;
197+
198+
return $this;
199+
}
200+
164201
/**
165202
* Renders the button.
166203
*
167204
* @return string
168205
*/
169206
public function make(): string
170207
{
171-
$str = '<a href="%s" class="btn btn-sm btn-%s ml-1%s" %s>%s%s</a>';
172-
173208
if (! empty($this->label) && ! empty($this->icon)) {
174209
$this->label = $this->label.' ';
175210
}
@@ -182,6 +217,9 @@ public function make(): string
182217
return sprintf('%s="%s"', $k, $this->attributes[$k]);
183218
}, array_keys($this->attributes)));
184219

185-
return sprintf($str, $this->href, $this->color, $this->class, $attributes, $this->label, $this->icon);
220+
$tooltip = ! empty($this->tooltip) ? sprintf(' data-toggle="tooltip" title="%s"', e($this->tooltip)) : '';
221+
$str = '<a href="%s"%s class="btn btn-sm btn-%s ml-1%s" %s>%s%s</a>';
222+
223+
return sprintf($str, $this->href, $tooltip, $this->color, $this->class, $attributes, $this->label, $this->icon);
186224
}
187225
}

src/resources/lang/bg/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'Да изтриете този елемент?',
55
'deleteSuccess' => 'Позицията е успешно изтрита',
6+
'show' => 'Преглед',
7+
'edit' => 'Редактиране',
8+
'delete' => 'Изтриване',
69
];

src/resources/lang/en/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'Delete this item?',
55
'deleteSuccess' => 'Item successfully deleted',
6+
'show' => 'Show',
7+
'edit' => 'Edit',
8+
'delete' => 'Delete',
69
];

src/resources/lang/es/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => '¿Borrar este elemento?',
55
'deleteSuccess' => 'Elemento eliminado con éxito',
6+
'show' => 'Ver',
7+
'edit' => 'Editar',
8+
'delete' => 'Eliminar',
69
];

src/resources/lang/fa/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'این مورد حذف شود؟',
55
'deleteSuccess' => 'مورد با موفقیت حذف شد',
6+
'show' => 'نمایش',
7+
'edit' => 'ویرایش',
8+
'delete' => 'حذف',
69
];

src/resources/lang/fr/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'Supprimer cet élément ?',
55
'deleteSuccess' => "L'élément a été supprimé",
6+
'show' => 'Voir',
7+
'edit' => 'Éditer',
8+
'delete' => 'Supprimer',
69
];

src/resources/lang/it/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'Cancellare questo elemento?',
55
'deleteSuccess' => 'Elemento eliminato con successo',
6+
'show' => 'Mostra',
7+
'edit' => 'Modifica',
8+
'delete' => 'Elimina',
69
];

src/resources/lang/tr/datatable.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
return [
44
'deleteConfirm' => 'Bu öğe silinsin mi?',
55
'deleteSuccess' => 'Öğe başarıyla silindi',
6+
'show' => 'Göster',
7+
'edit' => 'Düzenle',
8+
'delete' => 'Sil',
69
];

tests/Controllers/DatatablesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ public function testUsersDatatables()
4848
$data = $resource->getOriginalContent()['data'][0];
4949

5050
$this->assertEquals($user->email, $data['email']);
51-
$this->assertEquals('<a href="/admin/users/1/edit" class="btn btn-sm btn-primary ml-1" data-action="dt-edit-element"><i class="fas fa-fw fa-pencil-alt"></i></a>', $data['dt-actions']);
51+
$this->assertEquals('<a href="/admin/users/1/edit" data-toggle="tooltip" title="Edit" class="btn btn-sm btn-primary ml-1" data-action="dt-edit-element"><i class="fas fa-fw fa-pencil-alt"></i></a>', $data['dt-actions']);
5252
}
5353
}

0 commit comments

Comments
 (0)