Radio.php 1.8 KB

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