javascript - 在 React 中的数据预览中显示 PhoneInput 值
问题描述
我正在尝试PhoneInput
使用事件处理程序在数据预览(React)中显示值handleOnChangePhoneInput
。我如何使它工作?
我想在演示中“提交”按钮正下方的“数据预览”表中显示用户输入的“电话号码”值。例如,当一个人输入“名字”值并单击“提交”时,名字会显示在数据预览中,但我无法使其适用于“电话号码”和“出生数据”。
演示
零件
<PhoneInput
name="phoneNumber"
type="text"
country={"us"}
enableAreaCodes={true}
onlyCountries={["us"]}
areaCodes={{ us: ["000"] }}
inputProps={{
name: "phone",
country: "us",
required: true,
autoFocus: true
}}
value={this.state.phone}
onChange={this.handleOnChangePhoneInput}
inputStyle={{
width: "230px",
height: "45px"
}}
/>
事件
handleOnChangePhoneInput = value => {
this.setState({ phone: value }, () => {
return this.state.phone;
});
};
[谢谢!我是 React 的新手。]
解决方案
所以这就是你要做的事情,首先你需要保持输入的状态,所以你必须稍微修改一下状态
state = {
firstName: "",
lastName: "",
phone: "",
emailAddress: "",
eligibleAge: false,
max: new Date(
new Date().getFullYear() - 21,
new Date().getMonth(),
new Date().getDate()
),
min: new Date(
new Date().getFullYear() - 120,
new Date().getMonth(),
new Date().getDate()
),
selectedDate: ""
};
您要做的下一件事是创建三个额外的事件侦听器,一个用于所有文本输入,一个用于复选框,一个用于日历,这可以合并为一个,但为简单起见,我们将其分开
// Text Inputs
onChange = e => {
console.log(e);
this.setState(
{
...this.state,
[e.fieldProps.name]: e.fieldProps.value
},
() => {
console.log(this.state);
}
);
};
// CheckBox
onCheckBoxChange = e => {
this.setState(
{
...this.state,
eligibleAge: !this.state.eligibleAge
},
() => {
console.log(this.state);
}
);
};
// Calender
onSelect = e => {
var SelectedDate =
e.getDate() + "-" + (e.getMonth() + 1) + "-" + e.getFullYear();
this.setState(
{
...this.state,
selectedDate: SelectedDate
},
() => {
console.log(this.state);
}
);
};
您要做的下一件事是将参数传递给函数。(因为在您提供的演示中,如果字段为空,则不会显示。所以我决定通过检查该字段是否为空来模仿)
<Form
onSubmitStart={() =>
this.props.onSubmitStart({
...(this.state.firstName !== "" && {
firstName: this.state.firstName
}),
...(this.state.lastName !== "" && {
lastName: this.state.lastName
}),
...(this.state.emailAddress !== "" && {
emailAddress: this.state.emailAddress
}),
...(this.state.phone !== "" && { phone: this.state.phone }),
selectedDate: this.state.selectedDate,
eligibleAge: this.state.eligibleAge
})
}
>
在父组件(index.js
)中,您只需更改
handleSubmitStart = ({serialized}) => {
对此:
handleSubmitStart = serialized => {
代码沙盒:在这里
推荐阅读
- android - 单击edittext时如何取消隐藏系统栏
- java - 嵌套文档列表(大小未知)中的 MongoDB 过滤器
- angular - Angular 8 HostListener'window:unload'如何在卸载之前进行API调用
- asp.net-core - 使用 F# 和 asp.net 核心注册控制器
- opencv - 需要帮助定义边框宽度为 2 的 5x5 自定义 aruco 字典
- asp.net-core - 将 NLog 与 .Net Core 一起使用时,aspnet-request:serverVariable 的任何替代方案?
- java - Apache Beam 数据流中的外部 api 调用
- android - RecyclerView 在刷卡之前不会更新
- javascript - 如何使用 Node.js 将 JSON 流写入文件?
- amazon-web-services - Apache Velocity 模板语言 (VTL):处理冒号