是否有任何方法可以单独验证ANT DESIGN表的每一列



我所做的是在ant设计表中呈现数据,现在每行的每一列都是可编辑的。当呈现错误的数据时,例如电子邮件格式不正确,应该会有验证消息,直到电子邮件无效;当用户开始打字时,我还需要验证用户输入,这也是为了编辑。就像当用户开始键入时,我还需要验证每个输入更改,无论是正确的电子邮件还是错误的,同样,我需要对行的每一列使用自定义regex,我希望我尽最大努力解决我面临的问题。

我试过的东西在下面。

1-当数据被呈现到表中时,我得到了每行两个可编辑的列。数据呈现

2-用户开始编辑时用户编辑

3-当文本框为空时,验证失败。我的目标是验证每次击键,无论验证通过错误消失时输入的电子邮件是否正确。验证失败的

以下是我迄今为止尝试的代码

import React, { useContext, useState, useEffect, useRef } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table, Input, Button, Popconfirm, Form } from "antd";
const EditableContext = React.createContext();
const EditableRow = ({ index, ...props }) => {
const [form] = Form.useForm();
return (
<Form form={form} component={false}>
<EditableContext.Provider value={form}>
<tr {...props} />
</EditableContext.Provider>
</Form>
);
};
const EditableCell = ({
title,
editable,
children,
dataIndex,
record,
handleSave,
...restProps
}) => {
const [editing, setEditing] = useState(false);
const inputRef = useRef();
const form = useContext(EditableContext);
useEffect(() => {
if (editing) {
inputRef.current.focus();
}
}, [editing]);
const toggleEdit = () => {
setEditing(!editing);
form.setFieldsValue({
[dataIndex]: record[dataIndex]
});
};
const save = async e => {
try {
const values = await form.validateFields();
toggleEdit();
handleSave({ ...record, ...values });
} catch (errInfo) {
console.log("Save failed:", errInfo);
}
};
let childNode = children;
if (editable) {
childNode = editing ? (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
message: `${title} is required.`
}
]}
>
<Input ref={inputRef} onPressEnter={save} onBlur={save} />
</Form.Item>
) : (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
return <td {...restProps}>{childNode}</td>;
};
class EditableTable extends React.Component {
constructor(props) {
super(props);
this.columns = [
{
title: "name",
dataIndex: "name",
width: "30%",
editable: true
},
{
title: "age",
dataIndex: "age",
editable: true
},
{
title: "address",
dataIndex: "address"
},
{
title: "operation",
dataIndex: "operation",
render: (text, record) =>
this.state.dataSource.length >= 1 ? (
<Popconfirm
title="Sure to delete?"
onConfirm={() => this.handleDelete(record.key)}
>
<a>Delete</a>
</Popconfirm>
) : null
}
];
this.state = {
dataSource: [
{
key: "0",
name: "Edward King 0",
age: "32",
address: "London, Park Lane no. 0"
},
{
key: "1",
name: "Edward King 1",
age: "32",
address: "London, Park Lane no. 1"
}
],
count: 2
};
}
handleDelete = key => {
const dataSource = [...this.state.dataSource];
this.setState({
dataSource: dataSource.filter(item => item.key !== key)
});
};
handleAdd = () => {
const { count, dataSource } = this.state;
const newData = {
key: count,
name: `Edward King ${count}`,
age: 32,
address: `London, Park Lane no. ${count}`
};
this.setState({
dataSource: [...dataSource, newData],
count: count + 1
});
};
handleSave = row => {
const newData = [...this.state.dataSource];
const index = newData.findIndex(item => row.key === item.key);
const item = newData[index];
newData.splice(index, 1, { ...item, ...row });
this.setState({
dataSource: newData
});
};
render() {
const { dataSource } = this.state;
const components = {
body: {
row: EditableRow,
cell: EditableCell
}
};
const columns = this.columns.map(col => {
if (!col.editable) {
return col;
}
return {
...col,
onCell: record => ({
record,
editable: col.editable,
dataIndex: col.dataIndex,
title: col.title,
handleSave: this.handleSave
})
};
});
return (
<div>
<Button
onClick={this.handleAdd}
type="primary"
style={{
marginBottom: 16
}}
>
Add a row
</Button>
<Table
components={components}
rowClassName={() => "editable-row"}
bordered
dataSource={dataSource}
columns={columns}
/>
</div>
);
}
}
ReactDOM.render(<EditableTable />, document.getElementById("container"));

