v1.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace zin;
  3. require_once dirname(__DIR__) . DS . 'toolbar' . DS . 'v1.php';
  4. class section extends wg
  5. {
  6. /**
  7. * @var mixed[]
  8. */
  9. protected static $defineProps = array(
  10. 'title?: string', // 标题
  11. 'content?: string|array', // 内容
  12. 'useHtml?: bool=false', // 内容是否解析 HTML 标签
  13. 'required?: bool=false' // 标题上是否显示必填标记
  14. );
  15. /**
  16. * @var mixed[]
  17. */
  18. protected static $defineBlocks = array(
  19. 'subtitle' => array(),
  20. 'actions' => array()
  21. );
  22. /**
  23. * @param mixed $child
  24. */
  25. protected function onAddChild($child)
  26. {
  27. if(is_string($child) && !$this->props->has('content'))
  28. {
  29. $this->props->set('content', $child);
  30. return false;
  31. }
  32. }
  33. private function title()
  34. {
  35. $title = $this->prop('title');
  36. $titleActions = $this->prop('titleActions');
  37. $actionsView = $this->block('actions');
  38. $required = $this->prop('required');
  39. if(empty($actionsView))
  40. {
  41. return div
  42. (
  43. setClass('font-bold text-md', 'mb-2', 'inline-flex'),
  44. $required ? h::label(setClass('form-label required mr-1 pb-3')) : null,
  45. $titleActions ? toolbar::create($titleActions) : null,
  46. $title
  47. );
  48. }
  49. return div
  50. (
  51. setClass('flex', 'items-center', 'mb-2'),
  52. div
  53. (
  54. setClass('font-bold text-md', 'inline-flex'),
  55. $required ? h::label(setClass('form-label required mr-1')) : null,
  56. $titleActions ? toolbar::create($titleActions) : null,
  57. $title
  58. ),
  59. $actionsView
  60. );
  61. }
  62. /**
  63. * @param string|\zin\node $content
  64. */
  65. private function content($content)
  66. {
  67. $useHtml = $this->prop('useHtml') === true && is_string($content);
  68. return div
  69. (
  70. setClass('article'),
  71. $useHtml ? html($content) : $content
  72. );
  73. }
  74. /**
  75. * @return \zin\node|mixed[]|null
  76. */
  77. private function buildContent()
  78. {
  79. $content = $this->prop('content');
  80. if(!isset($content)) return null;
  81. return $this->content($content);
  82. }
  83. protected function build()
  84. {
  85. return div
  86. (
  87. setClass('section'),
  88. set($this->getRestProps()),
  89. $this->title(),
  90. $this->block('subtitle'),
  91. $this->buildContent(),
  92. $this->children()
  93. );
  94. }
  95. }