Radio.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. class Radio extends Field
  6. {
  7. protected $inline = true;
  8. protected $style = 'primary';
  9. /**
  10. * Set options.
  11. *
  12. * @param array|\Closure|string $options
  13. *
  14. * @return $this
  15. */
  16. public function options($options = [])
  17. {
  18. if ($options instanceof \Closure) {
  19. $this->options = $options;
  20. return $this;
  21. }
  22. $this->options = Helper::array($options);
  23. return $this;
  24. }
  25. /**
  26. * Draw inline radios.
  27. *
  28. * @return $this
  29. */
  30. public function inline()
  31. {
  32. $this->inline = true;
  33. return $this;
  34. }
  35. /**
  36. * Draw stacked radios.
  37. *
  38. * @return $this
  39. */
  40. public function stacked()
  41. {
  42. $this->inline = false;
  43. return $this;
  44. }
  45. /**
  46. * "info", "primary", "inverse", "danger", "success", "purple".
  47. *
  48. * @param string $v
  49. *
  50. * @return $this
  51. */
  52. public function style($v)
  53. {
  54. $this->style = $v;
  55. return $this;
  56. }
  57. /**
  58. * Set options.
  59. *
  60. * @param array|callable|string $values
  61. *
  62. * @return $this
  63. */
  64. public function values($values)
  65. {
  66. return $this->options($values);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function render()
  72. {
  73. if ($this->options instanceof \Closure) {
  74. $this->options(
  75. $this->options->call($this->getFormModel(), $this->value(), $this)
  76. );
  77. }
  78. $this->addVariables([
  79. 'options' => $this->options,
  80. 'inline' => $this->inline,
  81. 'radioStyle' => $this->style,
  82. ]);
  83. return parent::render();
  84. }
  85. }