import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Form, Row, Col, Input, InputNumber, Select, DatePicker, notification } from 'antd'
|
import moment from 'moment'
|
import Utils from '@/utils/utils.js'
|
import './index.scss'
|
|
const {MonthPicker} = DatePicker
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
action: PropTypes.object, // 搜索条件列表
|
dict: PropTypes.object, // 字典项
|
data: PropTypes.any, // 表格数据
|
configMap: PropTypes.object
|
}
|
|
state = {
|
datatype: null,
|
readtype: null,
|
formlist: []
|
}
|
|
componentDidMount () {
|
let action = JSON.parse(JSON.stringify(this.props.action))
|
|
let datatype = {}
|
let readtype = {}
|
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
|
readtype[item.field] = item.readonly === 'true'
|
formlist.push(item)
|
})
|
})
|
} else {
|
formlist = action.fields.map(item => {
|
datatype[item.field] = item.type
|
readtype[item.field] = item.readonly === 'true'
|
|
return item
|
})
|
}
|
|
formlist = formlist.map(item => {
|
if (item.type === 'select' || item.type === 'link') {
|
if (item.setAll === 'true') {
|
item.options.unshift({
|
key: Utils.getuuid(),
|
Value: '',
|
Text: this.props.dict['main.all']
|
})
|
}
|
|
if (item.resourceType === '1' && this.props.configMap.hasOwnProperty(item.uuid)) {
|
item.options = [...item.options, ...this.props.configMap[item.uuid]]
|
}
|
|
item.oriOptions = item.options
|
}
|
|
if (!/^date/.test(item.type) && this.props.data && this.props.data.hasOwnProperty(item.field)) {
|
item.initval = this.props.data[item.field]
|
}
|
|
return item
|
})
|
|
let error = false
|
|
formlist = formlist.map(item => {
|
if (item.type === 'link') {
|
let supItem = formlist.filter(form => form.field === item.linkField)[0]
|
if (!supItem) {
|
error = true
|
} else {
|
item.options = item.oriOptions.filter(option => option.parentId === supItem.initval)
|
}
|
}
|
return item
|
})
|
|
if (error) {
|
notification.warning({
|
top: 92,
|
message: '关联菜单设置错误!',
|
duration: 10
|
})
|
}
|
|
this.setState({
|
readtype: readtype,
|
datatype: datatype,
|
formlist: formlist
|
})
|
}
|
|
resetform = (formlist, supfields, index) => {
|
index++
|
let subfields = []
|
|
supfields.forEach(supfield => {
|
formlist = formlist.map(item => {
|
if (item.type === 'link' && item.linkField === supfield.field) {
|
item.options = item.oriOptions.filter(option => option.parentId === supfield.initval)
|
item.initval = item.options[0] ? item.options[0].Value : ''
|
item.hiden = true
|
|
subfields.push(item)
|
}
|
return item
|
})
|
})
|
|
if (subfields.length === 0 || index > 6) {
|
return formlist
|
} else {
|
return this.resetform(formlist, subfields, index)
|
}
|
}
|
|
selectChange = (_field, value) => {
|
let formlist = JSON.parse(JSON.stringify(this.state.formlist))
|
|
let subfields = []
|
formlist = formlist.map(item => {
|
if (item.type === 'link' && item.linkField === _field.field) {
|
item.options = item.oriOptions.filter(option => option.parentId === value)
|
item.initval = item.options[0] ? item.options[0].Value : ''
|
item.hiden = true
|
|
subfields.push(item)
|
}
|
return item
|
})
|
|
if (subfields.length === 0) return
|
|
formlist = this.resetform(formlist, subfields, 0)
|
this.setState({
|
formlist: formlist
|
}, () => {
|
this.setState({
|
formlist: formlist.map(item => {
|
item.hiden = false
|
return item
|
})
|
})
|
})
|
}
|
|
getFields() {
|
const { getFieldDecorator } = this.props.form
|
|
const fields = []
|
let cols = 2
|
if (this.props.action.setting && this.props.action.setting.cols) {
|
cols = parseInt(this.props.action.setting.cols)
|
if (cols > 3 || cols < 1) {
|
cols = 2
|
}
|
}
|
|
this.state.formlist.forEach((item, index) => {
|
if ((!item.field && item.type !== 'title') || item.hiden) return
|
|
if (item.type === 'title') {
|
fields.push(
|
<Col span={24} 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: 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 = 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' || item.type === 'link') { // 下拉搜索
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: 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}
|
onChange={(value) => {this.selectChange(item, value)}}
|
// 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 />
|
)}
|
</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 />
|
)}
|
</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')} />
|
<DatePicker showTime />
|
)}
|
</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.datatype[key] === 'datetime') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD HH:mm:ss')
|
}
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: _value
|
})
|
} else if (this.state.datatype[key] === 'datemonth') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM')
|
}
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: _value
|
})
|
} else if (this.state.datatype[key] === 'date') {
|
let _value = ''
|
if (values[key]) {
|
_value = moment(values[key]).format('YYYY-MM-DD')
|
}
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: _value
|
})
|
} else if (this.state.datatype[key] === 'number') {
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: values[key]
|
})
|
} else {
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: values[key].replace(/(^\s*|\s*$)/ig, '')
|
// value: values[key].replace(/[\x00-\xff]+/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 main-form-field" id="form-box">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|