From 328a4d282ccc39ca6c1e6652ffa9eb6e4ff99d9c Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 12 May 2023 18:58:22 +0000 Subject: [PATCH] - Added new methods for each inputs ui_...; fixed empty or 0 values checkbox/radio checked handling --- src/Form/UiInput.php | 20 ++++++++++++++++---- src/Form/UiRadio.php | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Form/UiInput.php b/src/Form/UiInput.php index 96a3dca..4039e04 100644 --- a/src/Form/UiInput.php +++ b/src/Form/UiInput.php @@ -3,10 +3,9 @@ namespace Picea\Ui\Form; use Picea\Ui\Common\UiElement; -use Picea\Extension\Extension; -use Picea\Extension\ExtensionTrait; +use Picea\Extension\{ Extension, FunctionExtension, ExtensionTrait }; -class UiInput extends UiElement implements Extension { +class UiInput extends UiElement implements Extension, FunctionExtension { use ExtensionTrait; public string $token = "ui:input"; @@ -19,7 +18,7 @@ class UiInput extends UiElement implements Extension { 'tag-type' => "single", ]; - protected /* ? mixed */ $value; + protected mixed $value; protected string $name; @@ -32,6 +31,19 @@ class UiInput extends UiElement implements Extension { return "buildHtml($arguments) ?>"; } + public function exportFunctions(): array + { + if ( null === $type = $this->attributes['type'] ?? null ) { + return []; + } + + $key = str_replace('-', '_', 'ui_'. ( $this->attributes['type'] ?? 'text' )); + + return [ + $key => fn(string $name, mixed $value = null, array $attributes = [], array $options = []) : string => $this->buildHtml($name, $value, $attributes, $options), + ]; + } + public function buildHtml(string $name, mixed $value = null, array $attributes = [], array $options = []) : string { $this->name = $name; diff --git a/src/Form/UiRadio.php b/src/Form/UiRadio.php index 7ff99e8..c4bf2eb 100644 --- a/src/Form/UiRadio.php +++ b/src/Form/UiRadio.php @@ -13,7 +13,7 @@ class UiRadio extends UiInput { protected function objectAttribute() : array { - if ( ( $this->options['value'] ?? false ) && ( $this->options['value'] === $this->value ) ) { + if ( isset($this->options['value']) && ( $this->options['value'] === $this->value ) ) { return [ 'checked' => "checked" ];