You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.6 KiB

6 years ago
<script type="text/jsx">
import {getElementInnerWidth} from '@/utils/browser'
6 years ago
export default {
name: "SearchForm",
6 years ago
provide() {
return {
searchForm: this
}
},
6 years ago
props: {
labelWidth: {type: String, default: '120px'},
xs: {type: Number, default: 24},// <768px
6 years ago
sm: {type: Number, default: 12},// >=768px
md: {type: Number, default: 8}, // >=998px
lg: {type: Number, default: 6} // >=1200px
6 years ago
},
6 years ago
data() {
return {
showCollapse: false,
collapse: true,
num: 0,
width: 'auto'
6 years ago
}
},
6 years ago
methods: {
handleCollapse() {
this.collapse = !this.collapse
},
getElementNumInRow() {
const vw = getElementInnerWidth(this.$el.parentNode)
let lineNum = this.lg
if (vw < 768) lineNum = this.xs
else if (vw < 998) lineNum = this.sm
else if (vw < 1200) lineNum = this.md
return Math.floor(24 / lineNum)
6 years ago
},
resize() {
const num = this.getElementNumInRow()
this.num = num
this.width = `${100 / num}%`
6 years ago
this.showCollapse = num < this.$slots.default.length
}
},
6 years ago
mounted() {
this.resize()
window.addEventListener('resize', this.resize)
},
6 years ago
beforeDestroy() {
window.removeEventListener('resize', this.resize)
},
6 years ago
render() {
const slots = this.$slots.default
const collapseChildren = []
6 years ago
if (this.showCollapse) {
//此处直接对el-row使用v-show会无效
6 years ago
collapseChildren.push(
<el-collapse-transition>
<div v-show={!this.collapse}>
<el-row gutter={20}>
{slots.slice(this.num)}
</el-row>
</div>
</el-collapse-transition>
)
const collapseSlot = this.$scopedSlots.collapse
collapseChildren.push(
collapseSlot ? collapseSlot({collapse: this.collapse, handle: this.handleCollapse}) :
<div class="searchForm__collapse">
<el-button
type="text"
size="mini"
icon={this.collapse ? 'el-icon-arrow-down' : 'el-icon-arrow-up'}
on-click={this.handleCollapse}
>
{this.collapse ? '展开' : '收起'}
</el-button>
</div>
)
}
return (
<el-form class="searchForm" label-position="right" label-width={this.labelWidth} size="small">
<el-row gutter={20}>
{this.num > slots.length ? slots : slots.slice(0, this.num)}
</el-row>
{this.showCollapse && collapseChildren}
6 years ago
</el-form>
)
}
}
</script>
<style lang="scss">
.searchForm {
margin-bottom: -15px;
6 years ago
.searchForm__collapse {
position: relative;
bottom: 20px;
margin: 0 auto;
text-align: center;
}
}
</style>