From 0b3ad6800dfa02a002c0724bccae5dfa722450f7 Mon Sep 17 00:00:00 2001 From: sunhao Date: Tue, 5 Mar 2024 15:52:26 +0800 Subject: [PATCH] * zin: support for setting props with control prop. --- lib/zin/func.php | 1 + lib/zin/wg/content/v1.php | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/zin/func.php b/lib/zin/func.php index e03f237fae..cba9ab424d 100644 --- a/lib/zin/func.php +++ b/lib/zin/func.php @@ -33,6 +33,7 @@ function formBatch(): formBatch {return createWg('formBatch', func_get_args());} function formBatchItem(): formBatchItem {return createWg('formBatchItem', func_get_args());} function formBatchPanel(): formBatchPanel {return createWg('formBatchPanel', func_get_args());} function batchActions(): batchActions {return createWg('batchActions', func_get_args());} +function content(): content {return createWg('content', func_get_args());} function listItem(): listItem {return createWg('listitem', func_get_args());} function breadcrumb(): breadcrumb {return createWg('breadcrumb', func_get_args());} function control(): control {return createWg('control', func_get_args());} diff --git a/lib/zin/wg/content/v1.php b/lib/zin/wg/content/v1.php index 72403221a0..ab0b479a7a 100644 --- a/lib/zin/wg/content/v1.php +++ b/lib/zin/wg/content/v1.php @@ -6,10 +6,18 @@ class content extends wg { protected static array $defineProps = array ( - 'control?: string', // 内容类型,值可以为:html, text 以及其他部件的类型。 + 'control?: string|array', // 内容类型,值可以为:html, text 以及其他部件的类型,也可以指定为包含 `control` 键值的控件属性数组。 'render?: callable|Closure' // 自定义构建函数。 ); + protected static array $controlMap = array + ( + 'list' => 'simpleList', + 'status' => 'statusLabel', + 'pri' => 'priLabel', + 'severity' => 'severityLabel', + ); + protected function buildText(): node { return div @@ -41,11 +49,6 @@ class content extends wg ); } - protected function buildList(): node - { - return new simpleList(set($this->props->skip('control')), $this->children()); - } - protected function buildDivider(): node { return hr(setClass('divider')); @@ -60,13 +63,28 @@ class content extends wg $control = $this->prop('control'); if($control) { - $methodName = "build{$control}"; + $controlProps = array(); + $controlName = ''; + if(is_string($control)) + { + $controlName = $control; + } + elseif(is_array($control)) + { + $controlName = $control['control']; + unset($control['control']); + $controlProps = $control; + } + + $methodName = "build{$controlName}"; if(method_exists($this, $methodName)) return $this->$methodName(); - $wgName = "\\zin\\$control"; - if(class_exists($wgName)) return new $wgName(set($this->props->skip('control')), $this->children()); + if(isset(static::$controlMap[$controlName])) $controlName = static::$controlMap[$controlName]; - return createWg($control, set($this->props->skip('control')), 'div'); + $wgName = "\\zin\\$controlName"; + if(class_exists($wgName)) return new $wgName(set($this->props->skip('controlName')), $controlProps ? set($controlProps) : null, $this->children()); + + return createWg($controlName, set($this->props->skip('control')), $controlProps ? set($controlProps) : null, 'div'); } if($this->hasProp('children')) $this->prop('children');