链接到CODESANDBOX

我的目标:

1-当数据被呈现时,我需要在那里进行验证,如果电子邮件不正确,那么该无效电子邮件将有效。

2-当用户开始键入时,当电子邮件正确时,它将验证Keystore。验证将消失。

3-会有更多的列,比如电话,地址,会有regex。我需要使用regex分别验证每行中的每一列。

我尽力描述我的问题,我能提供的任何其他信息都告诉我,谢谢

我只是通过在我的editablecell函数中使用switch-case语句来解决这个问题,如下所示

EditableCell = ({
title,
editable,
children,
dataIndex,
record,
...restProps
}) => {
const [editing, setEditing] = useState(false);
const inputRef = useRef();
const form = useContext(EditableContext);
useEffect(() => {
if (editing) {
inputRef.current.focus();
}
}, [editing]);
const toggleEdit = () => {
setEditing(!editing);
form.setFieldsValue({
[dataIndex]: record[dataIndex]
});
};
let childNode = children;
if (editable) {
switch (dataIndex) {
case "cName":
{
if (editing) {
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
message: "Enter Name Please"
}
]}
>
<Input
ref={inputRef}
onChange={e => {
this.onChangeExelField(
record.key,
"cName",
e.target.value
);
}}
/>
</Form.Item>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
case "cEmail":
{
if (editing) {
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
type: "email",
message: "Enter Valid Email Address"
}
]}
>
<Input
ref={inputRef}
onChange={e => {
this.onChangeExelField(
record.key,
"cEmail",
e.target.value
);
}}
/>
</Form.Item>
);
} else if (
!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$/i.test(childNode[1])
) {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24,
borderRadius: "3px",
border: "red solid 1px"
}}
onClick={toggleEdit}
>
{children}
</div>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
case "cPhone":
{
if (editing) {
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
pattern: new RegExp("^3[0-9+]{9}$"),
message: "Enter Correct Phone ( 3xxxxxxxxx )"
}
]}
>
<Input
ref={inputRef}
onChange={e => {
this.onChangeExelField(
record.key,
"cPhone",
e.target.value
);
}}
/>
</Form.Item>
);
} else if (!/^3[0-9+]{9}$/i.test(childNode[1])) {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24,
borderRadius: "3px",
border: "red solid 1px"
}}
onClick={toggleEdit}
>
{children}
</div>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
case "cAddress":
{
if (editing) {
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
pattern: new RegExp("^[a-zA-Z0-9 _/#&+-||]{25,}$"),
message: "Address Must be 25 letters"
}
]}
>
<Input
ref={inputRef}
onChange={e => {
console.log(record);
this.onChangeExelField(
record.key,
"cAddress",
e.target.value
);
}}
/>
</Form.Item>
);
} else if (!/^[a-zA-Z0-9 _/#&+-||]{25,}$/i.test(childNode[1])) {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24,
borderRadius: "3px",
border: "red solid 1px"
}}
onClick={toggleEdit}
>
{children}
</div>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
case "cCity":
{
if (editing) {
const options = [];
this.props.cities.fetchedCities.docs.map(p => {
options.push({ value: p._id, label: p.cityName });
});
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true
}
]}
>
<Select
ref={inputRef}
options={options}
onChange={e => {
this.onChangeExelField(record.key, "cCity", e.label);
}}
/>
</Form.Item>
);
} else if (record.invalidFields.includes("cCity")) {
const options = [];
this.props.cities.fetchedCities.docs.map(p => {
options.push({ value: p._id, label: p.cityName });
});
childNode = (
<div>
<Select
ref={inputRef}
options={options}
onChange={e => {
console.log(e);
this.onChangeExelField(record.key, "cCity", e.label);
}}
/>
</div>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
case "cAmount":
{
if (editing) {
childNode = (
<Form.Item
style={{
margin: 0
}}
name={dataIndex}
rules={[
{
required: true,
message: "Enter Valid Amount"
}
]}
>
<Input
ref={inputRef}
onChange={e => {
this.onChangeExelField(
record.key,
"cAmount",
e.target.value
);
}}
/>
</Form.Item>
);
} else {
childNode = (
<div
className="editable-cell-value-wrap"
style={{
paddingRight: 24
}}
onClick={toggleEdit}
>
{children}
</div>
);
}
}
break;
default:
return null;
}
}
return <td {...restProps}>{childNode}</td>;
};

相关内容

  • 没有找到相关文章

最新更新