v1.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'listitem' . DS . 'v1.php';
  4. require_once dirname(__DIR__) . DS . 'content' . DS . 'v1.php';
  5. class simpleList extends wg
  6. {
  7. /**
  8. * @var bool
  9. */
  10. public $hasIcons = false;
  11. /**
  12. * @var bool
  13. */
  14. public $hasCheckbox = false;
  15. /**
  16. * @var mixed[]
  17. */
  18. protected static $defineProps = array
  19. (
  20. 'items' => '?array',
  21. 'tagName' => '?string="ul"',
  22. 'divider' => '?bool', // 是否显示分割线。
  23. 'border' => '?bool', // 是否显示边框。
  24. 'hover' => '?bool=true', // 是否有鼠标悬停效果。
  25. 'compact' => '?bool=true', // 是否显示为紧凑模式。
  26. 'onRenderItem' => '?callable', // 渲染条目的回调函数。
  27. );
  28. public static function getPageCSS()
  29. {
  30. return <<<'CSS'
  31. .simple-list.has-border {border: 1px solid var(--color-border);}
  32. .simple-list.has-border .list-item {padding-left: 8px; padding-right: 8px;}
  33. .simple-list.has-divider * + .list-item {border-top: 1px solid var(--color-border);}
  34. .simple-list.has-hover .list-item {transition: .2s background-color;}
  35. .simple-list.has-hover .list-item:hover {background-color: var(--state-color);}
  36. .simple-list.is-loose .list-item {padding-top: 2px; padding-bottom: 2px;}
  37. CSS;
  38. }
  39. /**
  40. * @var bool
  41. */
  42. public $isH5List = true;
  43. public function onBuildItem($item)
  44. {
  45. if($item === null) return null;
  46. if($item instanceof item)
  47. {
  48. $item = array_merge($item->props->toArray(), array('children' => $item->children()));
  49. }
  50. if($item instanceof node) return $item;
  51. if(isset($item['control']))
  52. {
  53. return new content(set($item));
  54. }
  55. $type = isset($item['type']) ? $item['type'] : 'item';
  56. if($type === 'divider')
  57. {
  58. return div(setClass('divider list-divider item'));
  59. }
  60. if(isset($item['icon']) && $item['icon'] !== null) $this->hasIcons = true;
  61. if(isset($item['checked']) && $item['checked'] !== null) $this->hasCheckbox = true;
  62. return new listItem
  63. (
  64. $this->isH5List ? set::tagName('li') : null,
  65. setClass("list-$type"),
  66. set($item)
  67. );
  68. }
  69. protected function buildItems()
  70. {
  71. $items = $this->prop('items');
  72. $onRenderItem = $this->prop('onRenderItem');
  73. $itemsView = array();
  74. if(is_array($items))
  75. {
  76. foreach ($items as $key => $item)
  77. {
  78. if(is_callable($onRenderItem)) $item = $onRenderItem($item, $key);
  79. if(is_string($item)) $item = array('title' => $item);
  80. if(is_array($item) && is_string($key) && !isset($item['title'])) $item['title'] = $key;
  81. $itemsView[] = $this->onBuildItem($item);
  82. }
  83. }
  84. return $itemsView;
  85. }
  86. protected function build()
  87. {
  88. $tagName = $this->prop('tagName');
  89. $divider = $this->prop('divider');
  90. $border = $this->prop('border');
  91. $this->isH5List = $tagName === 'ul' || $tagName === 'ol';
  92. $items = $this->buildItems();
  93. return h::$tagName
  94. (
  95. setClass('list simple-list', array('has-icons' => $this->hasIcons, 'has-checkbox' => $this->hasCheckbox, 'has-divider' => $divider, 'has-border' => $border, 'has-hover' => $this->prop('hover')), $this->prop('compact') ? 'is-compact' : 'is-loose'),
  96. set($this->getRestProps()),
  97. $items,
  98. $this->children()
  99. );
  100. }
  101. }