Row.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Illuminate\Contracts\Support\Renderable;
  5. /**
  6. * Class Row.
  7. *
  8. * @method Field\Text text($column, $label = '')
  9. * @method Field\Checkbox checkbox($column, $label = '')
  10. * @method Field\Radio radio($column, $label = '')
  11. * @method Field\Select select($column, $label = '')
  12. * @method Field\MultipleSelect multipleSelect($column, $label = '')
  13. * @method Field\Textarea textarea($column, $label = '')
  14. * @method Field\Hidden hidden($column, $label = '')
  15. * @method Field\Id id($column, $label = '')
  16. * @method Field\Ip ip($column, $label = '')
  17. * @method Field\Url url($column, $label = '')
  18. * @method Field\Email email($column, $label = '')
  19. * @method Field\Mobile mobile($column, $label = '')
  20. * @method Field\Slider slider($column, $label = '')
  21. * @method Field\Map map($latitude, $longitude, $label = '')
  22. * @method Field\Editor editor($column, $label = '')
  23. * @method Field\Date date($column, $label = '')
  24. * @method Field\Datetime datetime($column, $label = '')
  25. * @method Field\Time time($column, $label = '')
  26. * @method Field\Year year($column, $label = '')
  27. * @method Field\Month month($column, $label = '')
  28. * @method Field\DateRange dateRange($start, $end, $label = '')
  29. * @method Field\DateTimeRange datetimeRange($start, $end, $label = '')
  30. * @method Field\TimeRange timeRange($start, $end, $label = '')
  31. * @method Field\Number number($column, $label = '')
  32. * @method Field\Currency currency($column, $label = '')
  33. * @method Field\SwitchField switch ($column, $label = '')
  34. * @method Field\Display display($column, $label = '')
  35. * @method Field\Rate rate($column, $label = '')
  36. * @method Field\Divide divider()
  37. * @method Field\Password password($column, $label = '')
  38. * @method Field\Decimal decimal($column, $label = '')
  39. * @method Field\Html html($html, $label = '')
  40. * @method Field\Tags tags($column, $label = '')
  41. * @method Field\Icon icon($column, $label = '')
  42. * @method Field\Embeds embeds($column, $label = '')
  43. * @method Field\Captcha captcha()
  44. * @method Field\Listbox listbox($column, $label = '')
  45. * @method Field\SelectResource selectResource($column, $label = '')
  46. * @method Field\File file($column, $label = '')
  47. * @method Field\Image image($column, $label = '')
  48. * @method Field\MultipleFile multipleFile($column, $label = '')
  49. * @method Field\MultipleImage multipleImage($column, $label = '')
  50. * @method Field\HasMany hasMany($column, $labelOrCallback, $callback = null)
  51. * @method Field\Tree tree($column, $label = '')
  52. * @method Field\Table table($column, $labelOrCallback, $callback = null)
  53. * @method Field\ListField list($column, $label = '')
  54. * @method Field\Timezone timezone($column, $label = '')
  55. * @method Field\KeyValue keyValue($column, $label = '')
  56. * @method Field\Tel tel($column, $label = '')
  57. * @method Field\Markdown markdown($column, $label = '')
  58. */
  59. class Row implements Renderable
  60. {
  61. /**
  62. * Callback for add field to current row.s.
  63. *
  64. * @var \Closure
  65. */
  66. protected $callback;
  67. /**
  68. * Parent form.
  69. *
  70. * @var Form
  71. */
  72. protected $form;
  73. /**
  74. * Fields in this row.
  75. *
  76. * @var array
  77. */
  78. protected $fields = [];
  79. /**
  80. * Default field width for appended field.
  81. *
  82. * @var int
  83. */
  84. protected $defaultFieldWidth = 12;
  85. /**
  86. * Row constructor.
  87. *
  88. * @param \Closure $callback
  89. * @param Form $form
  90. */
  91. public function __construct(\Closure $callback, Form $form)
  92. {
  93. $this->callback = $callback;
  94. $this->form = $form;
  95. call_user_func($this->callback, $this);
  96. }
  97. /**
  98. * Get fields of this row.
  99. *
  100. * @return array
  101. */
  102. public function fields()
  103. {
  104. return $this->fields;
  105. }
  106. /**
  107. * Set width for a incomming field.
  108. *
  109. * @param int $width
  110. *
  111. * @return $this
  112. */
  113. public function width($width = 12)
  114. {
  115. $this->defaultFieldWidth = $width;
  116. return $this;
  117. }
  118. /**
  119. * Render the row.
  120. *
  121. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  122. */
  123. public function render()
  124. {
  125. return view('admin::form.row', ['fields' => $this->fields]);
  126. }
  127. /**
  128. * Add field.
  129. *
  130. * @param string $method
  131. * @param array $arguments
  132. *
  133. * @return Field|void
  134. */
  135. public function __call($method, $arguments)
  136. {
  137. $field = $this->form->__call($method, $arguments);
  138. $field->disableHorizontal();
  139. $this->fields[] = [
  140. 'width' => $this->defaultFieldWidth,
  141. 'element' => $field,
  142. ];
  143. return $field;
  144. }
  145. }