Skip to content

Commit f7f23bf

Browse files
author
aguingand
authored
Merge pull request #557 from code16/refactor-html-fields-templates
[9.0] Refactor html fields templates
2 parents c46cd84 + 3df5ac1 commit f7f23bf

File tree

26 files changed

+111
-282
lines changed

26 files changed

+111
-282
lines changed

demo/app/Sharp/Posts/Blocks/AbstractPostBlockForm.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ public function buildFormFields(FieldsContainer $formFields): void
2424
$formFields
2525
->addField(
2626
SharpFormHtmlField::make('type')
27-
->setInlineTemplate('Post block type: <strong>{{name}}</strong><div class="small" v-if="help">{{help}}</div>'),
27+
->setTemplate(<<<'blade'
28+
Post block type: <strong>{{ $name }}</strong>
29+
@if($help)
30+
<div><small>{{ $help }}</small></div>
31+
@endif'
32+
blade),
2833
);
2934

3035
if ($field = $this->getContentField()) {

demo/app/Sharp/Posts/Commands/ComposeEmailWithPostsWizardCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function buildFormFieldsForStepChooseRecipients(FieldsContainer $formFiel
121121
$formFields
122122
->addField(
123123
SharpFormHtmlField::make('message')
124-
->setInlineTemplate('<div style="max-height: 100px; overflow: auto; border: 1px solid #ddd; padding: 10px 15px; font-size: .85em;" v-html="text"></div>')
124+
->setTemplate('<div style="max-height: 100px; overflow: auto; border: 1px solid #ddd; padding: 10px 15px; font-size: .85em;">{!! $text !!}</div>')
125125
->setLabel('Message'),
126126
)
127127
->addField(

demo/app/Sharp/TestForm/TestForm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function buildFormFields(FieldsContainer $formFields): void
104104
->addField(
105105
SharpFormHtmlField::make('html')
106106
->setLabel('Html')
107-
->setInlineTemplate('Your name is <strong>{{name}}</strong>'),
107+
->setTemplate('Your name is <strong>{{ $name }}</strong>'),
108108
)
109109
->addField(
110110
SharpFormListField::make('list')

docs/guide/form-fields/html.md

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

77
## Configuration
88

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

11-
Write the template as a string, using placeholders for data (eg: `{{var}}`). Example:
11+
Write the blade template as a string. Example:
1212

1313
```php
1414
SharpFormHtmlField::make('panel')
15-
->setInlineTemplate('This product is offline since <strong>{{date}}</strong>')
15+
->setTemplate('This product is offline since <strong>{{ $date }}</strong>')
1616
```
1717

1818
This example would mean that your transformed data has an object named `panel` containing a `date` attribute. Here a custom transformer example for this particular case:
@@ -28,21 +28,13 @@ function find($id): array
2828
}
2929
```
3030

31-
### `setTemplatePath(string $templatePath)`
31+
You can also pass a view (blade) :
3232

33-
Use this if you need more control: give the path of a full template, in its own file.
34-
35-
The template will be [interpreted by Vue.js](https://vuejs.org/v2/guide/syntax.html), meaning you can add data placeholders, DOM structure but also directives, and anything that Vue will parse. For instance:
36-
37-
```vue
38-
<div v-if="show">result is {{value}}</div>
39-
<div v-else>result is unknown</div>
33+
```php
34+
SharpFormHtmlField::make('panel')
35+
->setTemplate(view('sharp.form-htm-field'))
4036
```
4137

42-
### `setAdditionalTemplateData(array $data)`
43-
44-
Pass data to the template that is not part of the transformed data.
45-
4638
## Formatter
4739

4840
- `toFront`: sent as provided.

docs/guide/show-fields/html.md

Lines changed: 0 additions & 46 deletions
This file was deleted.

docs/guide/upgrading/9.0.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,4 +826,13 @@ new class extends Migration
826826

827827
### `SharpFormListField` collapsed items template feature was removed
828828

829-
`setCollapsedItemInlineTemplate()` & `setCollapsedItemTemplatePath()` methods was removed due to limited usage and general migration into non-Vue templates.
829+
`setCollapsedItemInlineTemplate()` & `setCollapsedItemTemplatePath()` methods was removed due to limited usage and general migration into blade templates.
830+
831+
### `SharpFormHtmlField` has migrated to blade templates
832+
833+
`setInlineTemplate()` & `setTemplatePath()` must be converted to :
834+
- `setTemplate('blade template string')` or
835+
- `setTemplate(view('sharp.form-field'))`.
836+
837+
See [field page](../form-fields/html) for more information.
838+

ide.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
3+
"view": {
4+
"paths": [
5+
{
6+
"path": "tests/Fixtures/resources/views",
7+
"namespace": "fixtures"
8+
}
9+
]
10+
}
11+
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<script setup lang="ts" >
2-
import { TemplateRenderer } from '@/components';
32
import { FormHtmlFieldData } from "@/types";
43
import FormFieldLayout from "@/form/components/FormFieldLayout.vue";
54
import { FormFieldProps } from "@/form/types";
@@ -9,11 +8,7 @@
98

109
<template>
1110
<FormFieldLayout v-bind="props">
12-
<div class="text-sm">
13-
<TemplateRenderer
14-
:template="field.template"
15-
:template-data="value"
16-
/>
11+
<div class="text-sm" v-html="props.value">
1712
</div>
1813
</FormFieldLayout>
1914
</template>

resources/js/form/components/fields/list/List.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@
7373
});
7474
7575
let itemKeyIndex = 0;
76-
const itemKey = Symbol('itemKey');
77-
const errorIndex = Symbol('errorIndex');
76+
const itemKey = Symbol('itemKey') as unknown as string;
77+
const errorIndex = Symbol('errorIndex') as unknown as string;
7878
7979
watch(() => form.errors, () => {
8080
emit('input', props.value?.map(((item, index) => ({ ...item, [errorIndex]: index }))));

src/Auth/TwoFactor/Commands/Activate2faViaTotpWizardCommandTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function buildFormFieldsForStepConfirm(FieldsContainer $formFields): v
8686
->addField(
8787
SharpFormHtmlField::make('qr')
8888
->setLabel(trans('sharp::auth.2fa.totp.commands.activate.qrcode_field_label'))
89-
->setInlineTemplate('<div style="text-align: center; margin: 1em 0;" v-html="svg"></div>')
89+
->setTemplate('<div style="text-align: center; margin: 1em 0;">{!! $svg !!}</div>')
9090
)
9191
->addField(
9292
SharpFormTextField::make('code')

0 commit comments

Comments
 (0)