Skip to content

Commit

Permalink
添加tab表单布局,之前的已经作废
Browse files Browse the repository at this point in the history
  • Loading branch information
yidalh committed Sep 25, 2020
1 parent 62de2c4 commit c77fbf2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
2 changes: 1 addition & 1 deletion public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/app.js": "/app.js?id=cac522b00977fe684e6c",
"/app.js": "/app.js?id=1e7802b8e9706be294d9",
"/manifest.js": "/manifest.js?id=8991394a854ee5cdffc3",
"/vendor.js": "/vendor.js?id=df0be4950fcb717193ba"
}
11 changes: 5 additions & 6 deletions resources/js/components/form/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,20 @@
:size="attrs.attrs.size"
:disabled="attrs.attrs.disabled"
>
<component :is="attrs.attrs.hideTab ? 'div' : 'el-tabs'">
<component :is="attrs.attrs.hideTab ? 'div' : 'el-tabs'" :tab-position="attrs.tabPosition">
<component
:is="attrs.attrs.hideTab ? 'div' : 'el-tab-pane'"
:label="tab"
v-for="tab in attrs.tabs"
:key="tab"
:label="tab.name"
v-for="tab in attrs.formItemLayout"
:key="tab.name"
>
<component
v-for="(row, index) in attrs.formItemLayout"
v-for="(row, index) in tab.rows"
:key="index"
:is="row.componentName"
:attrs="row"
:formData="formData"
:formItems="attrs.formItems"
:tab="tab"
/>
</component>
</component>
Expand Down
8 changes: 3 additions & 5 deletions resources/js/components/form/FormItem.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<ItemIf v-if="tab == item.tab" :form_item="item" :form_items="formItems" :form_data="formData">
<div>
<component
v-if="item.topComponent"
:is="item.topComponent.componentName"
Expand All @@ -19,9 +19,7 @@
<el-col :span="item.inputWidth">
<template v-if="item.relationName">
<ItemDiaplsy
v-model="
formData[item.relationName][item.relationValueKey]
"
v-model="formData[item.relationName][item.relationValueKey]"
:form-item="item"
:form-items="formItems"
:form-data="formData"
Expand All @@ -47,7 +45,7 @@
:is="item.footerComponent.componentName"
:attrs="item.footerComponent"
/>
</ItemIf>
</div>
</template>
<script>
import ItemDiaplsy from "./ItemDiaplsy";
Expand Down
48 changes: 39 additions & 9 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SmallRuralDog\Admin\Form\FormActions;
use SmallRuralDog\Admin\Form\FormAttrs;
use SmallRuralDog\Admin\Form\FormItem;
use SmallRuralDog\Admin\Form\FormTab;
use SmallRuralDog\Admin\Form\HasHooks;
use SmallRuralDog\Admin\Form\HasRef;
use SmallRuralDog\Admin\Form\TraitFormAttrs;
Expand Down Expand Up @@ -43,8 +44,8 @@ class Form extends Component
protected $formItems = [];
protected $formItemLayout = [];
protected $ignoreEmptyProps = [];
protected $tabPosition = "top";

protected $tabs = [];

const MODE_EDIT = 'edit';
const MODE_CREATE = 'create';
Expand Down Expand Up @@ -143,11 +144,45 @@ public function row(\Closure $closure)
{

$row = new Row();

call_user_func($closure, $row, $this);

$this->formItemLayout[] = $row;
$this->tab("default", function (FormTab $formTab) use ($row) {
$formTab->row($row);
});

return $this;
}

/**
* 自定义tab布局
* @param $tabName
* @param \Closure $closure
* @return $this
*/
public function tab($tabName, \Closure $closure)
{

$tab = collect($this->formItemLayout)->filter(function (FormTab $formTab) use ($tabName) {
return $formTab->getName() == $tabName;
})->first();
if (empty($tab)) {
$tab = new FormTab($tabName, $this);
call_user_func($closure, $tab, $this);
$this->formItemLayout[] = $tab;
} else {
call_user_func($closure, $tab, $this);
}
return $this;
}

/**
* tab位置
* @param $tabPosition
* @return $this
*/
public function tabPosition($tabPosition)
{
$this->tabPosition = $tabPosition;
return $this;
}

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

$this->tabs = collect($items)->map(function (FormItem $item) {
return $item->getTab();
})->unique()->all();

$this->ignoreEmptyProps = collect($items)->filter(function (FormItem $item) {
return $item->isIgnoreEmpty();
})->map(function (FormItem $item) {
Expand Down Expand Up @@ -837,10 +868,9 @@ public function jsonSerialize()
'dataUrl' => $this->dataUrl,
'mode' => $this->getMode(),
'attrs' => $this->attrs,
//'formItems' => $this->formItemsAttr,
'ignoreEmptyProps' => $this->ignoreEmptyProps,
'formItemLayout' => $this->formItemLayout,
'tabs' => $this->tabs,
'tabPosition' => $this->tabPosition,
'defaultValues' => (object)$this->formItemsValue,
'formRules' => (object)$this->formRules,
'ref' => $this->ref,
Expand Down
64 changes: 64 additions & 0 deletions src/Form/FormTab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php


namespace SmallRuralDog\Admin\Form;


use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Layout\Row;
use SmallRuralDog\Admin\Traits\AdminJsonBuilder;

class FormTab extends AdminJsonBuilder
{
protected $name;
protected $rows = [];
protected $form;

public function __construct($name,Form $form)
{
$this->name = $name;
$this->form = $form;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $name
* @return FormTab
*/
public function name($name)
{
$this->name = $name;
return $this;
}


/**
* @param Row|\Closure $closure
* @return $this
*/
public function row($closure)
{
if ($closure instanceof \Closure) {
$row = new Row();
call_user_func($closure, $row, $this->form);
$this->rows = collect($this->rows)->add($row);
} else {
$this->rows = collect($this->rows)->add($closure);
}
return $this;
}

public function jsonSerialize()
{
return ['name' => $this->name, 'rows' => $this->rows];
}


}

0 comments on commit c77fbf2

Please sign in to comment.