antd|antd Form 表单基础使用 及子组件传值
文章图片
/**
* 父组件 -start
*/
class Test extends PureComponent {
state = { data: {}, type: void 0 };
componentDidMount() {
// this.setState({
//type: "edit",
//data: {
//sex_type: "no",
//id: "sb",
//info: "33",
//sex: "man",
//child: {
//info: "000",
//input: "i2",
//sex: "man",
//},
//},
// });
}render() {
let { data = https://www.it610.com/article/{}, type } = this.state;
return (test
使用的事test里的form表单 俗称子表单
);
}
}
【antd|antd Form 表单基础使用 及子组件传值】test的表单子组件
/**
* form 的组件 -start
*/
class TestForm extends PureComponent {
render() {
const { getFieldDecorator, getFieldValue } = this.props.form;
let { data = https://www.it610.com/article/{}, type } = this.props;
if (type ==="edit") {
getFieldDecorator("id", {
initialValue: data.id,
});
}
return (
{getFieldDecorator("sex_type", {
initialValue: data.sex_type || "no",
rules: [
{
required: true,
message: "小郑是的吧",
},
],
})({[
{ label: "是的", value: "yes" },
{ label: "不是", value: "no" },
].map((item, index) => {
return ({item.label});
})})}
{/* 性别 */}
{getFieldValue("sex_type") === "no" && (
{getFieldDecorator("sex", {
initialValue: data.sex || "man",
})(
男
女
)}
)}
{/* 子组件 */}
{getFieldDecorator("child1", {
initialValue: ["1"],
})( )}
{getFieldDecorator("child2", {
initialValue: [],
})( )}
{getFieldDecorator("child3", {
initialValue: ["2"],
})( )}
);
}
}//真的formForm.create是创建一个form 这样就可以使用里面其方法
Test.Form = Form.create({ name: "normal_login" })(TestForm);
/**
* form 的组件 -end
*/
export default Test;
test内容的子组件
class Child extends PureComponent {
get form() {
return this.props.form;
}
render() {
let { show, data = https://www.it610.com/article/{}, form, ...otherProps } = this.props;
if (this.form) {
this.form.getFieldDecorator("child.info", {
initialValue: data?.child?.info,
});
this.form.getFieldDecorator("child.sex", {
initialValue: data?.child?.sex,
});
}return ({/* 把父组件所有的东西都给了子组件...otherProps 父组件其他的数据*/}
1
2
{show ? "有show" : "没有show"}
{this.form?.getFieldDecorator("child.input", {
initialValue: data?.child?.input,
rules: [
{
required: true,
message: "不能为空",
},
],
})()}
);
}
}
推荐阅读
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- ffmpeg源码分析01(结构体)
- 7、前端--jQuery简介、基本选择器、基本筛选器、属性选择器、表单选择器、筛选器方法、节点操作、绑定事件
- performSelectorOnMainThread:withObject:waitUntilDone:参数设置为NO或YES的区别
- React.js的表单(六)
- @逆战千锋|@逆战千锋 为什么sql语句执行之后表单中没有数据
- Angular|Angular 日期管道 datePipe - dateFormat
- FormData加axios实现图片上传(多图)
- Android|Android Studio 插件 - GsonFormat
- element-ui表单验证例子(validateField验证部分表单)