From 5263f453adc75c0cb33e32a108ba5e956de92c4c Mon Sep 17 00:00:00 2001 From: sunhao Date: Tue, 5 Mar 2024 16:39:01 +0800 Subject: [PATCH] + zin: add datalist widget. --- lib/zin/func.php | 1 + lib/zin/wg/datalist/v1.php | 81 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 lib/zin/wg/datalist/v1.php diff --git a/lib/zin/func.php b/lib/zin/func.php index 5319a1c0e5..aae2a988c9 100644 --- a/lib/zin/func.php +++ b/lib/zin/func.php @@ -37,6 +37,7 @@ function content(): content {return createWg('content', func_get_args());} function listItem(): listItem {return createWg('listitem', func_get_args());} function simpleList(): simpleList {return createWg('simplelist', func_get_args());} function breadcrumb(): breadcrumb {return createWg('breadcrumb', func_get_args());} +function datalist(): datalist {return createWg('datalist', func_get_args());} function control(): control {return createWg('control', func_get_args());} function select(): select {return createWg('select', func_get_args());} function formLabel(): formLabel {return createWg('formLabel', func_get_args());} diff --git a/lib/zin/wg/datalist/v1.php b/lib/zin/wg/datalist/v1.php new file mode 100644 index 0000000000..1e0e37f312 --- /dev/null +++ b/lib/zin/wg/datalist/v1.php @@ -0,0 +1,81 @@ + '?array', + 'labelWidth' => '?int=76' + ); + + public function onBuildItem($item): node|null + { + if($item === null) return null; + + if($item instanceof setting) $item = $item->toArray(); + if($item instanceof item) + { + $item = array_merge($item->props->toArray(), array('children' => $item->children())); + } + if($item instanceof node) return $item; + + $label = isset($item['label']) ? $item['label'] : null; + $children = isset($item['children']) ? $item['children'] : null; + $content = null; + + unset($item['label']); + unset($item['children']); + $content = isset($item['control']) ? new content(set($item), $children) : $children; + + return div + ( + setClass('datalist-item'), + div + ( + setClass('datalist-item-label'), + $label + ), + div + ( + setClass('datalist-item-content'), + $content + ) + ); + } + + protected function buildItems() + { + $items = $this->prop('items'); + $itemsView = array(); + if(is_array($itemsView)) + { + foreach ($items as $key => $item) + { + if($item === '-') $item = array('control' => 'divider'); + elseif(is_string($item)) $item = array('children' => $item); + + if(is_array($item) && is_string($key)) $item['label'] = $key; + + $itemsView[] = $this->onBuildItem($item); + } + } + + return $itemsView; + } + + protected function build() + { + return div + ( + setClass('datalist'), + setStyle('--datalist-label-width', $this->prop('labelWidth') . 'px'), + set($this->getRestProps()), + $this->buildItems(), + $this->children() + ); + } +}