Row.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Illuminate\Contracts\Support\Renderable;
  5. class Row implements Renderable
  6. {
  7. /**
  8. * Callback for add field to current row.s.
  9. *
  10. * @var \Closure
  11. */
  12. protected $callback;
  13. /**
  14. * Parent form.
  15. *
  16. * @var Form
  17. */
  18. protected $form;
  19. /**
  20. * Fields in this row.
  21. *
  22. * @var array
  23. */
  24. protected $fields = [];
  25. /**
  26. * Default field width for appended field.
  27. *
  28. * @var int
  29. */
  30. protected $defaultFieldWidth = 12;
  31. /**
  32. * Row constructor.
  33. *
  34. * @param \Closure $callback
  35. * @param Form $form
  36. */
  37. public function __construct(\Closure $callback, Form $form)
  38. {
  39. $this->callback = $callback;
  40. $this->form = $form;
  41. call_user_func($this->callback, $this);
  42. }
  43. /**
  44. * Get fields of this row.
  45. *
  46. * @return array
  47. */
  48. public function getFields()
  49. {
  50. return $this->fields;
  51. }
  52. /**
  53. * Set width for a incomming field.
  54. *
  55. * @param int $width
  56. *
  57. * @return $this
  58. */
  59. public function width($width = 12)
  60. {
  61. $this->defaultFieldWidth = $width;
  62. return $this;
  63. }
  64. /**
  65. * Render the row.
  66. *
  67. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  68. */
  69. public function render()
  70. {
  71. return view('admin::form.row', ['fields' => $this->fields]);
  72. }
  73. /**
  74. * Add field.
  75. *
  76. * @param string $method
  77. * @param array $arguments
  78. *
  79. * @return Field|void
  80. */
  81. public function __call($method, $arguments)
  82. {
  83. $field = $this->form->__call($method, $arguments);
  84. $field->disableHorizontal();
  85. $this->fields[] = [
  86. 'width' => $this->defaultFieldWidth,
  87. 'element' => $field,
  88. ];
  89. return $field;
  90. }
  91. }