Skip to content

Commit c77fbf2

Browse files
committed
添加tab表单布局,之前的已经作废
1 parent 62de2c4 commit c77fbf2

File tree

6 files changed

+113
-22
lines changed

6 files changed

+113
-22
lines changed

public/app.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/mix-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"/app.js": "/app.js?id=cac522b00977fe684e6c",
2+
"/app.js": "/app.js?id=1e7802b8e9706be294d9",
33
"/manifest.js": "/manifest.js?id=8991394a854ee5cdffc3",
44
"/vendor.js": "/vendor.js?id=df0be4950fcb717193ba"
55
}

resources/js/components/form/Form.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@
2626
:size="attrs.attrs.size"
2727
:disabled="attrs.attrs.disabled"
2828
>
29-
<component :is="attrs.attrs.hideTab ? 'div' : 'el-tabs'">
29+
<component :is="attrs.attrs.hideTab ? 'div' : 'el-tabs'" :tab-position="attrs.tabPosition">
3030
<component
3131
:is="attrs.attrs.hideTab ? 'div' : 'el-tab-pane'"
32-
:label="tab"
33-
v-for="tab in attrs.tabs"
34-
:key="tab"
32+
:label="tab.name"
33+
v-for="tab in attrs.formItemLayout"
34+
:key="tab.name"
3535
>
3636
<component
37-
v-for="(row, index) in attrs.formItemLayout"
37+
v-for="(row, index) in tab.rows"
3838
:key="index"
3939
:is="row.componentName"
4040
:attrs="row"
4141
:formData="formData"
4242
:formItems="attrs.formItems"
43-
:tab="tab"
4443
/>
4544
</component>
4645
</component>

resources/js/components/form/FormItem.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<ItemIf v-if="tab == item.tab" :form_item="item" :form_items="formItems" :form_data="formData">
2+
<div>
33
<component
44
v-if="item.topComponent"
55
:is="item.topComponent.componentName"
@@ -19,9 +19,7 @@
1919
<el-col :span="item.inputWidth">
2020
<template v-if="item.relationName">
2121
<ItemDiaplsy
22-
v-model="
23-
formData[item.relationName][item.relationValueKey]
24-
"
22+
v-model="formData[item.relationName][item.relationValueKey]"
2523
:form-item="item"
2624
:form-items="formItems"
2725
:form-data="formData"
@@ -47,7 +45,7 @@
4745
:is="item.footerComponent.componentName"
4846
:attrs="item.footerComponent"
4947
/>
50-
</ItemIf>
48+
</div>
5149
</template>
5250
<script>
5351
import ItemDiaplsy from "./ItemDiaplsy";

src/Form.php

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SmallRuralDog\Admin\Form\FormActions;
1515
use SmallRuralDog\Admin\Form\FormAttrs;
1616
use SmallRuralDog\Admin\Form\FormItem;
17+
use SmallRuralDog\Admin\Form\FormTab;
1718
use SmallRuralDog\Admin\Form\HasHooks;
1819
use SmallRuralDog\Admin\Form\HasRef;
1920
use SmallRuralDog\Admin\Form\TraitFormAttrs;
@@ -43,8 +44,8 @@ class Form extends Component
4344
protected $formItems = [];
4445
protected $formItemLayout = [];
4546
protected $ignoreEmptyProps = [];
47+
protected $tabPosition = "top";
4648

47-
protected $tabs = [];
4849

4950
const MODE_EDIT = 'edit';
5051
const MODE_CREATE = 'create';
@@ -143,11 +144,45 @@ public function row(\Closure $closure)
143144
{
144145

145146
$row = new Row();
146-
147147
call_user_func($closure, $row, $this);
148148

149-
$this->formItemLayout[] = $row;
149+
$this->tab("default", function (FormTab $formTab) use ($row) {
150+
$formTab->row($row);
151+
});
152+
153+
return $this;
154+
}
155+
156+
/**
157+
* 自定义tab布局
158+
* @param $tabName
159+
* @param \Closure $closure
160+
* @return $this
161+
*/
162+
public function tab($tabName, \Closure $closure)
163+
{
164+
165+
$tab = collect($this->formItemLayout)->filter(function (FormTab $formTab) use ($tabName) {
166+
return $formTab->getName() == $tabName;
167+
})->first();
168+
if (empty($tab)) {
169+
$tab = new FormTab($tabName, $this);
170+
call_user_func($closure, $tab, $this);
171+
$this->formItemLayout[] = $tab;
172+
} else {
173+
call_user_func($closure, $tab, $this);
174+
}
175+
return $this;
176+
}
150177

178+
/**
179+
* tab位置
180+
* @param $tabPosition
181+
* @return $this
182+
*/
183+
public function tabPosition($tabPosition)
184+
{
185+
$this->tabPosition = $tabPosition;
151186
return $this;
152187
}
153188

@@ -171,10 +206,6 @@ protected function addItem($prop, $label, $field)
171206
protected function items($items = [])
172207
{
173208

174-
$this->tabs = collect($items)->map(function (FormItem $item) {
175-
return $item->getTab();
176-
})->unique()->all();
177-
178209
$this->ignoreEmptyProps = collect($items)->filter(function (FormItem $item) {
179210
return $item->isIgnoreEmpty();
180211
})->map(function (FormItem $item) {
@@ -837,10 +868,9 @@ public function jsonSerialize()
837868
'dataUrl' => $this->dataUrl,
838869
'mode' => $this->getMode(),
839870
'attrs' => $this->attrs,
840-
//'formItems' => $this->formItemsAttr,
841871
'ignoreEmptyProps' => $this->ignoreEmptyProps,
842872
'formItemLayout' => $this->formItemLayout,
843-
'tabs' => $this->tabs,
873+
'tabPosition' => $this->tabPosition,
844874
'defaultValues' => (object)$this->formItemsValue,
845875
'formRules' => (object)$this->formRules,
846876
'ref' => $this->ref,

src/Form/FormTab.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
4+
namespace SmallRuralDog\Admin\Form;
5+
6+
7+
use SmallRuralDog\Admin\Form;
8+
use SmallRuralDog\Admin\Layout\Row;
9+
use SmallRuralDog\Admin\Traits\AdminJsonBuilder;
10+
11+
class FormTab extends AdminJsonBuilder
12+
{
13+
protected $name;
14+
protected $rows = [];
15+
protected $form;
16+
17+
public function __construct($name,Form $form)
18+
{
19+
$this->name = $name;
20+
$this->form = $form;
21+
}
22+
23+
/**
24+
* @return mixed
25+
*/
26+
public function getName()
27+
{
28+
return $this->name;
29+
}
30+
31+
/**
32+
* @param mixed $name
33+
* @return FormTab
34+
*/
35+
public function name($name)
36+
{
37+
$this->name = $name;
38+
return $this;
39+
}
40+
41+
42+
/**
43+
* @param Row|\Closure $closure
44+
* @return $this
45+
*/
46+
public function row($closure)
47+
{
48+
if ($closure instanceof \Closure) {
49+
$row = new Row();
50+
call_user_func($closure, $row, $this->form);
51+
$this->rows = collect($this->rows)->add($row);
52+
} else {
53+
$this->rows = collect($this->rows)->add($closure);
54+
}
55+
return $this;
56+
}
57+
58+
public function jsonSerialize()
59+
{
60+
return ['name' => $this->name, 'rows' => $this->rows];
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)