Skip to content

Commit 1a50d82

Browse files
committed
cleanup
1 parent 7a51738 commit 1a50d82

4 files changed

Lines changed: 60 additions & 34 deletions

File tree

demo/app/Sharp/Posts/PostForm.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929
use Code16\Sharp\Form\Layout\FormLayoutTab;
3030
use Code16\Sharp\Form\SharpForm;
3131
use Code16\Sharp\Utils\Fields\FieldsContainer;
32-
use Illuminate\Support\Arr;
3332
use Illuminate\Support\Facades\Blade;
3433
use Illuminate\Support\Facades\Storage;
35-
use Illuminate\Support\Str;
3634

3735
class PostForm extends SharpForm
3836
{
@@ -160,17 +158,16 @@ public function buildFormFields(FieldsContainer $formFields): void
160158
->addItemField(
161159
SharpFormHtmlField::make('document_infos')
162160
->setLiveRefresh(linkedFields: ['document'])
163-
->setTemplate(function (array $data, string $fieldKey) {
164-
$itemData = Arr::get($data, Str::beforeLast($fieldKey, '.'));
165-
if (! isset($itemData['document']['file_name'])) {
161+
->setTemplate(function (array $data) {
162+
if (! isset($data['document']['file_name'])) {
166163
return '';
167164
}
168165

169166
return sprintf(
170167
'File uploaded at : %s',
171168
Carbon::createFromTimestamp(
172-
Storage::disk($itemData['document']['disk'])
173-
->lastModified($itemData['document']['file_name'])
169+
Storage::disk($data['document']['disk'])
170+
->lastModified($data['document']['file_name'])
174171
)
175172
);
176173
})

demo/app/Sharp/TestForm/TestForm.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Code16\Sharp\Form\Layout\FormLayoutTab;
2626
use Code16\Sharp\Form\SharpSingleForm;
2727
use Code16\Sharp\Utils\Fields\FieldsContainer;
28+
use Illuminate\Database\Eloquent\Builder;
2829

2930
class TestForm extends SharpSingleForm
3031
{
@@ -161,7 +162,16 @@ public function buildFormFields(FieldsContainer $formFields): void
161162
->setListItemTemplate('{{ $name }}')
162163
->setResultItemTemplate('{{ $name }} ({{ $id }})')
163164
->setRemoteCallback(function ($search, $data) {
164-
dd($data);
165+
$users = User::orderBy('name');
166+
167+
foreach (explode(' ', trim($search)) as $word) {
168+
$users->where(function (Builder $query) use ($word) {
169+
$query->orWhere('name', 'like', "%$word%")
170+
->orWhere('email', 'like', "%$word%");
171+
});
172+
}
173+
174+
return $users->limit(10)->get();
165175
}, linkedFields: ['select']),
166176
)
167177
->addItemField(SharpFormEditorField::make('markdown2')
@@ -170,6 +180,18 @@ public function buildFormFields(FieldsContainer $formFields): void
170180
->setToolbar([
171181
SharpFormEditorField::B, SharpFormEditorField::I, SharpFormEditorField::A,
172182
]),
183+
)
184+
->addItemField(
185+
SharpFormHtmlField::make('document_infos')
186+
->setLiveRefresh(linkedFields: ['select'])
187+
->setTemplate(function (array $data) {
188+
return isset($data['select'])
189+
? sprintf(
190+
'You have selected : %s',
191+
$this->options()[$data['select']]
192+
)
193+
: '';
194+
})
173195
),
174196
)
175197
->addField(

src/Form/Fields/SharpFormHtmlField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SharpFormHtmlField extends SharpFormField
1111
{
1212
const FIELD_TYPE = 'html';
1313

14-
/** @var View|Closure(array, string)|string */
14+
/** @var View|Closure(array)|string */
1515
private View|Closure|string $template;
1616

1717
private bool $liveRefresh = false;
@@ -42,12 +42,12 @@ public function setTemplate(View|Closure|string $template): self
4242
return $this;
4343
}
4444

45-
public function render(array $data, string $fieldKey): string
45+
public function render(array $data): string
4646
{
4747
if ($this->template instanceof Closure) {
48-
$view = ($this->template)($data, $fieldKey);
48+
$view = ($this->template)($data);
4949

50-
return $view instanceof View ? $view->render() : $view;
50+
return $view instanceof View ? $view->with($data)->render() : $view;
5151
}
5252

5353
if (is_string($this->template)) {

src/Utils/Fields/HandleFormHtmlFields.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,46 @@ final public function formatHtmlFields(array $frontData, bool $keepOnlyHtmlField
3636
if ($field instanceof SharpFormHtmlField) {
3737
return [
3838
$key => $field->render([
39-
'fieldKey' => $key,
4039
...$formattedData,
4140
...(is_array($value) ? $value : []),
42-
], $key),
41+
]),
4342
];
4443
}
4544

46-
if ($field instanceof SharpFormListField && $field->itemFields()->whereInstanceOf(SharpFormHtmlField::class)->isNotEmpty()) {
45+
if ($field instanceof SharpFormListField
46+
&& $field->itemFields()->whereInstanceOf(SharpFormHtmlField::class)->isNotEmpty()
47+
) {
4748
return [
48-
$key => collect($value)->map(function ($item, $index) use ($field, $key, $formattedData, $keepOnlyHtmlFields) {
49-
return collect($item)->mapWithKeys(function ($itemValue, $itemKey) use ($field, $key, $index, $formattedData, $keepOnlyHtmlFields) {
50-
$itemField = $field->findItemFormFieldByKey($itemKey);
51-
52-
if ($itemField instanceof SharpFormHtmlField) {
53-
$fieldKey = "$key.$index.$itemKey";
54-
55-
return [
56-
$itemKey => $itemField->render([
57-
'fieldKey' => $fieldKey,
58-
...$formattedData,
59-
...(is_array($itemValue) ? $itemValue : []),
60-
], $fieldKey),
61-
];
62-
}
63-
64-
return $keepOnlyHtmlFields ? [] : [$itemKey => $itemValue];
65-
})->all();
66-
})->all(),
49+
$key => $this->formatListHtmlFields($field, $value, $formattedData, $keepOnlyHtmlFields),
6750
];
6851
}
6952

7053
return $keepOnlyHtmlFields ? [] : [$key => $value];
7154
})
7255
->all();
7356
}
57+
58+
private function formatListHtmlFields(
59+
SharpFormListField $field,
60+
array $listValue,
61+
array $formattedData,
62+
bool $keepOnlyHtmlFields
63+
): array {
64+
return collect($listValue)->map(fn ($item, $index) => collect($item)->mapWithKeys(
65+
function ($itemFieldValue, $itemFieldKey) use ($field, $index, $formattedData, $keepOnlyHtmlFields) {
66+
$itemField = $field->findItemFormFieldByKey($itemFieldKey);
67+
68+
if ($itemField instanceof SharpFormHtmlField) {
69+
return [
70+
$itemFieldKey => $itemField->render([
71+
...($formattedData[$field->key][$index] ?? []),
72+
...(is_array($itemFieldValue) ? $itemFieldValue : []),
73+
]),
74+
];
75+
}
76+
77+
return $keepOnlyHtmlFields ? [] : [$itemFieldKey => $itemFieldValue];
78+
})->all()
79+
)->all();
80+
}
7481
}

0 commit comments

Comments
 (0)