Radio.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. protected $style = 'primary';
  10. protected $cascadeEvent = 'change';
  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. * "info", "primary", "inverse", "danger", "success", "purple".
  27. *
  28. * @param string $style
  29. *
  30. * @return $this
  31. */
  32. public function style(string $style)
  33. {
  34. $this->style = $style;
  35. return $this;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function render()
  41. {
  42. if ($this->options instanceof \Closure) {
  43. $this->options(
  44. $this->options->call($this->values(), $this->value(), $this)
  45. );
  46. }
  47. $this->addCascadeScript();
  48. $radio = WidgetRadio::make($this->getElementName(), $this->options, $this->style);
  49. if ($this->attributes['disabled'] ?? false) {
  50. $radio->disable();
  51. }
  52. $radio
  53. ->inline()
  54. ->check(old($this->column, $this->value()))
  55. ->class($this->getElementClassString());
  56. $this->addVariables([
  57. 'radio' => $radio,
  58. ]);
  59. return parent::render();
  60. }
  61. }