javascript - 如何处理反应原生文本输入组件的输入验证?
问题描述
我正在尝试在我的PhoneInput
组件上实现电话验证,但是在抽象级别上我有点迷茫和困惑。
我有 3 个组件的层次结构State (custom state handler) -> Phone Form (the phone form) -> Phone Input (actual phone input)
。
尝试的事情:
对
PhoneInput
viaonBlur()
事件进行验证,但无法正确获取对象 phone 值;得到[object Object]
或circular Dependency
将验证放入
State
组件中,但无法使我的状态正确更改将整个重构为一个无需使用组件即可
PhoneInput
处理的类,但我的登录表单停止工作state
State
期望的行为:0
当我输入一个以我想从电话号码中删除零开头的电话号码时。
代码:(我已经从示例中删除了所有样式表、道具和导入,但留下了我正在尝试做的事情的评论)
State.js
...omitted_code
export default class State extends React.Component<Props> {
state = this.props.default
update = values => {
// [NOTE]: 2nd Try
// const phoneValue = values.phone
// const beginsWithZero = /^0?/
// if (phoneValue === beginsWithZero) {
// const phoneFormatValue = phoneValue.replace(beginsWithZero, '')
// return this.setState(phoneFormatValue)
// } else {
// return this.setState(values)
// }
this.setState(values)
// [NOTE]: All this works, but I cannot make the state to update with phoneFormattedValue
// console.log(`This is the value: ${JSON.stringify(values, null, 2)}`)
// const beginsWithZero = /^0?/
// const phoneValue = values.phone
// const phoneFormattedValue = phoneValue.replace(beginsWithZero, '')
// console.log(`This is the phoneValue: ${phoneValue}`)
// console.log(`This is the phoneFormatted: ${phoneFormattedValue}`)
// console.log(`This is the state: ${JSON.stringify(this.state, null, 2)}`)
// formatPhoneValue = () => {}
this.props.onChange({ ...this.state, ...values })
}
render() {
const { children } = this.props
return children(this.state, this.update)
}
}
State.defaultProps = {
default: {},
onChange: () => {},
}
PhoneForm.js
..omitted_code
const PhoneForm = ({ locale, countryCode, countryCodes, style, onChange, error }: Props) => (
<State default={{ ...DEFAULT_FORM, countryCode }} onChange={onChange}>
//[NOTE]: the form and the update is what I think causes me problems
{(form, update) => (
<LoginField style={error ? error && styles.error : false}>
<PhoneInput
{...form}
countryCodes={countryCodes}
style={loginFieldStyles.input}
onChangeText={update}
/>
</LoginField>
</View>
)}
</State>
)
..omitted_code
电话输入.js
// [NOTE]: 1st Try: trying to handle the validation inside the component itself, would like to take that phone value from `changeText({ countryCode ,phone})` and formatted onBlur
const isValidPhoneNumber = value => {
const phoneRegex = /^0?/
// const phoneFormated = phone.replace(phoneRegex, '')
// const phoneStartsWithZero = phone.startsWith('0')
// const phoneFormated = phoneStartsWithZero ? phone.replace(phoneRegex, '') : phone
// console.log(`${phone.startsWith('0')} starts with zero.`)
//[NOTE]: Returns either undefined or [object Object]
console.log('This is the value.phone', value.phone)
console.log('This is the value', value)
// console.log(`This is the phone that was entered phone: ${phoneFormated}`)
// return phoneFormated
}
type PhoneInputProps = {
...omitted_code
}
const PhoneInput = ({
...omitted_code
}: PhoneInputProps) => (
<View style={styles.host}>
...omitted_code
<RNTextInput
...omitted_code
value={phone}
onChangeText={phone => onChangeText({ countryCode, phone })}
onFocus={phone => console.log(`I was focused, this is the phone: ${phone}`)}
onBlur={value => isValidPhoneNumber(value)}
{...props}
/>
</View>
)
解决方案
推荐阅读
- powershell - 如何在 Powershell 中创建宏(不是 excel)?
- php - 如果我不获取 SQL 查询怎么办?
- c++ - 此版本的 path/to/g++.exe 与您正在运行的 windows 版本不兼容
- java - 当您添加带有按钮的 JPanel 时,为什么 KeyListener 不会收到事件?
- c - 来自 gcc -0 文件 file.c 的 exe 文件在 vsc 终端中不起作用
- c# - Asp.NetCore + Next.js 客户端项目
- asp.net - ASP.NET Core 3.1 HTTP post [FromBody] 总是收到 null
- pandas - Pandas read_csv 在 gzipped 文件上失败并出现 OSError: Not a gzipped file (b'NU')
- javascript - 下拉菜单在第二页上不起作用
- python - 使用简单的 django 项目设置 gitlab CI