Skip to content

Commit 2aa879c

Browse files
committed
ad
d docs
1 parent 24208e7 commit 2aa879c

3 files changed

Lines changed: 35 additions & 27 deletions

File tree

demo/app/Sharp/Posts/PostForm.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use App\Sharp\Utils\Embeds\CodeEmbed;
1010
use App\Sharp\Utils\Embeds\RelatedPostEmbed;
1111
use App\Sharp\Utils\Embeds\TableOfContentsEmbed;
12-
use Carbon\Carbon;
1312
use Code16\Sharp\Form\Eloquent\Uploads\Transformers\SharpUploadModelFormAttributeTransformer;
1413
use Code16\Sharp\Form\Eloquent\WithSharpFormEloquentUpdater;
1514
use Code16\Sharp\Form\Fields\Editor\Uploads\SharpFormEditorUpload;
@@ -30,7 +29,6 @@
3029
use Code16\Sharp\Form\SharpForm;
3130
use Code16\Sharp\Utils\Fields\FieldsContainer;
3231
use Illuminate\Support\Facades\Blade;
33-
use Illuminate\Support\Facades\Storage;
3432

3533
class PostForm extends SharpForm
3634
{
@@ -110,7 +108,7 @@ public function buildFormFields(FieldsContainer $formFields): void
110108
)
111109
->addField(
112110
SharpFormHtmlField::make('publication_label')
113-
->setLiveRefresh(linkedFields: ['author_id', 'published_at'])
111+
->setLiveRefresh(linkedFields: ['author_id', 'published_at', 'attachments'])
114112
->setTemplate(function (array $data) {
115113
if (! isset($data['published_at'])) {
116114
return '';
@@ -121,9 +119,13 @@ public function buildFormFields(FieldsContainer $formFields): void
121119
@if($author)
122120
by {{ $author->name }}.
123121
@endif
122+
<br>
123+
{{ count($linkAttachments) }} link attachments, {{ count($fileAttachments) }} file attachments.
124124
blade, [
125125
'published_at' => \Carbon\Carbon::parse($data['published_at'])->isoFormat('LLLL'),
126126
'author' => \App\Models\User::find($data['author_id']),
127+
'linkAttachments' => collect($data['attachments'])->where('is_link', true)->values(),
128+
'fileAttachments' => collect($data['attachments'])->where('is_link', false)->values(),
127129
]);
128130
})
129131
)
@@ -155,23 +157,6 @@ public function buildFormFields(FieldsContainer $formFields): void
155157
->setStorageBasePath('data/posts/{id}')
156158
->addConditionalDisplay('!is_link'),
157159
)
158-
->addItemField(
159-
SharpFormHtmlField::make('document_infos')
160-
->setLiveRefresh(linkedFields: ['document'])
161-
->setTemplate(function (array $data) {
162-
if (! isset($data['document']['file_name'])) {
163-
return '';
164-
}
165-
166-
return sprintf(
167-
'File uploaded at : %s',
168-
Carbon::createFromTimestamp(
169-
Storage::disk($data['document']['disk'])
170-
->lastModified($data['document']['file_name'])
171-
)
172-
);
173-
})
174-
),
175160
)
176161
->when(sharp()->context()->isUpdate(), fn ($formFields) => $formFields->addField(
177162
SharpFormAutocompleteRemoteField::make('author_id')
@@ -211,9 +196,7 @@ public function buildFormLayout(FormLayout $formLayout): void
211196
->withField('publication_label')
212197
->withListField('attachments', function (FormLayoutColumn $item) {
213198
$item->withFields(title: 8, is_link: 4)
214-
->withField('link_url')
215-
->withField('document')
216-
->withField('document_infos');
199+
->withField('link_url');
217200
});
218201
})
219202
->addColumn(6, function (FormLayoutColumn $column) {

docs/guide/form-fields/html.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This field is read-only, and is meant to display some dynamic information in the
66

77
## Configuration
88

9-
### `setTemplate(string|Closure|View $template)`
9+
### `setTemplate(string|View|Closure $template)`
1010

1111
Write the blade template as a string. Example:
1212

@@ -35,7 +35,32 @@ SharpFormHtmlField::make('panel')
3535
->setTemplate(view('sharp.form-htm-field'))
3636
```
3737

38-
### `setLiveRefresh(bool $liveRefresh = true, array $linkedeFields = [])`
38+
Using a closure:
39+
40+
```php
41+
SharpFormHtmlField::make('panel')
42+
->setTemplate(function (array $data) {
43+
return 'You have chosen:'.$data['another_form_field'].'. Date: '.$data['date'];
44+
})
45+
```
46+
47+
#### Accessing to other field values in the form
48+
49+
In the template, all other field values of the form are available (alongside the Html field value). This is particularly useful when using `setLiveRefresh()` (described below).
50+
51+
### `setLiveRefresh(bool $liveRefresh = true, ?array $linkedFields = null)`
52+
53+
Use this method to dynamically update Html field when the user changes another field.
54+
The `$linkedFields` parameter allows filtering which field to watch (without it the internal refresh endpoint is called on any field update).
55+
56+
```php
57+
SharpFormHtmlField::make('total')
58+
->setLiveRefresh(linkedFields: ['products'])
59+
->setTemplate(function (array $data) {
60+
return 'Total:'.collect($data['products'])
61+
->sum(fn ($product) => $product['price']);
62+
})
63+
```
3964

4065
## Formatter
4166

resources/js/form/components/Form.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@
103103
[fieldKey]: value,
104104
};
105105
106+
props.form.data = data;
107+
106108
if((props.form.shouldRefresh(fieldKey) || inputOptions.shouldRefresh) && !inputOptions.skipRefresh) {
107109
refresh(data);
108110
}
109-
110-
props.form.data = data;
111111
}
112112
113113
const title = useTemplateRef<HTMLElement>('title');

0 commit comments

Comments
 (0)