Radio.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. class Radio extends Widget
  5. {
  6. protected $view = 'admin::widgets.radio';
  7. protected $type = 'radio';
  8. protected $style = 'primary';
  9. protected $right = '14px';
  10. protected $checked;
  11. protected $disabledValues = [];
  12. protected $size;
  13. protected $inline = false;
  14. public function __construct(
  15. ?string $name = null,
  16. array $options = [],
  17. string $style = 'primary'
  18. ) {
  19. $this->name($name);
  20. $this->options($options);
  21. $this->style($style);
  22. }
  23. /**
  24. * 设置表单 "name" 属性.
  25. *
  26. * @param string $name
  27. *
  28. * @return $this
  29. */
  30. public function name(?string $name)
  31. {
  32. return $this->setHtmlAttribute('name', $name);
  33. }
  34. /**
  35. * 设置为小尺寸.
  36. *
  37. * @return $this
  38. */
  39. public function small()
  40. {
  41. return $this->size('sm');
  42. }
  43. /**
  44. * 设置为大尺寸.
  45. *
  46. * @return $this
  47. */
  48. public function large()
  49. {
  50. return $this->size('lg');
  51. }
  52. /**
  53. * 尺寸设置.
  54. *
  55. * "sm", "lg"
  56. *
  57. * @param string $size
  58. *
  59. * @return $this
  60. */
  61. public function size(string $size)
  62. {
  63. $this->size = $size;
  64. return $this;
  65. }
  66. /**
  67. * 是否排成一行.
  68. *
  69. * @param bool $inine
  70. *
  71. * @return $this
  72. */
  73. public function inline(bool $inine = true)
  74. {
  75. $this->inline = $inine;
  76. return $this;
  77. }
  78. /**
  79. * 设置禁选的选项.
  80. *
  81. * @param string|array $values
  82. *
  83. * @return $this
  84. */
  85. public function disable($values = null)
  86. {
  87. if ($values) {
  88. $this->disabledValues = (array) $values;
  89. return $this;
  90. }
  91. return $this->setHtmlAttribute('disabled', 'disabled');
  92. }
  93. /**
  94. * 设置 "margin-right" 样式.
  95. *
  96. * @param string $value
  97. *
  98. * @return $this
  99. */
  100. public function right(string $value)
  101. {
  102. $this->right = $value;
  103. return $this;
  104. }
  105. /**
  106. * 设置选中的选项.
  107. *
  108. * @param string $id
  109. *
  110. * @return $this
  111. */
  112. public function check($option)
  113. {
  114. $this->checked = $option;
  115. return $this;
  116. }
  117. /**
  118. * 设置选项的名称和值.
  119. *
  120. * eg: $opts = [
  121. * 1 => 'foo',
  122. * 2 => 'bar',
  123. * ...
  124. * ]
  125. *
  126. * @param array $opts
  127. *
  128. * @return $this
  129. */
  130. public function options($opts = [])
  131. {
  132. if ($opts instanceof Arrayable) {
  133. $opts = $opts->toArray();
  134. }
  135. $this->options = $opts;
  136. return $this;
  137. }
  138. /**
  139. * 设置样式.
  140. *
  141. * 支持 "info", "primary", "danger", "success".
  142. *
  143. * @param string $style
  144. *
  145. * @return $this
  146. */
  147. public function style(string $style)
  148. {
  149. $this->style = $style;
  150. return $this;
  151. }
  152. /**
  153. * @return array
  154. */
  155. public function variables()
  156. {
  157. return [
  158. 'style' => $this->style,
  159. 'options' => $this->options,
  160. 'attributes' => $this->formatHtmlAttributes(),
  161. 'checked' => $this->checked,
  162. 'disabled' => $this->disabledValues,
  163. 'right' => $this->right,
  164. 'size' => $this->size,
  165. 'inline' => $this->inline,
  166. ];
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function render()
  172. {
  173. $this->setHtmlAttribute('type', $this->type);
  174. return parent::render();
  175. }
  176. }