Skip to content

Commit 37c6014

Browse files
committed
wip live html
1 parent d595c69 commit 37c6014

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

src/Form/Fields/Formatters/HtmlFormatter.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@
77

88
class HtmlFormatter extends AbstractSimpleFormatter
99
{
10+
protected ?string $fieldKey = null;
11+
protected ?array $formData = null;
12+
1013
/**
1114
* @param SharpFormHtmlField $field
1215
*/
1316
public function toFront(SharpFormField $field, $value)
1417
{
15-
return $field->render(is_array($value) ? $value : []);
18+
return $field->render([
19+
'fieldKey' => $this->fieldKey,
20+
'formData' => $this->formData,
21+
...is_array($value) ? $value : [],
22+
]);
1623
}
1724

1825
public function fromFront(SharpFormField $field, string $attribute, $value)
1926
{
2027
return null;
2128
}
29+
30+
public function setRenderData(string $fieldKey, array $formData): self
31+
{
32+
$this->fieldKey = $fieldKey;
33+
$this->formData = $formData;
34+
35+
return $this;
36+
}
2237
}

src/Form/Fields/SharpFormHtmlField.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ class SharpFormHtmlField extends SharpFormField
1111
const FIELD_TYPE = 'html';
1212

1313
private View|string $template;
14+
private bool $liveRefresh = false;
1415

1516
public static function make(string $key): self
1617
{
1718
return new static($key, static::FIELD_TYPE, new HtmlFormatter());
1819
}
1920

21+
public function setLiveRefresh(bool $liveRefresh = true): self
22+
{
23+
$this->liveRefresh = $liveRefresh;
24+
25+
return $this;
26+
}
27+
2028
public function setTemplate(View|string $template): self
2129
{
2230
$this->template = $template;
@@ -35,6 +43,8 @@ public function render(array $data): string
3543

3644
public function toArray(): array
3745
{
38-
return parent::buildArray([]);
46+
return parent::buildArray([
47+
'liveRefresh' => $this->liveRefresh,
48+
]);
3949
}
4050
}

src/Utils/Fields/HandleFields.php

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

55
use Code16\Sharp\Form\Fields\SharpFormField;
6+
use Code16\Sharp\Form\Fields\SharpFormHtmlField;
67
use Code16\Sharp\Show\Fields\SharpShowField;
78
use Illuminate\Support\Collection;
89

@@ -87,12 +88,38 @@ final public function applyFormatters(?array $attributes): ?array
8788

8889
$field = $this->findFieldByKey($key);
8990

91+
if ($field instanceof SharpFormHtmlField) {
92+
return $value;
93+
}
94+
9095
return $field
9196
? $field->formatter()
9297
->setDataLocalizations($this->getDataLocalizations())
9398
->toFront($field, $value)
9499
: $value;
95100
})
101+
->pipe(function (Collection $data) {
102+
$formData = collect($data)->map(function ($value, $key) {
103+
if ($field = $this->findFieldByKey($key)) {
104+
return $field->formatter()
105+
->setDataLocalizations($this->getDataLocalizations())
106+
->fromFront($field, $key, $value);
107+
}
108+
109+
return $value;
110+
})->all();
111+
112+
return $data->map(function ($value, $key) use ($formData) {
113+
if (($field = $this->findFieldByKey($key)) instanceof SharpFormHtmlField) {
114+
return $field->formatter()
115+
->setRenderData(fieldKey: $key, formData: $formData)
116+
->setDataLocalizations($this->getDataLocalizations())
117+
->toFront($field, $value);
118+
}
119+
120+
return $value;
121+
});
122+
})
96123
->all();
97124
}
98125

0 commit comments

Comments
 (0)