UNPKG

element-ui-for-gov

Version:

element-ui for gov

234 lines (223 loc) 7 kB
import Button from 'element-ui-for-gov/packages/button'; import ELForm from 'element-ui-for-gov/packages/form/src/form'; import ElFormItem from 'element-ui-for-gov/packages/form-item'; import ElInput from 'element-ui-for-gov/packages/input'; import ElInputNumber from 'element-ui-for-gov/packages/input-number'; import ElSelect from 'element-ui-for-gov/packages/select'; import Option from 'element-ui-for-gov/packages/option'; import TimeSelect from 'element-ui-for-gov/packages/time-select'; import TimePick from 'element-ui-for-gov/packages/time-picker'; import ElDatePicker from 'element-ui-for-gov/packages/date-picker'; const QUERY_BOX_HEIGHT = '40px'; /* istanbul ignore next */ export default { name: 'QueryBox', props: { searchItems: Array, labelWidth: String, showFuzzy: { type: Boolean, default: true }, advFold: { type: Boolean, default: false }, showAdvFoldBtn: { type: Boolean, default: true }, placeholder: { type: String, default: '请输入搜索内容' }, fuzzyKey: { type: String, default: 'keyword' }, renderButtons: Function, showMoreBtn: { type: Boolean, default: true } }, components: { Button, ELForm, ElFormItem, ElInput, ElInputNumber, ElSelect, Option, ElDatePicker, TimeSelect, TimePick }, data() { return { show: this.advFold, form: { [this.fuzzyKey]: '' } }; }, mounted() { if (this.showFuzzy) return; if (this.show) { this.$refs.queryBoxBody.style.height = ''; } else { this.$refs.queryBoxBody.style.height = QUERY_BOX_HEIGHT; } }, computed: { }, methods: { search() { this.$emit('search', this.form); }, reset() { for (let key in this.form) { this.form[key] = ''; } this.$emit('reset', this.form); }, more() { if (this.show) { this.show = false; this.$refs.queryBoxBody.style.height = QUERY_BOX_HEIGHT; } else { this.show = true; this.$refs.queryBoxBody.style.height = ''; } }, onValueHandle(item, val) { console.log(item.key, val); this.form[item.key] = val; }, setKey(item) { if (this.form[item.key] === undefined) { this.$set(this.form, item.key, item.defaultValue || ''); } }, renderContent() { if (this.searchItems) { return this.searchItems.map(item => { return <ElFormItem label={item.label} labelWidth={item.labelWidth}> {this.renderItem(item)} </ElFormItem>; }); } }, renderItem(item) { this.setKey(item); const placeholder = item.placeholder; if (item.type === 'text') { return <ElInput value={this.form[item.key]} onInput={(v) => this.onValueHandle(item, v)} autocomplete="off" placeholder={placeholder}/>; } if (item.type === 'number') { return <ElInputNumber value={this.form[item.key]} controls-position="right" onChange={(v) => this.onValueHandle(item, v)} autocomplete="off"/>; } if (item.type === 'select') { return <ElSelect value={this.form[item.key]} placeholder={placeholder} onInput={(v) => this.onValueHandle(item, v)}> { item.options && item.options.map(optionItem => { return <Option key={optionItem.value} label={optionItem.label} value={optionItem.value}> </Option>; }) } </ElSelect>; } if (item.type === 'time-picker') { return <TimePick value={this.form[item.key]} type={item.dateType} onInput={(v) => this.onValueHandle(item, v)} {...item.props}> </TimePick>; } if (item.type === 'time-select') { return <TimeSelect value={this.form[item.key]} onInput={(v) => this.onValueHandle(item, v)} {...{props: item.props}}> </TimeSelect>; } if (item.type === 'date-picker') { return <ElDatePicker value={this.form[item.key]} type={item.dateType} onInput={(v) => this.onValueHandle(item, v)} {...{props: item.props}}> </ElDatePicker>; } if (typeof item.type === 'function') { // 自定义组件 return item.type(item.key, this.form, (val) => { this.form[item.key] = val; }); } } }, render(h) { const searchBtn = <Button type="primary" onClick={this.search}>搜索</Button>; const resetBtn = <Button onClick={this.reset}>重置</Button>; return ( <div class="el-query-box"> { this.showFuzzy && <div class="el-query-box--search-keyword"> <ElInput placeholder={this.placeholder} prefix-icon="sg-icon sg-icon-fangdajing" value={this.form[this.fuzzyKey]} onChange={this.search} onInput={(val) => { this.form[this.fuzzyKey] = val; }}> </ElInput> <Button type="primary" onClick={this.search}>查询</Button> { this.showAdvFoldBtn && <Button onClick={() => { this.show = !this.show; }}> 高级搜索 <i class={`el-icon--right ${this.show ? 'el-icon-arrow-up' : 'el-icon-arrow-down'}`}/> </Button> } </div> } <div style={{ display: this.show || !this.showFuzzy ? 'block' : 'none' }} class="el-query-box--body" ref="queryBoxBody"> <ELForm show={this.show} model={this.ruleForm} inline status-icon ref="ruleForm" label-width={this.labelWidth && '100px'}> { !this.showFuzzy && <div class="el-query-box--button-wrap-right "> { [ searchBtn, resetBtn, this.showMoreBtn && <Button type="text" onClick={this.more}>更多<i class={`${this.show ? 'el-icon-arrow-up' : 'el-icon-arrow-down'}`}></i></Button> ] } </div> } {this.renderContent()} { this.showFuzzy && <div class="el-query-box--button-wrap"> { this.renderButtons ? this.renderButtons(this.reset, this.search) : [ searchBtn, resetBtn ] } </div> } </ELForm> </div> </div> ); } };