# weui_form_demo
**Repository Path**: fanhangcheng/weui_form_demo
## Basic Information
- **Project Name**: weui_form_demo
- **Description**: 封装基于weui的form组件
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2022-06-18
- **Last Updated**: 2023-04-14
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 封装WeUI Form组件
## 参数说明
| 属性 | 描述 | 默认值 | 必需 |
|---|
| formData | 表单绑定数据,用于初始化 | {} | 是 |
| options | 表单配置 | [] | 是 |
| rules | 表单验证规则 | [] | 否 |
| title | 卡片标题 | “” | 否 |
## options
| 属性 | 描述 | 必需 |
|---|
| title | 表单表头 | 是 |
| key | 表单对应的字段 | 是 |
| type | 控件类型 | 是 |
| rules | 是否开启验证 | 否 |
| option | 表单配置 | 否 |
| disabled | 禁用 | 否 |
| slot | 开启自定义控件 | 否 |
如果开启了 slot ,一定要指定好 slot="需要自定义的字段"
## type
| 属性 | 描述 |
|---|
| selector | 选择框 |
| text | 文本输入框 |
| textarea | 文本域 |
| number | 数字 |
| idcard | 身份证 |
| digit | 小数点 |
| password | 密码 |
| switch | 开关 |
| date | 日期 |
| time | 时间 |
| region | 地区选择 |
| file | 图片上传 |
## option
只有在type为[selector]时才需要设置
## props
如果是普通选择框无需设置
## 方法
| 属性 | 描述 | 回调函数 |
|---|
| validate() | 表单验证 | isValidate |
## 示例代码
### index.json
```json
{
"navigationBarBackgroundColor": "#3b4994",
"navigationBarTextStyle": "white",
"navigationBarTitleText": "测试页面",
"usingComponents": {
"my-form": "/components/Form/sform",
"mp-icon": "weui-miniprogram/icon/icon"
}
}
```
### index.wxml
```html
其他备注:
```
### index.js
```js
Page({
/**
* 页面的初始数据
*/
data: {
myFormData: {
formData: {
1: "",
2: "",
3: "",
4: "",
5: "",
test_slot: "",
test_area:""
},
options: [{
title: "调查总穴数",
key: "1",
type: "selector",
rules: true,
option: {
data: [{
id: 1,
name: "选项1"
},
{
id: 2,
name: "选项2"
},
{
id: 3,
name: "选项3"
},
{
id: 4,
name: "选项4"
}
],
props: {
key: "name"
}
}
},
{
title: "调查总株数",
key: "2",
type: "selector",
rules: true,
option: {
data: ["选项1", "选项2", "选项3"]
}
},
{
title: "调查总穗数",
key: "3",
type: "text",
rules: true
},
{
title: "活虫数",
key: "4",
type: "textarea",
rules: true
},
{
title: "数字",
key: "5",
type: "number",
rules: true
},
{
title:"",
key: "test_slot",
slot: true
},
{
title:"",
key: "test_area",
slot: true
}
],
rules: []
},
},
// 提交表单
submitForm() {
// 获取组件对象
const myForm = this.selectComponent("#my-form");
// 调用验证方法
if (myForm.validate()) {
console.log(myForm.data.formData);
}
}
})
```
### index.wxss
```css
.main {
padding: 5vh 0;
}
#my-form input {
border:1rpx solid red;
padding-left: 10rpx
}
.slot-box {
display: flex;
justify-content: space-between;
align-items: center;
}
.inp1 {
width: 400rpx;
font-size: 14px;
border-radius: 20rpx;
border-color:#bbb !important;
}
.inp2 {
width: 150rpx;
}
.slot-box2 {
margin-top: 50rpx;
display: flex;
justify-content: space-between;
}
.slot-box2 text {
width: 300rpx;
}
.slot-box2 textarea {
width: 100%;
border:1px solid #bbb
}
```