Checkbox.php 1.7 KB

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