Tab.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Tab extends Widget
  5. {
  6. const TYPE_CONTENT = 1;
  7. const TYPE_LINK = 2;
  8. /**
  9. * @var string
  10. */
  11. protected $view = 'admin::widgets.tab';
  12. /**
  13. * @var array
  14. */
  15. protected $data = [
  16. 'id' => '',
  17. 'title' => '',
  18. 'tabs' => [],
  19. 'dropDown' => [],
  20. 'active' => 0,
  21. 'padding' => null,
  22. 'tabStyle' => '',
  23. ];
  24. /**
  25. * Add a tab and its contents.
  26. *
  27. * @param string $title
  28. * @param string|Renderable $content
  29. * @param bool $active
  30. *
  31. * @return $this
  32. */
  33. public function add($title, $content, $active = false)
  34. {
  35. $this->data['tabs'][] = [
  36. 'id' => mt_rand(),
  37. 'title' => $title,
  38. 'content' => $this->toString($this->formatRenderable($content)),
  39. 'type' => static::TYPE_CONTENT,
  40. ];
  41. if ($active) {
  42. $this->data['active'] = count($this->data['tabs']) - 1;
  43. }
  44. return $this;
  45. }
  46. /**
  47. * Add a link on tab.
  48. *
  49. * @param string $title
  50. * @param string $href
  51. * @param bool $active
  52. *
  53. * @return $this
  54. */
  55. public function addLink($title, $href, $active = false)
  56. {
  57. $this->data['tabs'][] = [
  58. 'id' => mt_rand(),
  59. 'title' => $title,
  60. 'href' => $href,
  61. 'type' => static::TYPE_LINK,
  62. ];
  63. if ($active) {
  64. $this->data['active'] = count($this->data['tabs']) - 1;
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Set tab content padding.
  70. *
  71. * @param string $padding
  72. */
  73. public function padding(string $padding)
  74. {
  75. $this->data['padding'] = 'padding:'.$padding;
  76. return $this;
  77. }
  78. public function noPadding()
  79. {
  80. return $this->padding('0');
  81. }
  82. /**
  83. * Set title.
  84. *
  85. * @param string $title
  86. */
  87. public function title($title = '')
  88. {
  89. $this->data['title'] = $title;
  90. return $this;
  91. }
  92. /**
  93. * Set drop-down items.
  94. *
  95. * @param array $links
  96. *
  97. * @return $this
  98. */
  99. public function dropdown(array $links)
  100. {
  101. if (is_array($links[0])) {
  102. foreach ($links as $link) {
  103. call_user_func([$this, 'dropDown'], $link);
  104. }
  105. return $this;
  106. }
  107. $this->data['dropDown'][] = [
  108. 'name' => $links[0],
  109. 'href' => $links[1],
  110. ];
  111. return $this;
  112. }
  113. public function withCard()
  114. {
  115. return $this
  116. ->class('card', true)
  117. ->style('padding:.25rem .4rem .4rem');
  118. }
  119. public function vertical()
  120. {
  121. return $this
  122. ->class('nav-vertical d-block', true)
  123. ->style('padding:0!important;')
  124. ->tabStyle('nav-left flex-column');
  125. }
  126. public function theme(string $style = 'primary')
  127. {
  128. return $this
  129. ->class('nav-theme-'.$style, true)
  130. ->style('padding:0!important;');
  131. }
  132. public function tabStyle($type)
  133. {
  134. $this->data['tabStyle'] = $type;
  135. return $this;
  136. }
  137. /**
  138. * Render Tab.
  139. *
  140. * @return string
  141. */
  142. public function render()
  143. {
  144. $data = array_merge(
  145. $this->data,
  146. ['attributes' => $this->formatHtmlAttributes()]
  147. );
  148. return view($this->view, $data)->render();
  149. }
  150. }