mirror of
https://gitee.com/vant-contrib/vant-weapp.git
synced 2025-04-06 03:58:05 +08:00
[new feature] 新增 Radio (#354)
* [new component]: Radio * [new component]: Radio misc * [new component] Radio code review * [new component] Radio code review * [new component] Radio misc * [new component] Radio improve style
This commit is contained in:
parent
eff6b8ff30
commit
cf9cfdb44b
@ -56,6 +56,7 @@ module.exports = {
|
||||
include: {
|
||||
checkbox: require('./packages/checkbox/README.md'),
|
||||
field: require('./packages/field/README.md'),
|
||||
radio: require('./packages/radio/README.md'),
|
||||
search: require('./packages/search/README.md'),
|
||||
select: require('./packages/select/README.md'),
|
||||
stepper: require('./packages/stepper/README.md'),
|
||||
|
@ -17,6 +17,7 @@
|
||||
"pages/noticebar/index",
|
||||
"pages/panel/index",
|
||||
"pages/popup/index",
|
||||
"pages/radio/index",
|
||||
"pages/stepper/index",
|
||||
"pages/steps/index",
|
||||
"pages/switch/index",
|
||||
|
@ -17,9 +17,6 @@ export default {
|
||||
}, {
|
||||
name: 'Cell 单元格',
|
||||
path: '/pages/cell/index'
|
||||
}, {
|
||||
name: 'Checkbox 复选框',
|
||||
path: '/pages/checkbox/index'
|
||||
}, {
|
||||
name: 'Helper 基础样式',
|
||||
path: '/pages/helper/index'
|
||||
@ -72,13 +69,21 @@ export default {
|
||||
title: '表单',
|
||||
content: [
|
||||
{
|
||||
name: 'Field 输入框',
|
||||
path: '/pages/field/index'
|
||||
name: 'Checkbox 复选框',
|
||||
path: '/pages/checkbox/index'
|
||||
},
|
||||
{
|
||||
name: 'Datetime 时间选择器',
|
||||
path: '/pages/datetime/index'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'Field 输入框',
|
||||
path: '/pages/field/index'
|
||||
},
|
||||
{
|
||||
name: 'Radio 单选框',
|
||||
path: '/pages/radio/index'
|
||||
},
|
||||
]
|
||||
},
|
||||
action: {
|
||||
|
19
example/pages/radio/index.js
Normal file
19
example/pages/radio/index.js
Normal file
@ -0,0 +1,19 @@
|
||||
Page({
|
||||
|
||||
data: {
|
||||
items: [
|
||||
{name: 'USA', value: '美国'},
|
||||
{name: 'CHN', value: '中国', checked: 'true'}
|
||||
],
|
||||
|
||||
items1: [
|
||||
{name: 'USA', value: '美国'},
|
||||
{name: 'BRA', value: '巴西', disabled: true },
|
||||
{name: 'CHN', value: '中国', checked: 'true'}
|
||||
]
|
||||
},
|
||||
|
||||
handleRadioChange: function(e) {
|
||||
console.log('radio发生change事件,携带value值为:', e.detail)
|
||||
}
|
||||
});
|
9
example/pages/radio/index.json
Normal file
9
example/pages/radio/index.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"navigationBarTitleText": "Radio 单选框",
|
||||
"usingComponents": {
|
||||
"zan-radio": "../../dist/radio/index",
|
||||
"zan-panel": "../../dist/panel/index",
|
||||
"zan-cell": "../../dist/cell/index",
|
||||
"doc-page": "../../components/doc-page/index"
|
||||
}
|
||||
}
|
35
example/pages/radio/index.wxml
Normal file
35
example/pages/radio/index.wxml
Normal file
@ -0,0 +1,35 @@
|
||||
<doc-page title="radio" without-padding>
|
||||
<zan-panel title='基本用法'>
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
|
||||
<zan-panel title='禁用状态'>
|
||||
<zan-radio
|
||||
items="{{ items1 }}"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
|
||||
<zan-panel title='事件'>
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
bind:change="handleRadioChange"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
|
||||
<zan-panel title='自定义样式'>
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
radio-color="radio-color"
|
||||
radio-class="radio-demo"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
|
||||
<zan-panel title='列表模式'>
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
type="list"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
</doc-page>
|
8
example/pages/radio/index.wxss
Normal file
8
example/pages/radio/index.wxss
Normal file
@ -0,0 +1,8 @@
|
||||
.radio-demo {
|
||||
font-size: 12px !important;
|
||||
}
|
||||
|
||||
.radio-color {
|
||||
background-color: red !important;
|
||||
border-color: red !important;
|
||||
}
|
@ -1,25 +1,13 @@
|
||||
<view
|
||||
class="checkbox-class zan-checkbox {{ isInGroup ? 'zan-checkbox__item' : ''}} {{ type === 'list' ? 'zan-checkbox__list-item' : ''}}"
|
||||
bindtap="{{ labelDisabled ? '' : 'handleClick' }}"
|
||||
data-id="test"
|
||||
>
|
||||
<zan-icon
|
||||
type="{{ checked ? 'checked' : 'check'}}"
|
||||
class="zan-checkbox__icon {{ 'zan-checkbox--' + shape }} {{ disabled ? 'zan-checkbox--disabled' : '' }} {{ checked ? 'zan-checkbox--checked' : '' }}"
|
||||
class="zan-checkbox__icon {{ disabled ? 'zan-checkbox--disabled' : '' }} {{ checked ? 'zan-checkbox--checked' : '' }}"
|
||||
bindtap="{{ labelDisabled ? 'handleClick': '' }}"
|
||||
></zan-icon>
|
||||
<text class="zan-checkbox__label">
|
||||
<slot></slot>
|
||||
</text>
|
||||
</view>
|
||||
|
||||
<wxs module="parse">
|
||||
function getColor(color) {
|
||||
color = color || '#ff4444'
|
||||
return color;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getColor: getColor
|
||||
};
|
||||
</wxs>
|
||||
|
106
packages/radio/README.md
Normal file
106
packages/radio/README.md
Normal file
@ -0,0 +1,106 @@
|
||||
## Radio 单选框
|
||||
|
||||
### 使用指南
|
||||
在 index.json 中引入组件
|
||||
```json
|
||||
{
|
||||
"usingComponents": {
|
||||
"zan-radio": "/packages/radio/index"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 代码演示
|
||||
|
||||
#### 基础数据结构
|
||||
|
||||
```js
|
||||
items: [
|
||||
{name: 'USA', value: '美国'},
|
||||
{name: 'BRA', value: '巴西', disabled: true },
|
||||
{name: 'CHN', value: '中国', checked: 'true'}
|
||||
]
|
||||
```
|
||||
|
||||
#### 基础用法
|
||||
|
||||
```html
|
||||
<zan-radio items="{{ items }}"></zan-radio>
|
||||
```
|
||||
|
||||
#### 禁用状态
|
||||
|
||||
```html
|
||||
<zan-radio items="{{ items }}"></zan-radio>
|
||||
```
|
||||
|
||||
```js
|
||||
Page({
|
||||
data: {
|
||||
items: [
|
||||
{name: 'USA', value: '美国'},
|
||||
{name: 'BRA', value: '巴西', disabled: true },
|
||||
{name: 'CHN', value: '中国', checked: 'true'}
|
||||
]
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### 自定义样式
|
||||
|
||||
```html
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
radio-color="radio-color"
|
||||
radio-class="radio-demo"
|
||||
></zan-radio>
|
||||
```
|
||||
|
||||
```css
|
||||
.radio-demo {
|
||||
font-size: 12px !important;
|
||||
}
|
||||
|
||||
.radio-color {
|
||||
color: red !important;
|
||||
}
|
||||
```
|
||||
|
||||
#### 事件
|
||||
|
||||
```html
|
||||
<zan-panel title='事件'>
|
||||
<zan-radio
|
||||
items="{{ items }}"
|
||||
bind:change="handleRadioChange"
|
||||
></zan-radio>
|
||||
</zan-panel>
|
||||
```
|
||||
|
||||
```js
|
||||
Page({
|
||||
handleRadioChange: function(e) {
|
||||
console.log('radio发生change事件,携带value值为:', e.detail)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
#### 列表模式
|
||||
|
||||
```html
|
||||
<zan-radio items="{{ items }}" type="list"></zan-radio>
|
||||
```
|
||||
|
||||
### 参数
|
||||
| 参数 | 说明 | 类型 | 默认值 | 必须 |
|
||||
|-----------|-----------|-----------|-------------|-------------|
|
||||
| items | 单选数组 | Array | [] | - |
|
||||
| type | 显示类型: list | String | - | |
|
||||
| radio-class | radio样式类| String| -|
|
||||
| radio-color | radio选中颜色 | String | #06bf04|
|
||||
|
||||
### 事件
|
||||
|
||||
| 事件名称 | 说明 | 回调参数 |
|
||||
| ---- | --- | ---|
|
||||
| change | 数值改变时触发 | event |
|
31
packages/radio/index.js
Normal file
31
packages/radio/index.js
Normal file
@ -0,0 +1,31 @@
|
||||
Component({
|
||||
behaviors: ['wx://form-field'],
|
||||
|
||||
externalClasses: ['radio-class', 'radio-color'],
|
||||
|
||||
properties: {
|
||||
items: Array,
|
||||
type: String
|
||||
},
|
||||
|
||||
methods: {
|
||||
radioChange(e) {
|
||||
this.selectItem(e.detail.value)
|
||||
this.triggerEvent('change', e)
|
||||
},
|
||||
|
||||
selectItem(value) {
|
||||
let { items } = this.data;
|
||||
|
||||
items.forEach(item => {
|
||||
if ( item.name === value ) {
|
||||
item.checked = true;
|
||||
} else {
|
||||
item.checked = false;
|
||||
}
|
||||
})
|
||||
|
||||
this.setData({ items })
|
||||
}
|
||||
}
|
||||
});
|
6
packages/radio/index.json
Normal file
6
packages/radio/index.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {
|
||||
"zan-icon": "../icon/index"
|
||||
}
|
||||
}
|
79
packages/radio/index.pcss
Normal file
79
packages/radio/index.pcss
Normal file
@ -0,0 +1,79 @@
|
||||
.zan-radio {
|
||||
display: block;
|
||||
padding: 0 10px;
|
||||
font-size: 14px;
|
||||
|
||||
&__item {
|
||||
display: block;
|
||||
margin-top: 10px;
|
||||
height: 20px;
|
||||
|
||||
&:last-child {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
}
|
||||
|
||||
&__list-item {
|
||||
display: block;
|
||||
padding: 10px 10px 10px 0;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
.zan-radio__icon-wrap {
|
||||
float: right;
|
||||
margin-top: 3px;
|
||||
}
|
||||
}
|
||||
|
||||
&__icon-wrap {
|
||||
position: relative;
|
||||
width: 14px;
|
||||
height: 15px;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
color: #aaa;
|
||||
.zan-radio__origin {
|
||||
opacity: 0;
|
||||
width: 14px;
|
||||
height: 20px;
|
||||
}
|
||||
.zan-radio__icon {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%,-50%);
|
||||
border: 1px solid #dcdfe6;
|
||||
border-radius: 100%;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
background-color: #fff;
|
||||
cursor: pointer;
|
||||
box-sizing: border-box;
|
||||
|
||||
.zan-radio__icon-inside {
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%,-50%);
|
||||
width: 4px;
|
||||
height: 4px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
&.zan-radio--checked {
|
||||
border-color: #06bf04;
|
||||
background: #06bf04;
|
||||
.zan-radio__icon-inside {
|
||||
background-color: #fff;
|
||||
}
|
||||
}
|
||||
|
||||
&.zan-radio--disabled {
|
||||
opacity: .6;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&__name {
|
||||
display: inline-block;
|
||||
margin-left: 10px;
|
||||
}
|
||||
}
|
28
packages/radio/index.wxml
Normal file
28
packages/radio/index.wxml
Normal file
@ -0,0 +1,28 @@
|
||||
<radio-group
|
||||
class="zan-radio radio-class"
|
||||
bindchange="radioChange"
|
||||
wx:if="{{ items && items.length }}"
|
||||
>
|
||||
<label
|
||||
class="zan-radio__label {{ type === 'list' ? 'zan-radio__list-item' : 'zan-radio__item'}}"
|
||||
wx:for="{{ items }}"
|
||||
wx:key="{{ item.value }}"
|
||||
wx:for-item="item"
|
||||
wx:for-index="index"
|
||||
>
|
||||
<view class="zan-radio__icon-wrap">
|
||||
<view class="zan-radio__icon {{ item.disabled ? 'zan-radio--disabled' : '' }} {{ item.checked ? 'zan-radio--checked radio-color' : '' }}">
|
||||
<view class="zan-radio__icon-inside"></view>
|
||||
</view>
|
||||
<radio
|
||||
class="zan-radio__origin"
|
||||
value="{{ item.name }}"
|
||||
checked="{{ item.checked }}"
|
||||
disabled="{{ item.disabled }}"
|
||||
/>
|
||||
</view>
|
||||
<view class="zan-radio__name">
|
||||
{{item.value}}
|
||||
</view>
|
||||
</label>
|
||||
</radio-group>
|
Loading…
x
Reference in New Issue
Block a user