diff --git a/src/Common/UiElement.php b/src/Common/UiElement.php
index a869fdc..982033a 100644
--- a/src/Common/UiElement.php
+++ b/src/Common/UiElement.php
@@ -178,7 +178,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
}
else if ( !is_numeric($key) ) {
# will output something like
- $attributesList[] = "$key=\"$value\"";
+ $attributesList[] = strpos($value, '"') !== false ? "$key='$value'" : "$key=\"$value\"";
}
else {
# will output something like
@@ -186,7 +186,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
}
}
-
+
$content = "";
foreach ( $this->childs as $item ) {
@@ -198,14 +198,14 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
}
}
- if ( in_array('no-tag', $this->options) ) {
+ if ( in_array('no-tag', $this->options, true) ) {
return $this->content . $content;
}
else {
- $compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options) ? " " . implode(" ", $attributesList) : "");
+ $compiledAttributes = (count($attributesList) && ! in_array('no-attr', $this->options, true) ? " " . implode(" ", $attributesList) : "");
# Force the node to contain a certain opening tag (php is a good example)
- if ( in_array('force-tag-open', $this->options) ) {
+ if ( in_array('force-tag-open', $this->options, true) ) {
$opentag = $this->options['force-tag-open'] . $compiledAttributes;
}
else {
@@ -217,7 +217,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
$closetag = "";
}
}
- elseif ( in_array('force-tag-close', $this->options) ) {
+ elseif ( in_array('force-tag-close', $this->options, true) ) {
$closetag = $this->options['force-tag-close'];
}
else {
@@ -346,7 +346,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
public function jsonSerialize() {
return [
'tag' => $this->tag,
- 'attr' => $this->attr,
+ 'attr' => $this->attributes,
'childs' => $this->childs,
'options' => $this->options,
];
@@ -404,7 +404,7 @@ class UiElement implements \ArrayAccess, \Iterator, \JsonSerializable {
return ! in_array(key($this->childs), [ NULL, FALSE ], true);
}
- public static function is_node($obj) : bool
+ public static function isNode($obj) : bool
{
return $obj instanceof UiElement;
}
diff --git a/src/Form/UiCheckbox.php b/src/Form/UiCheckbox.php
index 59bd1fb..7a9fcd8 100644
--- a/src/Form/UiCheckbox.php
+++ b/src/Form/UiCheckbox.php
@@ -2,12 +2,12 @@
namespace Picea\Ui\Form;
-class UiCheckbox extends UiInput {
+class UiCheckbox extends UiRadio {
- public string $token = "ui.checkbox";
-
- public array $attributes = [
- 'class' => "ui-checkbox",
- 'type' => "checkbox",
- ];
+ public string $token = "ui.checkbox";
+
+ public array $attributes = [
+ 'class' => "ui-checkbox",
+ 'type' => "checkbox",
+ ];
}
diff --git a/src/Form/UiForm.php b/src/Form/UiForm.php
index 1a0450f..5d5b5e2 100644
--- a/src/Form/UiForm.php
+++ b/src/Form/UiForm.php
@@ -57,6 +57,12 @@ class UiForm extends UiElement implements Extension {
$this->option('tag-type', 'single');
+
+ if ($attributes['class'] ?? false) {
+ $attributes['class'] .= " {$this->attributes['class']}";
+ unset($this->attributes['class']);
+ }
+
$this->attributes([ 'method' => $method, 'action' => $action ] + $attributes);
if ( $method !== "get" ) {
diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php
index 2abeeda..d3812ff 100644
--- a/src/Form/UiInput.php
+++ b/src/Form/UiInput.php
@@ -21,15 +21,34 @@ class UiInput extends UiElement implements Extension {
'tag-type' => "single",
];
+ protected /* ? mixed */ $value;
+
+ protected string $name;
+
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) : string
{
return "buildHtml($arguments) ?>";
}
- public function buildHtml(string $name, ? string $value = null, array $attributes = []) : string
+ public function buildHtml(string $name, /*? mixed */ $value = null, array $attributes = [], array $options = []) : string
{
- $this->setValue($value);
- $this->attributes([ 'name' => $name ] + $attributes + $this->attributes);
+ $this->name = $name;
+
+ if (null !== $this->value = $value) {
+ $this->setValue($value);
+ }
+
+ if ($options) {
+ $this->options += $options;
+ }
+
+ if ($attributes['class'] ?? false) {
+ $attributes['class'] .= " {$this->attributes['class']}";
+ unset($this->attributes['class']);
+ }
+
+ $this->attributes([ 'name' => $name ] + $attributes + $this->attributes + $this->objectAttribute());
+
return $this->render();
}
@@ -37,5 +56,9 @@ class UiInput extends UiElement implements Extension {
{
$this->attributes['value'] = $value;
}
-
+
+ protected function objectAttribute() : array
+ {
+ return [];
+ }
}
diff --git a/src/Form/UiNumeric.php b/src/Form/UiNumeric.php
index 2c3ad5c..c0ab774 100644
--- a/src/Form/UiNumeric.php
+++ b/src/Form/UiNumeric.php
@@ -8,6 +8,6 @@ class UiNumeric extends UiInput {
public array $attributes = [
'class' => "ui-numeric",
- 'type' => "numeric",
+ 'type' => "number",
];
}
diff --git a/src/Form/UiRadio.php b/src/Form/UiRadio.php
index be8d1d7..d335d22 100644
--- a/src/Form/UiRadio.php
+++ b/src/Form/UiRadio.php
@@ -4,10 +4,21 @@ namespace Picea\Ui\Form;
class UiRadio extends UiInput {
- public string $token = "ui.radio";
+ public string $token = "ui.radio";
- public array $attributes = [
- 'class' => "ui-radio",
- 'type' => "radio",
- ];
+ public array $attributes = [
+ 'class' => "ui-radio",
+ 'type' => "radio",
+ ];
+
+ protected function objectAttribute() : array
+ {
+ if ( ( $this->options['value'] ?? false ) && ( $this->options['value'] === $this->value ) ) {
+ return [
+ 'checked' => "checked"
+ ];
+ }
+
+ return [];
+ }
}
diff --git a/src/Form/UiSelect.php b/src/Form/UiSelect.php
index 7eca440..5ca08a4 100644
--- a/src/Form/UiSelect.php
+++ b/src/Form/UiSelect.php
@@ -38,26 +38,23 @@ class UiSelect extends UiElement implements Extension {
protected function setList(array $list, $selected, bool $strict = true) : void
{
foreach($list as $key => $item) {
- $isSelected = $this->isSelected($key, $selected, $strict);
-
if ($item instanceof UiElement) {
-
$this->append($item);
continue;
}
elseif ( is_array($item) ) {
$obj = new UiElement("optgroup");
- $obj->text($key);
+ $obj->attributes(['label' => $key]);
foreach($item as $subKey => $subItem) {
- $obj->append($this->createOption((string) $subItem, $subKey, $isSelected));
+ $obj->append($this->createOption((string) $subItem, $subKey, $this->isSelected($subKey, $selected, $strict)));
}
}
else {
- $obj = $this->createOption((string) $item, $key, $isSelected);
+ $obj = $this->createOption((string) $item, $key, $this->isSelected($key, $selected, $strict));
}
-
+
$this->append($obj);
}
}
@@ -77,14 +74,6 @@ class UiSelect extends UiElement implements Extension {
protected function isSelected($check, $value, bool $strict = true) : bool
{
-
- if (false !== ( $f = filter_var($value, FILTER_VALIDATE_INT) ) ) {
- $value = $f;
- }
- elseif (false !== ( $f = filter_var($value, FILTER_VALIDATE_FLOAT) ) ) {
- $value = $f;
- }
-
return $strict ? $check === $value : $check == $value;
}
}
diff --git a/src/Method/FormHandler.php b/src/Method/FormHandler.php
index a6a821d..223bb44 100644
--- a/src/Method/FormHandler.php
+++ b/src/Method/FormHandler.php
@@ -28,15 +28,17 @@ class FormHandler {
$this->context = method_exists($form, 'getContext') ? $form->getContext($request) : new FormContext($request);
}
+ $this->request->context = $this->context;
+
$this->formSent();
$this->initialize();
}
- protected function formSent() : void
+ public function formSent() : void
{
if ( false !== $this->context->formSent = $this->sent ) {
if ( $this->context->formName ?? false ) {
- $this->context->formSent = (bool) ( $this->request->getParsedBody()['picea-ui-form'][$this->context->formName] ?? false );
+ $this->sent = $this->context->formSent = (bool) ( $this->request->getParsedBody()['picea-ui-form'][$this->context->formName] ?? false );
}
}
}