优化公用Form报错
This commit is contained in:
parent
b884dc68eb
commit
d90f8b6dc4
|
@ -1,10 +1,10 @@
|
|||
import React from 'react'
|
||||
import './main.less'
|
||||
import { Icon } from 'zent'
|
||||
import _ from 'lodash'
|
||||
import Menu from '../menu/main.js'
|
||||
import ReactDom from 'react-dom'
|
||||
import Bus from '../../assets/eventBus.js'
|
||||
import React from "react"
|
||||
import "./main.less"
|
||||
import { Icon } from "zent"
|
||||
import _ from "lodash"
|
||||
import Menu from "../menu/main.js"
|
||||
import ReactDom from "react-dom"
|
||||
import Bus from "../../assets/eventBus.js"
|
||||
|
||||
export default class form extends React.Component {
|
||||
constructor(props) {
|
||||
|
@ -22,9 +22,9 @@ export default class form extends React.Component {
|
|||
this.setState({ rules: nextProps.rules })
|
||||
}
|
||||
componentDidMount() {
|
||||
Bus.addListener('change', (prop, e) => {
|
||||
Bus.addListener("change", (prop, e) => {
|
||||
let dom = document.getElementById(prop)
|
||||
let formCompontent = dom.getElementsByClassName('form-compontent')
|
||||
let formCompontent = dom.getElementsByClassName("form-compontent")
|
||||
let rules_item = _.get(this.props.rules, prop)
|
||||
|
||||
let item = formCompontent[0].firstChild
|
||||
|
@ -33,7 +33,7 @@ export default class form extends React.Component {
|
|||
} else {
|
||||
setTimeout(() => {
|
||||
this.isRequired(
|
||||
'',
|
||||
"",
|
||||
item.className,
|
||||
formCompontent[0],
|
||||
rules_item[0].message
|
||||
|
@ -43,9 +43,9 @@ export default class form extends React.Component {
|
|||
})
|
||||
|
||||
let self = this
|
||||
Bus.addListener('input', (prop, e) => {
|
||||
Bus.addListener("input", (prop, e) => {
|
||||
let dom = document.getElementById(prop)
|
||||
let formCompontent = dom.getElementsByClassName('form-compontent')
|
||||
let formCompontent = dom.getElementsByClassName("form-compontent")
|
||||
let rules_item = _.get(this.props.rules, prop)
|
||||
let item = formCompontent[0].firstChild
|
||||
if (!rules_item) {
|
||||
|
@ -53,8 +53,8 @@ export default class form extends React.Component {
|
|||
}
|
||||
|
||||
for (let j = 0; j < rules_item.length; j++) {
|
||||
if (rules_item[j].type == 'required') {
|
||||
if (e == '') {
|
||||
if (rules_item[j].type == "required") {
|
||||
if (e == "") {
|
||||
self.removeClass(formCompontent[0])
|
||||
self.isRequired(
|
||||
e,
|
||||
|
@ -68,7 +68,7 @@ export default class form extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
if (rules_item[j].type == 'regExp') {
|
||||
if (rules_item[j].type == "regExp") {
|
||||
var regu = rules_item[j].reg
|
||||
var re = new RegExp(regu)
|
||||
let s = e
|
||||
|
@ -86,14 +86,14 @@ export default class form extends React.Component {
|
|||
}
|
||||
})
|
||||
|
||||
Bus.addListener('close', (prop) => {
|
||||
Bus.addListener("close", (prop) => {
|
||||
let dom = document.getElementById(prop)
|
||||
let formCompontent = dom.getElementsByClassName('form-compontent')
|
||||
let formCompontent = dom.getElementsByClassName("form-compontent")
|
||||
let rules_item = _.get(this.props.rules, prop)
|
||||
let item = formCompontent[0].firstChild
|
||||
setTimeout(() => {
|
||||
this.isRequired(
|
||||
'',
|
||||
"",
|
||||
item.className,
|
||||
formCompontent[0],
|
||||
rules_item[0].message
|
||||
|
@ -102,7 +102,7 @@ export default class form extends React.Component {
|
|||
})
|
||||
|
||||
const dom = ReactDom.findDOMNode(this)
|
||||
let doms = dom.getElementsByClassName('form-Item')
|
||||
let doms = dom.getElementsByClassName("form-Item")
|
||||
|
||||
let props_com = _.filter(this.props.children, (item) => {
|
||||
return item != null
|
||||
|
@ -118,23 +118,23 @@ export default class form extends React.Component {
|
|||
|
||||
if (_.isArray(formItem)) {
|
||||
} else {
|
||||
let formItem_com = _.get(formItem, 'props.children.props')
|
||||
let formItem_com = _.get(formItem, "props.children.props")
|
||||
|
||||
let input_compontent = com[1].children[0].getElementsByTagName('input')
|
||||
let input_compontent = com[1].children[0].getElementsByTagName("input")
|
||||
if (input_compontent.length == 0) {
|
||||
input_compontent = com[1].children[0].getElementsByTagName('textarea')
|
||||
input_compontent = com[1].children[0].getElementsByTagName("textarea")
|
||||
}
|
||||
|
||||
let o = com[1].children[0]
|
||||
let rules_item = ''
|
||||
let rules_item = ""
|
||||
if (formItem?.props?.prop) {
|
||||
rules_item = _.get(self.props.rules, formItem.props.prop)
|
||||
}
|
||||
if (input_compontent.length > 0 && rules_item) {
|
||||
input_compontent[0].oninput = function (e) {
|
||||
for (let j = 0; j < rules_item.length; j++) {
|
||||
if (rules_item[j].type == 'required') {
|
||||
if (e.target.value == '') {
|
||||
if (rules_item[j].type == "required") {
|
||||
if (e.target.value == "") {
|
||||
self.removeClass(item)
|
||||
if (formItem_com) {
|
||||
self.isRequired(
|
||||
|
@ -150,7 +150,7 @@ export default class form extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
if (rules_item[j].type == 'regExp') {
|
||||
if (rules_item[j].type == "regExp") {
|
||||
var regu = rules_item[j].reg
|
||||
var re = new RegExp(regu)
|
||||
let s = e.target.value
|
||||
|
@ -172,8 +172,8 @@ export default class form extends React.Component {
|
|||
|
||||
input_compontent[0].onblur = function (e) {
|
||||
for (let j = 0; j < rules_item.length; j++) {
|
||||
if (e.target.value == '') {
|
||||
if (rules_item[j].type == 'required') {
|
||||
if (e.target.value == "") {
|
||||
if (rules_item[j].type == "required") {
|
||||
self.removeClass(item)
|
||||
if (formItem_com) {
|
||||
self.isRequired(
|
||||
|
@ -185,7 +185,7 @@ export default class form extends React.Component {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
if (rules_item[j].type == 'regExp') {
|
||||
if (rules_item[j].type == "regExp") {
|
||||
var regu = rules_item[j].reg
|
||||
var re = new RegExp(regu)
|
||||
let s = e.target.value
|
||||
|
@ -213,40 +213,40 @@ export default class form extends React.Component {
|
|||
}
|
||||
removeClass(compontent) {
|
||||
var str = compontent.firstChild.className
|
||||
let errors = compontent.getElementsByClassName('error-msg')
|
||||
let errors = compontent.getElementsByClassName("error-msg")
|
||||
|
||||
while (errors.length > 0) {
|
||||
errors[0].remove()
|
||||
}
|
||||
let index = str.indexOf('error')
|
||||
let index = str.indexOf("error")
|
||||
if (index > -1) {
|
||||
compontent.firstChild.className = str.replace(' error', '')
|
||||
compontent.firstChild.className = str.replace(" error", "")
|
||||
}
|
||||
}
|
||||
|
||||
isRequired(value, className, compontent, message) {
|
||||
if (
|
||||
compontent.firstChild.className.indexOf('error') < 0 &&
|
||||
compontent.lastChild.className.indexOf('error-msg') < 0
|
||||
compontent.firstChild.className.indexOf("error") < 0 &&
|
||||
compontent.lastChild.className.indexOf("error-msg") < 0
|
||||
) {
|
||||
var error = document.createElement('div')
|
||||
error.className = 'error-msg'
|
||||
error.innerHTML = '<span>' + message + '</span>'
|
||||
compontent.firstChild.className += ' error'
|
||||
var error = document.createElement("div")
|
||||
error.className = "error-msg"
|
||||
error.innerHTML = "<span>" + message + "</span>"
|
||||
compontent.firstChild.className += " error"
|
||||
compontent.appendChild(error)
|
||||
}
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps, prevState) {
|
||||
let formItem = this.props.children[0]
|
||||
let com = _.get(formItem, 'props.children.props')
|
||||
let com = _.get(formItem, "props.children.props")
|
||||
|
||||
if (prevState.children != this.state.children) {
|
||||
}
|
||||
}
|
||||
|
||||
cancel() {
|
||||
let doms = document.getElementsByClassName('form-Item')
|
||||
let doms = document.getElementsByClassName("form-Item")
|
||||
let self = this
|
||||
for (let i = 0; i < doms.length; i++) {
|
||||
var com = doms[i].children
|
||||
|
@ -254,7 +254,7 @@ export default class form extends React.Component {
|
|||
let formItem = this.props.children[i]
|
||||
let o = com[1].children[0]
|
||||
//formItem对象
|
||||
let formItem_com = _.get(formItem, 'props.children.props')
|
||||
let formItem_com = _.get(formItem, "props.children.props")
|
||||
//对应的验证规则
|
||||
let rules_item = _.get(this.props.rules, formItem.props.prop)
|
||||
if (rules_item) {
|
||||
|
@ -271,7 +271,7 @@ export default class form extends React.Component {
|
|||
|
||||
validator() {
|
||||
const dom = ReactDom.findDOMNode(this)
|
||||
let doms = dom.getElementsByClassName('form-Item')
|
||||
let doms = dom.getElementsByClassName("form-Item")
|
||||
let self = this
|
||||
let validate = true
|
||||
|
||||
|
@ -298,14 +298,14 @@ export default class form extends React.Component {
|
|||
if (_.isArray(formItem)) {
|
||||
let op = 1
|
||||
for (let k = 0; k < formItem.length; k++) {
|
||||
let formItem_com = _.get(formItem[k], 'props.children.props')
|
||||
let formItem_com = _.get(formItem[k], "props.children.props")
|
||||
//对应的验证规则
|
||||
let rules_item = _.get(this.props.rules, formItem[k].props.prop)
|
||||
if (rules_item) {
|
||||
for (let j = 0; j < rules_item.length; j++) {
|
||||
if (rules_item[j].type == 'required') {
|
||||
if (rules_item[j].type == "required") {
|
||||
if (
|
||||
formItem_com.value == '' ||
|
||||
formItem_com.value == "" ||
|
||||
formItem_com.value == undefined
|
||||
) {
|
||||
self.removeClass(item)
|
||||
|
@ -322,7 +322,7 @@ export default class form extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
if (rules_item[j].type == 'regExp') {
|
||||
if (rules_item[j].type == "regExp") {
|
||||
var regu = rules_item[j].reg
|
||||
var re = new RegExp(regu)
|
||||
let s = formItem_com.value
|
||||
|
@ -344,28 +344,28 @@ export default class form extends React.Component {
|
|||
}
|
||||
} else {
|
||||
//formItem对象
|
||||
let formItem_com = _.get(formItem, 'props.children.props')
|
||||
let formItem_com = _.get(formItem, "props.children.props")
|
||||
|
||||
if (formItem_com && formItem_com.hasOwnProperty('children')) {
|
||||
if (formItem_com && formItem_com.hasOwnProperty("children")) {
|
||||
if (_.isArray(formItem_com.children)) {
|
||||
formItem_com = formItem_com.children[0].props
|
||||
}
|
||||
//
|
||||
} else {
|
||||
let coms = _.get(formItem, 'props.children')
|
||||
let coms = _.get(formItem, "props.children")
|
||||
if (coms && _.isArray(coms)) {
|
||||
formItem_com = coms[0].props
|
||||
}
|
||||
}
|
||||
|
||||
//对应的验证规则
|
||||
let rules_item = _.get(this.props.rules, formItem.props.prop)
|
||||
let rules_item = _.get(this.props.rules, formItem?.props?.prop)
|
||||
|
||||
if (rules_item) {
|
||||
for (let j = 0; j < rules_item.length; j++) {
|
||||
if (rules_item[j].type == 'required') {
|
||||
if (formItem_com.value == '' || formItem_com.value == undefined) {
|
||||
let node = item.getElementsByTagName('input')
|
||||
if (rules_item[j].type == "required") {
|
||||
if (formItem_com.value == "" || formItem_com.value == undefined) {
|
||||
let node = item.getElementsByTagName("input")
|
||||
if (node && node.length > 0) {
|
||||
node[0].focus()
|
||||
}
|
||||
|
@ -384,7 +384,7 @@ export default class form extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
if (rules_item[j].type == 'regExp') {
|
||||
if (rules_item[j].type == "regExp") {
|
||||
var regu = rules_item[j].reg
|
||||
var re = new RegExp(regu)
|
||||
let s = formItem_com.value
|
||||
|
|
|
@ -1193,7 +1193,7 @@ export default class exchangeAdd extends React.Component {
|
|||
<FormItem labelname='发放总量' prop='issued' id='issued'>
|
||||
<div className='line'>{this.state.codeInfo.issued}</div>
|
||||
</FormItem>
|
||||
<FormItem labelname='剩余库存量' prop='issued' id='issued'>
|
||||
<FormItem labelname='剩余库存量' prop='stock' id='stock'>
|
||||
<div className='line'>{this.state.codeInfo.stock}</div>
|
||||
</FormItem>
|
||||
</>
|
||||
|
|
Loading…
Reference in New Issue