Radio.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Form\Field;
  4. use Dcat\Admin\Support\Helper;
  5. use Dcat\Admin\Widgets\Radio as WidgetRadio;
  6. class Radio extends Field
  7. {
  8. use CanCascadeFields;
  9. use CanLoadFields;
  10. protected $style = 'primary';
  11. protected $cascadeEvent = 'change';
  12. protected $inline = true;
  13. /**
  14. * @param array|\Closure|string $options
  15. *
  16. * @return $this
  17. */
  18. public function options($options = [])
  19. {
  20. if ($options instanceof \Closure) {
  21. $this->options = $options;
  22. return $this;
  23. }
  24. $this->options = Helper::array($options);
  25. return $this;
  26. }
  27. public function inline(bool $inline)
  28. {
  29. $this->inline = $inline;
  30. return $this;
  31. }
  32. /**
  33. * "info", "primary", "inverse", "danger", "success", "purple".
  34. *
  35. * @param string $style
  36. *
  37. * @return $this
  38. */
  39. public function style(string $style)
  40. {
  41. $this->style = $style;
  42. return $this;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function render()
  48. {
  49. if ($this->options instanceof \Closure) {
  50. $this->options(
  51. $this->options->call($this->values(), $this->value(), $this)
  52. );
  53. }
  54. $this->addCascadeScript();
  55. $radio = WidgetRadio::make($this->getElementName(), $this->options, $this->style);
  56. if ($this->attributes['disabled'] ?? false) {
  57. $radio->disable();
  58. }
  59. $radio
  60. ->inline($this->inline)
  61. ->check($this->value())
  62. ->class($this->getElementClassString());
  63. $this->addVariables([
  64. 'radio' => $radio,
  65. ]);
  66. return parent::render();
  67. }
  68. }