import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, InputNumber, Select, DatePicker } from 'antd'
|
import moment from 'moment'
|
import './index.scss'
|
|
const {MonthPicker} = DatePicker
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
action: PropTypes.object, // 搜索条件列表
|
dict: PropTypes.object, // 字典项
|
data: PropTypes.any // 表格数据
|
}
|
|
state = {
|
datatype: null,
|
formlist: []
|
}
|
|
componentDidMount () {
|
const { action } = this.props
|
let datatype = {}
|
let formlist = []
|
if (action.groups.length > 0) {
|
action.groups.forEach(group => {
|
if (group.sublist.length === 0) return
|
|
if (!group.default) {
|
formlist.push({
|
type: 'title',
|
label: group.label,
|
uuid: group.uuid
|
})
|
}
|
|
group.sublist.forEach(item => {
|
datatype[item.field] = item.type
|
formlist.push(item)
|
})
|
})
|
} else {
|
formlist = action.fields.map(item => {
|
datatype[item.field] = item.type
|
return item
|
})
|
}
|
|
this.setState({
|
datatype: datatype,
|
formlist: formlist
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
|
const fields = []
|
let cols = 2
|
if (this.props.form.setting && this.props.form.setting.cols) {
|
cols = parseInt(this.props.form.setting.cols)
|
if (cols > 3 || cols < 1) {
|
cols = 2
|
}
|
}
|
|
this.state.formlist.forEach((item, index) => {
|
if (!item.field && item.type !== 'title') return
|
|
if (item.type === 'title') {
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<p>{item.label}</p>
|
</Col>
|
)
|
} else if (item.type === 'text') {
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: this.props.data ? this.props.data[item.field] : item.initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly === 'true'} />)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'number') { // 数字
|
let min = (item.min || item.min === 0) ? item.min : -Infinity
|
let max = (item.max || item.max === 0) ? item.max : Infinity
|
let _initval = this.props.data ? this.props.data[item.field] : item.initval
|
let precision = (item.decimal || item.decimal === 0) ? item.decimal : null
|
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: _initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
}
|
]
|
})(
|
precision === null ?
|
<InputNumber initialValue={_initval} min={min} max={max} disabled={item.readonly === 'true'} /> :
|
<InputNumber initialValue={_initval} min={min} max={max} precision={precision} disabled={item.readonly === 'true'} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select') { // 下拉搜索
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: this.props.data ? this.props.data[item.field] : item.initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
getPopupContainer={() => document.getElementById('form-box')}
|
>
|
{item.options.map(option =>
|
<Select.Option id={option.key} title={option.Text} key={option.key} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'date') { // 时间搜索
|
let _initval = this.props.data ? this.props.data[item.field] : ''
|
if (_initval) {
|
_initval = moment(_initval, 'YYYY-MM-DD')
|
} else {
|
_initval = item.initval ? moment().subtract(item.initval, 'days') : null
|
}
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: _initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<DatePicker getCalendarContainer={() => document.getElementById('form-box')} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'datemonth') {
|
let _initval = this.props.data ? this.props.data[item.field] : ''
|
if (_initval) {
|
_initval = moment(_initval, 'YYYY-MM')
|
} else {
|
_initval = item.initval ? moment().subtract(item.initval, 'month') : null
|
}
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: _initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<MonthPicker getCalendarContainer={() => document.getElementById('form-box')} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'datetime') {
|
let _initval = this.props.data ? this.props.data[item.field] : ''
|
if (_initval) {
|
_initval = moment(_initval, 'YYYY-MM-DD HH:mm:ss')
|
} else {
|
_initval = item.initval ? moment().subtract(item.initval, 'days') : null
|
}
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: _initval,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<DatePicker showTime getCalendarContainer={() => document.getElementById('form-box')} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
|
return fields
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
let search = []
|
Object.keys(values).forEach(key => {
|
if (this.state.style[key] === 'datetime') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD HH:mm:ss')
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value
|
})
|
} else if (this.state.style[key] === 'datemonth') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM')
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value
|
})
|
} else if (this.state.style[key] === 'date') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD')
|
}
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: _value
|
})
|
} else {
|
search.push({
|
type: this.state.style[key],
|
key: key,
|
value: values[key].replace(/(^\s*|\s*$)/ig, '')
|
})
|
}
|
})
|
resolve(search)
|
} else {
|
reject(err)
|
}
|
})
|
})
|
}
|
|
handleReset = () => {
|
// 重置
|
this.props.form.resetFields()
|
}
|
|
render() {
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 8 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 16 }
|
}
|
}
|
return (
|
<Form {...formItemLayout} className="ant-advanced-search-form" id="form-box">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|