Skip to content

Commit 633a044

Browse files
author
antoine
committed
wip refactor html fields templates
1 parent c46cd84 commit 633a044

File tree

17 files changed

+106
-149
lines changed

17 files changed

+106
-149
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')
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')

src/Form/Fields/Formatters/HtmlFormatter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
namespace Code16\Sharp\Form\Fields\Formatters;
44

55
use Code16\Sharp\Form\Fields\SharpFormField;
6+
use Code16\Sharp\Form\Fields\SharpFormHtmlField;
67

78
class HtmlFormatter extends AbstractSimpleFormatter
89
{
10+
/**
11+
* @param SharpFormHtmlField $field
12+
*/
13+
public function toFront(SharpFormField $field, $value)
14+
{
15+
return $value ? $field->render($value) : null;
16+
}
17+
918
public function fromFront(SharpFormField $field, string $attribute, $value)
1019
{
1120
return null;

src/Form/Fields/SharpFormHtmlField.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,38 @@
33
namespace Code16\Sharp\Form\Fields;
44

55
use Code16\Sharp\Form\Fields\Formatters\HtmlFormatter;
6-
use Code16\Sharp\Form\Fields\Utils\SharpFormFieldWithTemplates;
6+
use Illuminate\Contracts\View\View;
7+
use Illuminate\Support\Facades\Blade;
78

89
class SharpFormHtmlField extends SharpFormField
910
{
10-
use SharpFormFieldWithTemplates {
11-
setTemplatePath as protected parentSetTemplatePath;
12-
setInlineTemplate as protected parentSetInlineTemplate;
13-
template as protected parentTemplate;
14-
}
15-
1611
const FIELD_TYPE = 'html';
17-
12+
13+
private View|string $template;
14+
1815
public static function make(string $key): self
1916
{
2017
return new static($key, static::FIELD_TYPE, new HtmlFormatter);
2118
}
22-
23-
public function setTemplatePath(string $templatePath): self
24-
{
25-
return $this->parentSetTemplatePath($templatePath, 'html');
26-
}
27-
28-
public function setInlineTemplate(string $template): self
29-
{
30-
return $this->parentSetInlineTemplate($template, 'html');
31-
}
32-
33-
public function setAdditionalTemplateData(array $data): self
34-
{
35-
return $this->setTemplateData($data);
36-
}
37-
38-
public function template(): ?string
19+
20+
public function toArray(): array
3921
{
40-
return $this->parentTemplate('html');
22+
return parent::buildArray([]);
4123
}
42-
43-
protected function validationRules(): array
24+
25+
public function setTemplate(View|string $template): self
4426
{
45-
return [
46-
'template' => 'required',
47-
'templateData' => 'nullable|array',
48-
];
27+
$this->template = $template;
28+
29+
return $this;
4930
}
50-
51-
public function toArray(): array
31+
32+
public function render(array $data): string
5233
{
53-
return parent::buildArray([
54-
'template' => $this->template(),
55-
'templateData' => $this->additionalTemplateData,
56-
]);
34+
if (is_string($this->template)) {
35+
return Blade::render($this->template, $data);
36+
}
37+
38+
return $this->template->with($data)->render();
5739
}
5840
}

src/Form/Fields/SharpFormListField.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Code16\Sharp\Form\Fields;
44

55
use Code16\Sharp\Form\Fields\Formatters\ListFormatter;
6-
use Code16\Sharp\Form\Fields\Utils\SharpFormFieldWithTemplates;
76

87
class SharpFormListField extends SharpFormField
98
{

src/Form/Fields/Utils/SharpFormFieldWithTemplates.php

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

0 commit comments

Comments
 (0)