|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\React; |
|
4
|
|
|
|
|
5
|
|
|
use Formularium\Field; |
|
6
|
|
|
use Formularium\HTMLElement; |
|
7
|
|
|
|
|
8
|
|
|
trait RenderableReactTrait |
|
9
|
|
|
{ |
|
10
|
|
|
public function viewable($value, Field $field, HTMLElement $previous): HTMLElement |
|
11
|
|
|
{ |
|
12
|
|
|
return $this->fix($value, $field, $previous); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
public function editable($value, Field $field, HTMLElement $previous): HTMLElement |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->fix($value, $field, $previous); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function fix($value, Field $field, HTMLElement $previous): HTMLElement |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$previous->walk(function (HTMLElement $element) use ($field) { |
|
23
|
|
|
if ($element->getTag() === 'input') { |
|
24
|
|
|
if ($element->getAttribute('type') === ['checkbox']) { |
|
25
|
|
|
$element->setAttribute('checked', "{this.state.{$field->getName()}}"); |
|
26
|
|
|
} else { |
|
27
|
|
|
$element->setAttribute('value', "{this.state.{$field->getName()}}"); |
|
28
|
|
|
} |
|
29
|
|
|
$element->setAttribute('onChange', '{this.handleInputChange}'); |
|
30
|
|
|
} |
|
31
|
|
|
if ($element->getTag() === 'textarea' || $element->getTag() === 'select') { |
|
32
|
|
|
$element->setAttribute('value', "{this.state.{$field->getName()}}"); |
|
33
|
|
|
$element->setAttribute('onChange', '{this.handleInputChange}'); |
|
34
|
|
|
} |
|
35
|
|
|
if ($element->getTag() === 'select') { |
|
36
|
|
|
$options = $element->get('[selected=selected]'); |
|
37
|
|
|
if (!empty($options)) { |
|
38
|
|
|
$element->setAttribute('value', $options[0]->getAttribute('value')[0]); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
if ($element->getTag() === 'option') { |
|
42
|
|
|
$element->removeAttribute('selected'); |
|
43
|
|
|
} |
|
44
|
|
|
if ($element->hasAttribute('for')) { |
|
45
|
|
|
$element->setAttribute('htmlFor', $element->getAttribute('for')); |
|
46
|
|
|
$element->removeAttribute('for'); |
|
47
|
|
|
} |
|
48
|
|
|
if ($element->hasAttribute('class')) { |
|
49
|
|
|
$element->setAttribute('className', $element->getAttribute('class')); |
|
50
|
|
|
$element->removeAttribute('class'); |
|
51
|
|
|
} |
|
52
|
|
|
if (!empty($element->getAttribute('minlength'))) { |
|
53
|
|
|
$element->setAttribute('minLength', $element->getAttribute('minlength')); |
|
54
|
|
|
$element->removeAttribute('minlength'); |
|
55
|
|
|
} |
|
56
|
|
|
if (!empty($element->getAttribute('maxlength'))) { |
|
57
|
|
|
$element->setAttribute('maxLength', $element->getAttribute('maxlength')); |
|
58
|
|
|
$element->removeAttribute('maxlength'); |
|
59
|
|
|
} |
|
60
|
|
|
}); |
|
61
|
|
|
return $previous; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.