SearchForm优化

master
toesbieya 5 years ago
parent 11e138ce3f
commit 1e8d4edcb9
  1. 86
      vue/full/src/component/form/Search/index.vue

@ -1,5 +1,5 @@
<script type="text/jsx"> <script type="text/jsx">
import {deepClone} from "@/util" import {isEmpty, deepClone} from "@/util"
import {getElementInnerWidth} from '@/util/browser' import {getElementInnerWidth} from '@/util/browser'
import {clearableComponentTag, clearFormItem} from "@/util/element-ui/elForm" import {clearableComponentTag, clearFormItem} from "@/util/element-ui/elForm"
import {findComponentByTag} from "@/util/vue" import {findComponentByTag} from "@/util/vue"
@ -13,8 +13,18 @@ export default {
}, },
props: { props: {
//
model: Object, model: Object,
labelWidth: {type: String, default: '120px'}, //
defaultExpand: { type: Boolean, default: true },
// el-form
labelWidth: String,
labelPosition: { type: String, default: 'right' },
labelSuffix: { type: String, default: ':' },
// el-rowgutter
gutter: { type: Number, default: 20 },
/*每个宽度下,一行能有多少个控件,需要是24的因数*/ /*每个宽度下,一行能有多少个控件,需要是24的因数*/
xs: {type: Number, default: 1}, // <768px xs: {type: Number, default: 1}, // <768px
@ -23,12 +33,32 @@ export default {
lg: {type: Number, default: 4} // >=1200px lg: {type: Number, default: 4} // >=1200px
}, },
data() { data(vm) {
return { return {
showCollapse: false, // maxLabelLength: 0, // el-form-itemlabel10.5
collapse: true, // showCollapse: false, //
num: 0, // collapse: !vm.defaultExpand, //
span: 8 //24 num: 0, //
span: 8 // 24
}
},
computed: {
innerLabelWith() {
// 使props.labelWidth
if (!isEmpty(this.labelWidth)) return this.labelWidth
// 使label + 1+ 1
const fixedLength = isEmpty(this.labelSuffix) ? 0 : 1
return `${this.maxLabelLength + fixedLength + 1}em`
}
},
watch: {
defaultExpand: {
immediate: true,
handler(v) {
v && (this.collapse = false)
}
} }
}, },
@ -52,6 +82,9 @@ export default {
// //
this.$emit('reset', deepClone(this.initialModel)) this.$emit('reset', deepClone(this.initialModel))
}, },
handleSubmit() {
this.handleSearch()
},
getElementNumInRow() { getElementNumInRow() {
const vw = getElementInnerWidth(this.$el.parentNode) const vw = getElementInnerWidth(this.$el.parentNode)
@ -86,19 +119,21 @@ export default {
return ( return (
<div class="search-form-action"> <div class="search-form-action">
<el-form-item label-width="0">
<el-button type="primary" size="small" on-click={this.handleSearch}> </el-button> <el-button type="primary" size="small" on-click={this.handleSearch}> </el-button>
<el-button type="dashed" size="small" plain on-click={this.handleReset}> </el-button> <el-button type="dashed" size="small" plain on-click={this.handleReset}> </el-button>
{this.showCollapse && ( {this.showCollapse && (
<el-button <el-button
class="search-form-action-button"
type="text" type="text"
size="small" size="small"
style="padding-left: 0"
on-click={this.handleCollapse} on-click={this.handleCollapse}
> >
{ctrl.t} {ctrl.t}
<v-icon icon={ctrl.i} style="margin-left: 0.5em"/> <v-icon icon={ctrl.i}/>
</el-button> </el-button>
)} )}
</el-form-item>
</div> </div>
) )
} }
@ -121,7 +156,20 @@ export default {
}, },
render() { render() {
const slots = this.$slots.default, collapse = this.showCollapse && this.collapse const slots = this.$slots.default.filter(i => i.tag)
const collapse = this.showCollapse && this.collapse
this.maxLabelLength = Math.max(...slots.map(vnode => {
const label = vnode.componentOptions?.propsData?.label
if (isEmpty(label)) return 0
// labelem1em0.5em
let num = 0
for (let i = 0; i < label.length; i++) {
num += label.charCodeAt(i) > 127 ? 1 : 0.5
}
return num
}))
const display = collapse ? slots.slice(0, this.num) : slots const display = collapse ? slots.slice(0, this.num) : slots
const hidden = collapse ? slots.slice(this.num) : [] const hidden = collapse ? slots.slice(this.num) : []
@ -129,12 +177,13 @@ export default {
return ( return (
<el-form <el-form
class="search-form" class="search-form"
label-position="right" label-position={this.labelPosition}
label-width={this.labelWidth} label-width={this.innerLabelWith}
label-suffix=":" label-suffix={this.labelSuffix}
size="small" size="small"
{...{ nativeOn: { submit: this.handleSubmit } }}
> >
<el-row gutter={20}> <el-row gutter={this.gutter}>
{this.renderChildren(display)} {this.renderChildren(display)}
{this.renderChildren(hidden, true)} {this.renderChildren(hidden, true)}
{this.renderAction()} {this.renderAction()}
@ -149,6 +198,15 @@ export default {
.search-form { .search-form {
&-action { &-action {
margin-left: auto; margin-left: auto;
text-align: right;
&-button {
padding-left: 0;
.icon {
margin-left: 0.5em;
}
}
} }
> .el-row { > .el-row {

Loading…
Cancel
Save