v1.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace zin;
  3. class btn extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'text?:string', // 按钮的文本。
  10. 'icon?:string', // 图标名称。
  11. 'iconClass?:string', // 图标的样式类。
  12. 'square?:bool', // 是否为方形按钮,通常用于只显示一个图标的按钮。
  13. 'disabled?:bool', // 是否禁用按钮。
  14. 'active?:bool', // 是否为激活状态。
  15. 'url?:string', // 按钮的链接地址。
  16. 'target?:string', // 按钮的链接目标。
  17. 'size?:string|int', // 按钮的尺寸,可选值为 `'xl'`、`'lg'`、`'md'`、`'sm'` 或者通过数字设置宽高,如 `20`。
  18. 'trailingIcon?:string', // 按钮尾部图标的名称。
  19. 'trailingIconClass?:string', // 按钮尾部图标的样式类。
  20. 'caret?:string|bool', // 按钮的下拉箭头,可选值为 `'top'`(向上)、`'bottom'`(向下) 或者 `true`(自动)。
  21. 'hint?:string', // 按钮的提示文本(鼠标悬停时显示)。
  22. 'type?:string', // 按钮的类型,可选值为 `'default'`、`'primary'`、`'success'`、`'info'`、`'warning'`、`'danger'`、`'link'`。
  23. 'btnType?:string="button"' // 按钮的类型,可选值为 `'button'`、`'submit'`、`'reset'`。
  24. );
  25. public function onAddChild($child)
  26. {
  27. if(is_string($child) && !$this->props->has('text'))
  28. {
  29. $this->props->set('text', $child);
  30. return false;
  31. }
  32. }
  33. protected function getProps()
  34. {
  35. $url = $this->prop('disabled') ? null : $this->prop('url');
  36. $target = $this->prop('target');
  37. $props = array_merge($this->getRestProps(), array('title' => $this->prop('hint')));
  38. if(empty($url))
  39. {
  40. $props['tagName'] = 'button';
  41. $props['type'] = $this->prop('btnType');
  42. if(!isset($props['data-target'])) $props['data-target'] = $target;
  43. return $props;
  44. }
  45. $props['tagName'] = 'a';
  46. if(!isset($props['href'])) $props['href'] = $url;
  47. if(!isset($props['target'])) $props['target'] = $target;
  48. return $props;
  49. }
  50. private function getChildren()
  51. {
  52. list($caret, $text, $icon, $iconClass, $trailingIcon, $trailingIconClass) = $this->prop(array('caret', 'text', 'icon', 'iconClass', 'trailingIcon', 'trailingIconClass'));
  53. $children = array();
  54. if(!empty($icon)) $children[] = icon($icon, setClass($iconClass));
  55. if(!empty($text)) $children[] = h::span($text, setClass('text'));
  56. $children[] = parent::build();
  57. if(!empty($trailingIcon)) $children[] = icon($trailingIcon, setClass($trailingIconClass));
  58. if(!empty($caret)) $children[] = h::span(setClass(is_string($caret) ? "caret-$caret" : 'caret'));
  59. return $children;
  60. }
  61. protected function getClassList()
  62. {
  63. list($url, $type, $caret, $text, $icon, $trailingIcon) = $this->prop(array('url', 'type', 'caret', 'text', 'icon', 'trailingIcon'));
  64. $onlyCaret = empty($text) && !empty($caret) && empty($icon) && empty($trailingIcon);
  65. $classList = array(
  66. 'btn' => true,
  67. 'disabled' => $this->prop('disabled'),
  68. 'active' => $this->prop('active'),
  69. 'btn-caret' => $onlyCaret,
  70. 'square' => $this->prop('square')
  71. );
  72. if(empty($type) && !empty($url)) $type = 'btn-default';
  73. else if($type === 'link') $type = 'btn-link';
  74. else if($type === 'default') $type = 'btn-default';
  75. if(!empty($type)) $classList[$type] = true;
  76. if(empty($text) && !empty($icon) && !isset($classList['square'])) $classList['square'] = true;
  77. $size = $this->prop('size');
  78. if(!empty($size)) $classList["size-$size"] = true;
  79. return $classList;
  80. }
  81. protected function build()
  82. {
  83. $props = $this->getProps();
  84. $children = $this->getChildren();
  85. $classList = $this->getClassList();
  86. return new h
  87. (
  88. set($props),
  89. setClass($classList),
  90. $children
  91. );
  92. }
  93. }