import React, { Component } from 'react' import { View, Text, StyleSheet, TouchableHighlight, TextInput, ScrollView, Animated, Easing, KeyboardAvoidingView, TouchableOpacity, Image } from 'react-native' import LoadingKit from '../../components/loading/LoadingKit'; import mockAction from './../../sevices/mockAction'; import { px2dp, onePx, colors, toastShort, isEmpty, filterSqlRows, filterEmoji } from './../../utils'; import redux from './../../redux/store'; import { BjTypeAppConfig, BjTypeResearchNameDict, BjTypeUserMsg } from '../../utils/types'; import API from '../../api'; import { adaptAPIAndName } from '../../utils/adapt'; import { setAppConfig } from '../../redux/actiions'; interface Props { type?: BjTypeResearchNameDict code?: string serialNumber?: string onConfirm?: Function, title?: string leftTitle?: string rightTitle?: string existText?: string leftPlaceHolder?: string rightPlaceHolder?: string notCheck?: boolean settingMode?: boolean }; interface State { opacity: any code: string loading: boolean codeExist: boolean serialNumber: string userId: number useToDefault: boolean } export default class KitSerialNumberInputToast extends Component { constructor(props) { super(props); this.show = this.show.bind(this) } public _state = redux.getState(); public toast = null; public active = false; public delayHideTimer = null; public delayTimer = null; public delayTimer1 = null; public inputRef = null; public readonly state: Readonly = { opacity: new Animated.Value(0), code: "", serialNumber: "", loading: false, codeExist: false, userId: this._state.userMsg && this._state.userMsg.userInfo && this._state.userMsg.userInfo.id ? this._state.userMsg.userInfo.id : 'TEMP_USER_ID', useToDefault: true }; componentWillUnmount() { this.delayHideTimer && clearTimeout(this.delayHideTimer); this.delayTimer && clearTimeout(this.delayTimer); this.inputRef && clearTimeout(this.inputRef); this.setState = (state, callback) => { return; } } show = () => { const { code, serialNumber } = this.props; this.setState({ code, serialNumber }) this.toast.setNativeProps({ style: { width: '100%', height: '100%', zIndex: 999999 } }) Animated.timing( this.state.opacity, { toValue: 0.32, duration: 250, //@ts-ignore easing: Easing.in(), useNativeDriver: true } ).start(); this.delayTimer = setTimeout(() => { this.inputRef && this.inputRef.focus(); }, 200); } hide = () => { this.active = false; Animated.timing( this.state.opacity, { toValue: 0, duration: 100, //@ts-ignore easing: Easing.in(), useNativeDriver: true } ).start(); this.delayHideTimer = setTimeout(() => { this.toast.setNativeProps({ style: { width: 0, height: 0, zIndex: -1 } }) }, 100); }; handleClose() { const { onConfirm, leftTitle, rightTitle, leftPlaceHolder, rightPlaceHolder, type, notCheck = false, settingMode = false, } = this.props; const { code, serialNumber, userId, useToDefault } = this.state; if (isEmpty(code)) { return toastShort(leftPlaceHolder) } if (isEmpty(serialNumber)) { return toastShort(rightPlaceHolder) } let serialNumberAndCode = code + '-' + serialNumber; if (settingMode) { onConfirm && onConfirm({ code, serialNumber }) this.hide(); return false } let params = { serialNumberAndCode: serialNumberAndCode, createUserId: userId, isLocalDelete: '0' }; this.setState({ loading: true }) const adaptResultForResearch = adaptAPIAndName(type, 'query', 'research-data'); mockAction.queryData(adaptResultForResearch.tableName, params) .then(res => { console.log("查询结果----->", res) if (res) { const list = filterSqlRows(res); console.log("当前调查的实例数------>", list) if (list && list.length) { this.setState({ codeExist: true, loading: false }) } else { try { this.setState({ codeExist: false, loading: false }) if (useToDefault) { let appConfig = this._state.AppConfig; appConfig[type] = code; redux.dispatch(setAppConfig(appConfig)) } onConfirm && onConfirm({ code, serialNumber }) this.hide(); } catch (e) { console.log("确定按钮逻辑错误---->", e) } } } }) .catch(err => { }) } onCodeChange(text) { text = filterEmoji(text); const { code } = this.state; if (isEmpty(text)) { this.setState({ code: text }) } else { var re = new RegExp("^[a-zA-Z]+$"); if (re.test(text)) { this.setState({ code: text }) } else { this.setState({ code }) toastShort("请输入纯英文") } } }; onSerialNumberChange(text) { text = filterEmoji(text); const { serialNumber } = this.state; var patrn = /^[0-9]*$/; if (!patrn.test(text)) { toastShort("请输入纯数字") this.setState({ serialNumber }) } else { this.setState({ serialNumber: text }) } }; switchIsUseToDefault() { let { useToDefault } = this.state; this.setState({ useToDefault: !useToDefault }) } render() { const { title = "", leftTitle = "", rightTitle = "", settingMode = false, existText = "编码已存在", leftPlaceHolder = "", rightPlaceHolder } = this.props; const { code, serialNumber, loading, codeExist, useToDefault } = this.state; return ( this.toast = c} style={styles.alertContainer}> {title} { loading ? : { codeExist ? {existText} : null } } {leftTitle} this.inputRef = c} style={styles.alertBodyInput} placeholder={leftPlaceHolder} keyboardType={'default'} maxLength={5} value={code} onChangeText={this.onCodeChange.bind(this)} /> 1-5位英文 - { settingMode ? {rightTitle} 1-5位数字 : {rightTitle} 1-5位数字 } { !settingMode ? { useToDefault ? : null } 将当前编号前缀设为默认值 : null } this.hide()} > 取消 this.handleClose()} > 确定 ) } } const styles = StyleSheet.create({ alertContainer: { position: 'absolute', width: '0%', height: '0%', justifyContent: 'center', alignItems: 'center', overflow: 'hidden', zIndex: -1 }, alertOverlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, width: '100%', height: '100%', backgroundColor: '#000' }, alertContent: { width: px2dp(320), backgroundColor: '#fff', borderRadius: px2dp(8), justifyContent: 'space-between', alignItems: 'center', overflow: 'hidden', // elevation: 12 }, alertHeader: { width: '100%', justifyContent: 'center', alignItems: 'center', // backgroundColor: '#999', paddingTop: px2dp(18) }, alertBody: { width: '100%', height: px2dp(186), justifyContent: 'flex-start', alignItems: 'center', paddingHorizontal: px2dp(18), paddingTop: px2dp(16), }, alertBodyTop: { width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, alertBodyTopRight: { flex: 1, height: px2dp(24), flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center' }, alertBodyCenter: { flex: 1, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', paddingBottom: px2dp(2) }, alertBodyCenterHalf: { flex: 1, justifyContent: 'flex-start', alignItems: 'flex-start' }, alertBodyInput: { width: '100%', height: px2dp(36), borderWidth: 1, borderColor: '#e9e9e9', borderRadius: px2dp(4), paddingHorizontal: px2dp(6), paddingVertical: px2dp(0), fontSize: 14, color: colors.blackColor }, title: { fontSize: 16, color: '#000', fontWeight: '600', lineHeight: 25, textAlign: 'center', letterSpacing: 0.8 }, borderTop: { borderBottomColor: '#d2d2d2', borderBottomWidth: onePx, width: px2dp(280), height: 1, }, alertFooter: { width: '100%', height: px2dp(52), justifyContent: 'center', alignItems: 'center', flexDirection: 'row', }, btn: { flex: 1, height: '100%', justifyContent: 'center', alignItems: 'center' }, border: { width: 1, height: px2dp(32), borderRightColor: '#d2d2d2', borderRightWidth: onePx }, checkedIconContainer: { width: px2dp(18), height: px2dp(18), borderWidth: 1, borderColor: '#7f7f7f', justifyContent: 'center', alignItems: 'center', borderRadius: 1, marginRight: px2dp(10) }, checkedIcon: { width: px2dp(14), height: px2dp(14), tintColor: colors.blueColor }, descText: { fontSize: 14, color: '#7f7f7f', lineHeight: 20, marginTop: px2dp(2), marginLeft: px2dp(28) }, switchBodyRow: { width: '100%', flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', paddingVertical: px2dp(16), paddingTop: px2dp(2) } })