Skip to content

Commit f76ca77

Browse files
authored
Merge pull request #1353 from dev-lnk/td-fix
2 parents c7050fe + d7478d7 commit f76ca77

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/Fields/Td.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use Illuminate\Contracts\View\View;
99
use Illuminate\View\ComponentAttributeBag;
1010
use MoonShine\Components\FieldsGroup;
11+
use MoonShine\Contracts\Fields\FieldsWrapper;
1112
use MoonShine\Decorations\LineBreak;
1213
use Throwable;
1314

1415
/**
1516
* @method static static make(Closure|string $label, ?Closure $fields = null)
1617
*/
17-
class Td extends Template
18+
class Td extends Template implements FieldsWrapper
1819
{
1920
private ?Closure $conditionalFields = null;
2021

@@ -60,6 +61,10 @@ public function hasConditionalFields(): bool
6061

6162
public function getConditionalFields(): array
6263
{
64+
if(\is_null($this->getData())) {
65+
return [];
66+
}
67+
6368
return value($this->conditionalFields, $this->getData(), $this);
6469
}
6570

@@ -105,12 +110,10 @@ protected function resolvePreview(): string|View
105110
return parent::resolvePreview();
106111
}
107112

108-
$fields = $this->hasConditionalFields()
109-
? $this->getConditionalFields()
110-
: $this->getFields();
113+
$fields = $this->getFields();
111114

112115
return FieldsGroup::make(
113-
Fields::make($fields)
116+
$fields
114117
)
115118
->mapFields(fn (Field $field, int $index): Field => $field
116119
->resolveFill($this->toRawValue(withoutModify: true), $this->getData())
@@ -119,4 +122,11 @@ protected function resolvePreview(): string|View
119122
->forcePreview())
120123
->render();
121124
}
122-
}
125+
126+
public function getFields(): Fields
127+
{
128+
return $this->hasConditionalFields()
129+
? Fields::make($this->getConditionalFields())
130+
: Fields::make($this->getRawFields());
131+
}
132+
}

src/Http/Requests/Resources/UpdateColumnFormRequest.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MoonShine\Http\Requests\Resources;
66

7+
use MoonShine\Contracts\Fields\FieldsWrapper;
78
use MoonShine\Exceptions\ResourceException;
89
use MoonShine\Fields\Field;
910
use MoonShine\Http\Requests\MoonShineFormRequest;
@@ -41,9 +42,27 @@ public function authorize(): bool
4142
*/
4243
public function getField(): ?Field
4344
{
44-
return $this->getResource()
45-
?->getIndexFields()
46-
?->withoutWrappers()
45+
$resource = $this->getResource();
46+
47+
if (\is_null($resource)) {
48+
return null;
49+
}
50+
51+
$data = $resource->getItem();
52+
53+
if (\is_null($data)) {
54+
return null;
55+
}
56+
57+
$fields = $resource->getIndexFields();
58+
$fields->each(
59+
fn (Field $field): Field => $field instanceof FieldsWrapper
60+
? $field->resolveFill($data->toArray(), $data)
61+
: $field
62+
);
63+
64+
return $fields
65+
->withoutWrappers()
4766
?->findByColumn(
4867
request()->input('field')
4968
);

0 commit comments

Comments
 (0)