Skip to content

Commit 9537581

Browse files
committed
Added an inline editing component
1 parent 3b523e2 commit 9537581

File tree

6 files changed

+85
-40
lines changed

6 files changed

+85
-40
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

3-
## [2.2.3] - 2021-06-26
3+
## [2.4.0] - Not released
4+
5+
### Added
6+
- Inline editing component
7+
## [2.3.0] - 2021-06-26
48
### Added
59
- Default value for filters
610
- Sortable list and grid views

doc/table-view.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ This view creates a dynamic data table with some features like filters, paginati
2626
- [Showing feedback messages](#showing-feedback-messages)
2727
- [Hiding actions](#hiding-actions)
2828
- [Confirmation message](#confirmation-message)
29+
- [Inline editing](#inline-editing)
2930
- [Showing UI components](#showing-ui-components)
3031
- [Avatar](#avatar)
3132
- [Badges](#badges)
33+
- [Icons](#icons)
3234

3335
## Table View example
3436

@@ -412,6 +414,53 @@ public function getConfirmationMessage($item = null)
412414
}
413415
```
414416

417+
# Inline editing
418+
It is possible to edit a field on the data table inline using an `editable` component instead of plain text in the `row` method.
419+
420+
```php
421+
use LaravelViews\Facades\UI;
422+
423+
public function row(User $user)
424+
{
425+
return [
426+
// ...Other fields
427+
UI::editable($user, 'email'),
428+
];
429+
}
430+
```
431+
432+
This component will show the current value and if if you clic on it, you will be able to edit it. To update the new value you should define a new method on the table view.
433+
434+
```php
435+
/**
436+
* Method fired by the `editable` component, it
437+
* gets the model instance and a key value array
438+
* with the modified
439+
*/
440+
public function update(User $user, $data)
441+
{
442+
}
443+
```
444+
445+
The `update` method gets the model instance, and a key-value array with the modified data so you can save it directly on the model.
446+
447+
```php
448+
use LaravelViews\Views\Traits\WithAlerts;
449+
450+
/**
451+
* Method fired by the `editable` component, it
452+
* gets the model instance and a key-value array
453+
* with the modified
454+
*/
455+
public function update(User $user, $data)
456+
{
457+
$user->update($data);
458+
$this->success();
459+
}
460+
```
461+
462+
Notice that you can fire a success or error alert as the actions with the `WithAlerts` trait.
463+
415464
# Showing UI components
416465
You can display some UI components instead of plain text like avatars, badges or icons, some of these components has different variants, you can customize these varians with the `laravel-views.php` config file.
417466

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
{{-- components.editable
22
3-
Render an editable input field
3+
Render an editable input field --}}
44

5-
--}}
6-
7-
<div
8-
x-data="{
9-
field: '{{ $field }}',
10-
id: {{ $model->id }},
11-
value: {{ json_encode($model->$field) }},
12-
original: {{ json_encode($model->$field) }},
13-
editing: false
14-
}"
15-
@click.away="editing = false; value = original;"
16-
>
17-
<input
18-
x-cloak
19-
x-ref="input"
20-
x-show="editing"
21-
x-model="value"
22-
@keydown.enter="
23-
$wire.update({
24-
id: id,
25-
value: value,
26-
field: field
27-
}); editing = false;"
28-
@keydown.escape="editing = false; value = original;"
29-
class="bg-green-100 text-black-700 border border-green-200 rounded py-2 px-4 leading-tight outline-none w-full"
30-
>
31-
<div
32-
x-show="!editing"
33-
@click="editing = true; $nextTick(() => {$refs.input.focus();$refs.input.setSelectionRange(-1, -1);});"
34-
x-html="value"
35-
class='transition-all duration-300 ease-in-out inline-block cursor-pointer border-b-2 border-dotted border-gray-400 hover:bg-gray-100 p-1'>
36-
{!! $model->$field !!}
37-
</div>
5+
<div x-data="{
6+
field: '{{ $field }}',
7+
id: {{ $model->id }},
8+
value: {{ json_encode($model->$field) }},
9+
original: {{ json_encode($model->$field) }},
10+
editing: false
11+
}"
12+
@click.away="editing = false; value = original;">
13+
<input x-cloak
14+
x-ref="input"
15+
x-show="editing"
16+
x-model="value"
17+
@keydown.enter="$wire.update(id, {
18+
[field]: value
19+
}); editing = false;"
20+
@keydown.escape="editing = false; value = original;"
21+
class="block appearance-none w-full bg-white border-gray-300 hover:border-gray-500 px-2 py-1 rounded focus:outline-none focus:bg-white focus:border-blue-600 focus:border-2 border">
22+
<div x-show="!editing"
23+
@click="editing = true; $nextTick(() => {$refs.input.focus()})"
24+
x-html="value"
25+
class='transition-all duration-300 ease-in-out px-2 py-1 rounded cursor-pointer focus:outline-none hover:bg-white hover:border-gray-500 border border-transparent'>
26+
{!! $model->$field !!}
27+
</div>
3828

3929
</div>

resources/views/list-view/list-item.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</div>
88
<div class="flex-1">
99
<div class="text-sm font-medium text-gray-900">
10-
{{ $title }}
10+
{!! $title !!}
1111
</div>
1212
<div class="text-sm">
13-
{{ $subtitle }}
13+
{!! $subtitle !!}
1414
</div>
1515
</div>
1616
<x-lv-actions :actions="$actions" :model="$model" />

src/UI/UI.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class UI
88
{
99
public function editable($model, $field)
1010
{
11-
return view('laravel-views::components.editable', [
11+
return $this->component('laravel-views::components.editable', [
1212
'model' => $model,
1313
'field' => $field
14-
])->render();
14+
]);
1515
}
1616

1717
public function badge($title, $type = 'default')

src/Views/Traits/WithAlerts.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace LaravelViews\Views\Traits;
44

5+
use LaravelViews\Views\View;
6+
57
trait WithAlerts
68
{
79
public function success($message = null)
@@ -16,7 +18,7 @@ public function error($message = null)
1618

1719
private function setMessage($type = 'success', $message = null)
1820
{
19-
$view = $this->view ? $this->view : $view;
21+
$view = $this->view && $this->view instanceof View ? $this->view : $this;
2022

2123
$messages = [
2224
'success' => __('Action was executed successfully'),

0 commit comments

Comments
 (0)