UNPKG

yylib-quick-mobile

Version:

yylib-quick-mobile

175 lines (169 loc) 5.82 kB
import React from 'react'; import {InputItem} from 'antd-mobile'; import YYIcon from '../icon/YYIcon' import classnames from 'classnames'; import {isFunction} from '../../utils/FunctionUtil'; import YYScan from '../plugin/YYScan'; import YYVoice from '../plugin/YYVoice'; import './YYInput.less' class YYInput extends React.Component { state = { value: this.props.value } onChange = (value) => { //设值到fieldStore let fields = {}; fields[this.props.field] = { dirty: true, errors: undefined, name: this.props.field, touched: true, validating: true, value: value } this.props.form.setFields(fields); //this.setState({value}) if (isFunction(this.props.onChange)) this.props.onChange(value); } onBlur = (value) => { if (isFunction(this.props.onBlur)) this.props.onBlur(value); } onExtraClick = (e) => { if (isFunction(this.props.onExtraClick)) this.props.onExtraClick(); } scanClick = (value) =>{ var data = {value:value} if (isFunction(this.props.scanClick)) this.props.scanClick(data); let fields = {}; fields[this.props.field] = { dirty: true, errors: undefined, name: this.props.field, touched: true, validating: true, value: data.value } this.props.form.setFields(fields); } voiceClick = (value) =>{ var data = {value:value} if (isFunction(this.props.voiceClick)) this.props.voiceClick(data); let fields = {}; fields[this.props.field] = { dirty: true, errors: undefined, name: this.props.field, touched: true, validating: true, value: data.value } this.props.form.setFields(fields); } /* onClick = (value) => { if (isFunction(this.props.onClick)&&this.props.disabled) this.props.onClick(value); }*/ onFocus = (value) => { if (isFunction(this.props.onFocus)) this.props.onFocus(value); } onErrorClick = () => { if (isFunction(this.props.onErrorClick)) this.props.onErrorClick(); } validateCallBack = (rule, value, callback) => { if (isFunction(this.props.validateCallBack)) { this.props.validateCallBack(rule, value, callback); callback(); } else { callback(); } } initExtra() { let extra = []; let {openScan,openVoice,nid} = this.props; if (openScan) { extra.push(<YYScan key={nid} onClick={this.scanClick.bind(this)}/>); } if (openVoice) { extra.push(<YYVoice key={nid} onClick={this.voiceClick.bind(this)}/>); } return extra; } render() { let {getFieldProps} = this.props.form; const {field, value, required, trigger, type, placeholder, editable, disabled, clear, maxLength, hasError, extra, labelNumber, updatePlaceholder, label, showIcon, icon, iconColor,visible,openScan,openVoice,validateCallBack, findUI, offline,parentType, uiorigin, RunInDesign, uititle, uitype, uikey, nid,control_event, ...restProps} = this.props; let requiredMsg = '必填项' + label + '未填写'; let wrapClz = classnames('yy-input',(!visible&&'hidden'), this.props.className); return ( <InputItem {...restProps} className={wrapClz} {...isFunction(getFieldProps) ? getFieldProps(field, { initialValue: value, rules: [ {required, message: requiredMsg}, {validator: this.validateCallBack}, ], trigger: trigger, valuePropName: 'value', }) : null} type={type} placeholder={placeholder} editable={editable} disabled={disabled} clear={clear} maxLength={type!='phone'&&maxLength} onChange={this.onChange} onBlur={this.onBlur} onFocus={this.onFocus} error={hasError} onErrorClick={this.onErrorClick} extra={extra?extra:this.initExtra()} onExtraClick={extra?this.onExtraClick:null} labelNumber={showIcon?labelNumber+1:labelNumber} updatePlaceholder={updatePlaceholder} name={field} > {showIcon && <YYIcon type={icon} color={iconColor} style={{float:'left',paddingRight:'8px'}} size="xs"/>} {required?<span className='yy-label-required'>{label}</span>:label} </InputItem>); } } YYInput.defaultProps = { field: "default", value: null, required: false, trigger: "onChange", type: "text", placeholder: "", editable: true, disabled: false, clear: true, maxLength: null, hasError: false, extra: "", labelNumber: 7, updatePlaceholder:false, label: '输入框', showIcon: false, openScan:false, openVoice:false, icon: "Home", iconColor: "red", validateCallBack:"", visible: true, //设计器需要的props,不添加会warning findUI:'', offline:false, parentType:'', uiorigin:'', RunInDesign:false, uititle:'', uitype:'', uikey:'', nid:'', control_event:{}, } module.exports = YYInput;