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 { formRule } from '@/utils/option.js'
|
import Utils from '@/utils/utils.js'
|
import FileUpload from '../fileupload'
|
import './index.scss'
|
|
const {MonthPicker} = DatePicker
|
const { TextArea } = Input
|
|
class MainSearch extends Component {
|
static propTpyes = {
|
action: PropTypes.object, // 按钮信息、表单列表
|
dict: PropTypes.object, // 字典项
|
data: PropTypes.any, // 表格数据
|
BData: PropTypes.any, // 主表数据
|
configMap: PropTypes.object, // 按钮及下拉表单配置信息集
|
inputSubmit: PropTypes.func // input回车提交
|
}
|
|
state = {
|
datatype: null,
|
readtype: null,
|
formlist: []
|
}
|
|
componentDidMount () {
|
const { data, BData } = this.props
|
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
|
})
|
}
|
|
let _inputfields = formlist.filter(item => item.type === 'text' || item.type === 'number') // 用于过滤下拉菜单关联表单
|
|
formlist = formlist.map(item => {
|
if (item.type === 'select' || item.type === 'link' || item.type === 'multiselect') {
|
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 = JSON.parse(JSON.stringify(item.options))
|
|
if (item.linkSubField && item.linkSubField.length > 0) {
|
let _fields = _inputfields.map(_item => _item.field)
|
item.linkSubField = item.linkSubField.filter(_item => _fields.includes(_item))
|
}
|
}
|
|
if (item.type === 'linkMain' && BData && BData.hasOwnProperty(item.field)) {
|
item.initval = BData[item.field]
|
} else 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 && data && data.hasOwnProperty(item.linkField)) {
|
supItem = {initval: data[item.linkField]}
|
}
|
|
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
|
}, () => {
|
if (action.setting && action.setting.focus) {
|
try {
|
let _form = document.getElementById('main-form-box')
|
let _item = _form.getElementsByTagName('input')
|
_item = [..._item]
|
_item.forEach(input => {
|
if (!input || input.id !== action.setting.focus) return
|
input.select()
|
})
|
} catch {
|
console.warn('表单获取失败!')
|
}
|
}
|
})
|
}
|
|
resetform = (formlist, supfields, index, fieldsvalue) => {
|
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 : ''
|
|
fieldsvalue[item.field] = item.initval
|
|
subfields.push(item)
|
}
|
return item
|
})
|
})
|
|
if (subfields.length === 0 || index > 6) {
|
this.props.form.setFieldsValue(fieldsvalue)
|
return formlist
|
} else {
|
return this.resetform(formlist, subfields, index, fieldsvalue)
|
}
|
}
|
|
selectChange = (_field, value, option) => {
|
let formlist = JSON.parse(JSON.stringify(this.state.formlist))
|
|
let subfields = []
|
let fieldsvalue = {}
|
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 : ''
|
|
fieldsvalue[item.field] = item.initval
|
|
subfields.push(item)
|
}
|
return item
|
})
|
|
// 表单切换时,更新关联字段
|
if (_field.type === 'select' && _field.linkSubField && _field.linkSubField.length > 0 && option.props.data) {
|
let _data = option.props.data
|
let fieldVal = {}
|
_field.linkSubField.forEach(subfield => {
|
fieldVal[subfield] = _data[subfield]
|
})
|
this.props.form.setFieldsValue(fieldVal)
|
}
|
|
if (subfields.length === 0) return
|
|
formlist = this.resetform(formlist, subfields, 0, fieldsvalue)
|
|
this.setState({
|
formlist: formlist
|
})
|
}
|
|
// handleInputNumber = (rule, value, callback, item) => {
|
// if (item.required === 'true' && (!value && value !== 0)) {
|
// callback(this.props.dict['form.required.input'] + item.label + '!')
|
// } else if ((item.min || item.min === 0) && (value || value === 0) && value < item.min) {
|
// callback(item.label + '最小值为' + item.min + '!')
|
// } else if ((item.max || item.max === 0) && (value || value === 0) && value > item.max) {
|
// callback(item.label + '最大值为' + item.max + '!')
|
// }
|
// }
|
|
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.hidden === 'true') 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 + '!'
|
},
|
{
|
max: formRule.input.max,
|
message: formRule.input.message
|
}
|
]
|
})(<Input placeholder="" autoComplete="off" disabled={item.readonly === 'true'} onPressEnter={this.handleSubmit} />)}
|
</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 min={min} max={max} disabled={item.readonly === 'true'} onPressEnter={this.handleSubmit} /> :
|
<InputNumber min={min} max={max} precision={precision} disabled={item.readonly === 'true'} onPressEnter={this.handleSubmit} />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'select' || item.type === 'link') { // 下拉搜索
|
let hasSubField = false
|
if (item.linkSubField && item.linkSubField.length > 0) { // 存在关联字段,数据存储
|
hasSubField = true
|
}
|
|
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, option) => {this.selectChange(item, value, option)}}
|
>
|
{item.options.map(option =>
|
<Select.Option id={option.key} data={hasSubField ? option : ''} title={option.Text} key={option.key} value={option.Value}>{option.Text}</Select.Option>
|
)}
|
</Select>
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'multiselect') { // 多选
|
let _initval = item.initval ? item.initval.split(',').filter(Boolean) : []
|
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 + '!'
|
}
|
]
|
})(
|
<Select
|
showSearch
|
mode="multiple"
|
filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
|
>
|
{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>
|
)
|
} else if (item.type === 'fileupload') {
|
let filelist = this.props.data ? this.props.data[item.field] : item.initval
|
if (filelist) {
|
try {
|
filelist = filelist.split(',').map((url, index) => {
|
return {
|
uid: `${index}`,
|
name: url.slice(url.lastIndexOf('/') + 1),
|
status: 'done',
|
url: url,
|
origin: true
|
}
|
})
|
} catch {
|
filelist = []
|
}
|
}
|
|
fields.push(
|
<Col span={24 / cols} key={index}>
|
<Form.Item label={item.label}>
|
{getFieldDecorator(item.field, {
|
initialValue: filelist,
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.select'] + item.label + '!'
|
}
|
]
|
})(
|
<FileUpload />
|
)}
|
</Form.Item>
|
</Col>
|
)
|
} else if (item.type === 'linkMain') {
|
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 === 'funcvar') {
|
// fields.push(
|
// <Col span={24 / cols} key={index}>
|
// <Form.Item label={item.label}>
|
// {getFieldDecorator(item.field, {
|
// initialValue: item.linkfield || '',
|
// })(<Input placeholder="" autoComplete="off" disabled={item.readonly === 'true'} />)}
|
// </Form.Item>
|
// </Col>
|
// )
|
} else if (item.type === 'textarea') {
|
let _labelcol = cols !== 3 ? 8 / cols : 3
|
let _wrapcol = cols !== 3 ? 16 + (cols - 1) * 4 : 21
|
let _style = {}
|
if (cols === 2) {
|
_style.paddingLeft = '7px'
|
}
|
fields.push(
|
<Col span={24} key={index} className="textarea-row" style={{..._style}}>
|
<Form.Item label={item.label} labelCol={{xs: { span: 24 }, sm: { span: _labelcol }}} wrapperCol={ {xs: { span: 24 }, sm: { span: _wrapcol }} }>
|
{getFieldDecorator(item.field, {
|
initialValue: item.initval || '',
|
rules: [
|
{
|
required: item.required === 'true',
|
message: this.props.dict['form.required.input'] + item.label + '!'
|
},
|
{
|
max: formRule.textarea.max,
|
message: formRule.textarea.message
|
}
|
]
|
})(<TextArea autosize={{ minRows: 2, maxRows: 6 }} disabled={item.readonly === 'true'} />)}
|
</Form.Item>
|
</Col>
|
)
|
}
|
})
|
|
return fields
|
}
|
|
handleConfirm = () => {
|
// 表单提交时检查输入值是否正确
|
return new Promise((resolve, reject) => {
|
this.props.form.validateFieldsAndScroll((err, values) => {
|
if (!err) {
|
let search = []
|
// 隐藏表单
|
this.state.formlist.forEach(item => {
|
if (!item.field) return
|
|
if (item.type === 'funcvar') {
|
search.push({
|
type: 'funcvar',
|
readonly: 'true',
|
key: item.field,
|
value: ''
|
})
|
} else if (item.hidden === 'true') {
|
search.push({
|
type: this.state.datatype[item.field],
|
readonly: this.state.readtype[item.field],
|
key: item.field,
|
value: item.initval
|
})
|
}
|
})
|
|
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 if (this.state.datatype[key] === 'multiselect') {
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: values[key] ? values[key].join(',') : ''
|
})
|
} else if (this.state.datatype[key] === 'fileupload') {
|
let vals = []
|
|
if (values[key] && values[key].length > 0) {
|
values[key].forEach(_val => {
|
if (_val.origin && _val.url) {
|
vals.push(_val.url)
|
} else if (!_val.origin && _val.status === 'done' && _val.response) {
|
vals.push(Utils.getrealurl(_val.response))
|
}
|
})
|
}
|
|
search.push({
|
type: this.state.datatype[key],
|
readonly: this.state.readtype[key],
|
key: key,
|
value: vals.join(',')
|
})
|
} else if (this.state.datatype[key] === 'funcvar') {
|
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)
|
}
|
})
|
})
|
}
|
|
handleSubmit = (e) => {
|
e.preventDefault()
|
this.props.inputSubmit()
|
}
|
|
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="main-form-box">
|
<Row gutter={24}>{this.getFields()}</Row>
|
</Form>
|
)
|
}
|
}
|
|
export default Form.create()(MainSearch)
|