import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, TextInput } from 'react-native'; import { colors, filterEmoji, isEmpty, onePx, px2dp, toast, toastShort } from '../../utils'; type limitRuleType = 'int&>0' | 'int' | 'float1' | 'float2' | 'max1' | 'max90' | 'max100' | 'max180' | 'max360'; interface Props { leftTitle?: string leftExtraTitle?: string rightTitle?: string rightExtraTitle?: string leftSubTitle?: string rightSubTitle?: string leftPlaceholderText?: string rightPlaceholderText?: string isLeftNeeded?: boolean isRightNeeded?: boolean isLast?: boolean leftType?: 'input' | 'text' | 'pick' rightType?: 'input' | 'text' | 'pick' onPressLeft?: Function onPressRight?: Function leftValue?: any rightValue?: string onChangeTextLeft?: Function onChangeTextRight?: Function leftTextInputType?: 'number' | 'string' rightTextInputType?: 'number' | 'string' leftTextInputMaxLength?: number rightTextInputMaxLength?: number hideLeftRightIcon?: boolean hideRightRightIcon?: boolean onLeftInputBlur?: Function onRightInputBlur?: Function hasPermission?: boolean leftValueLimitRule?: Array rightValueLimitRule?: Array inlineLayout?: boolean keepLeftTitle?: boolean } interface State { } export default class KitFormGroupRow extends React.Component { constructor(props: Props) { super(props); }; public readonly state: Readonly = { }; componentWillUnmount() { this.setState = (state, callback) => { return; } } onPressLeft() { const { leftType, onPressLeft } = this.props; onPressLeft && onPressLeft(); }; onPressRight() { const { rightType, onPressRight } = this.props; onPressRight && onPressRight(); } handleFilterTextByRule(ruleList, testText: string): { checkPass: boolean, text?: string | number } { if (ruleList.length == 0) { return { checkPass: true, text: testText } } if (isEmpty(testText) || testText.length == 0) { return { checkPass: true, text: '' }; } if (testText[0] == '0' && testText.length == 1) { return { checkPass: true, text: '0' }; } if (testText == '0.') { return { checkPass: true, text: '0.' }; } const numberTypeRuleList = ['int&>0', 'int', 'float1', 'float2', 'max1', 'max90', 'max100', 'max180', 'max360'] let checkPass = true; let needFilterUnNumberChart = false; ruleList.forEach(ruleStr => { if (numberTypeRuleList.includes(ruleStr)) { needFilterUnNumberChart = true; } }) var intPatrn = /^[0-9]*$/; if (needFilterUnNumberChart) { if (testText[0] == '.') { return { checkPass: false }; } testText = testText.replace(/[^\d^\.]+/g, ''); } ruleList.forEach(ruleString => { switch (ruleString) { case 'int': if (!intPatrn.test(testText)) { checkPass = false; toastShort("请输入整数") } break; case 'int&>0': if (!intPatrn.test(testText)) { checkPass = false; toastShort("请输入整数") } else if (Number(testText) <= 0) { checkPass = false; toastShort("此项数据不能小于或等于0, 请输入正整数") } break; case 'float1': testText = testText.replace(/[^\d^\.]+/g, ''); let arr = testText.split('.'); if (arr.length > 2) { checkPass = false; toastShort("只能输入一个小数点"); } else { if (arr[1] && arr[1].length > 1) { checkPass = false; toastShort("只能输入一个小数点后一位数"); } } break; case 'float2': testText = testText.replace(/[^\d^\.]+/g, ''); let arr1 = testText.split('.'); if (arr1.length > 2) { checkPass = false; toastShort("只能输入一个小数点"); } else { if (arr1[1] && arr1[1].length > 2) { checkPass = false; toastShort("只能输入一个小数点后两位数"); } } break; case 'max1': if (Number(testText) > 1) { checkPass = false; toastShort("最大值为1") } break; case 'max90': if (Number(testText) > 90) { checkPass = false; toastShort("最大值为90") } break; case 'max100': console.log("执行检测---->max100") if (Number(testText) > 100) { checkPass = false; toastShort("最大值为100") } break; case 'max180': if (Number(testText) > 180) { checkPass = false; toastShort("最大值为180") } break; case 'max360': if (Number(testText) > 360) { checkPass = false; toastShort("最大值为360") } break; default: break } }) if (needFilterUnNumberChart && testText.length) { if (testText[testText.length - 1] == '.' || testText === '0.' || testText.includes('.0')) { } else { testText = Number(testText) + '' } } return { checkPass: checkPass, text: testText }; }; _onChangeTextLeft(text) { const newText = filterEmoji(text); const { onChangeTextLeft, leftValueLimitRule = [] } = this.props; const checkResult = this.handleFilterTextByRule(leftValueLimitRule, newText); if (checkResult.checkPass) { onChangeTextLeft && onChangeTextLeft(checkResult.text); } } _onChangeTextRight(text) { const newText = filterEmoji(text); const { onChangeTextRight, rightValueLimitRule = [] } = this.props; const checkResult = this.handleFilterTextByRule(rightValueLimitRule, newText) if (checkResult.checkPass) { onChangeTextRight && onChangeTextRight(checkResult.text); } } render() { const { leftTitle, rightTitle, isLast = false, isLeftNeeded = false, isRightNeeded = false, leftPlaceholderText, rightPlaceholderText, leftType, rightType, leftValue, rightValue, onChangeTextLeft, onChangeTextRight, leftTextInputType = 'string', rightTextInputType = 'string', leftTextInputMaxLength = 30, rightTextInputMaxLength = 30, hideLeftRightIcon, hideRightRightIcon, onLeftInputBlur, onRightInputBlur, hasPermission = true, inlineLayout, leftSubTitle = "", rightSubTitle = "", rightExtraTitle = "", keepLeftTitle = false, leftExtraTitle = "" } = this.props; return ( {leftTitle} {leftExtraTitle} {isLeftNeeded && hasPermission && *} {/* */} { leftSubTitle ? {leftSubTitle}  : null } { leftType == 'input' ? onLeftInputBlur && onLeftInputBlur()} numberOfLines={1} /> : <> { leftValue ? {leftValue + ''} : {leftPlaceholderText} } } { !hideLeftRightIcon && hasPermission ? : null } { rightTitle || rightSubTitle ? 10 ? 15 : 16 }, inlineLayout ? { fontSize: 14, color: '#606266'} : null ]}> {rightTitle} {isRightNeeded ? * : null} {rightExtraTitle ? rightExtraTitle : ""} { rightSubTitle ? {rightSubTitle}  : null } { rightType == 'input' ? onRightInputBlur && onRightInputBlur()} numberOfLines={1} /> : <> { rightValue ? {rightValue + ''} : {rightPlaceholderText} } } { !hideRightRightIcon && hasPermission ? : null } : } ) } } const styles = StyleSheet.create({ formGroupRow: { width: px2dp(330), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: 1, borderBottomWidth: onePx, borderBottomColor: colors.borderLineColor, marginHorizontal: px2dp(10), paddingTop: px2dp(7), paddingBottom: px2dp(8) }, formGroupRowInline: { width: px2dp(258), marginHorizontal: 0 }, formGroupRowHalf: { flex: 1, flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', }, formGroupRowLeft: { flex: 1, justifyContent: 'center', alignItems: 'flex-start', }, formGroupTitleContainer: { width: '100%', height: px2dp(24), justifyContent: 'center', alignItems: 'flex-start', marginTop: px2dp(2) }, valueContainer: { width: '100%', height: px2dp(28), flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center' }, formRowTitle: { width: '100%', fontSize: 16, color: colors.blackColor, }, formRowSubTitle: { fontSize: 14, color: '#555', }, formGroupRowRight: { width: px2dp(26), justifyContent: 'center', alignItems: 'flex-start' }, rightIcon: { width: px2dp(14), height: px2dp(14), tintColor: '#909399' }, formRowPlaceHolderText: { fontSize: 14, color: colors.placeholderColor }, formValueText: { fontSize: 14, color: '#303133', overflow: 'hidden', paddingRight: px2dp(8) }, formRowPlaceHolderTextInput: { fontSize: 14, color: '#303133', width: '100%', height: px2dp(28), paddingVertical: px2dp(0), paddingHorizontal: px2dp(0), flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', overflow: 'hidden', } })