This commit is contained in:
hupeng
2023-10-11 11:27:47 +08:00
commit d0b337c596
659 changed files with 67106 additions and 0 deletions

View File

@ -0,0 +1,89 @@
export default {
props: {
// 绑定的值
value: {
type: [String, Number, Boolean],
default: ''
},
modelValue: {
type: [String, Number, Boolean],
default: ''
},
// 是否禁用全部radio
disabled: {
type: Boolean,
default: false
},
// 形状circle-圆形square-方形
shape: {
type: String,
default: 'circle'
},
// 选中状态下的颜色如设置此值将会覆盖parent的activeColor值
activeColor: {
type: String,
default: '#2979ff'
},
// 未选中的颜色
inactiveColor: {
type: String,
default: '#c8c9cc'
},
// 标识符
name: {
type: String,
default: ''
},
// 整个组件的尺寸默认px
size: {
type: [String, Number],
default: 18
},
// 布局方式row-横向column-纵向
placement: {
type: String,
default: 'row'
},
// label的文本
label: {
type: [String],
default: ''
},
// label的颜色 (默认 '#303133'
labelColor: {
type: [String],
default: '#303133'
},
// label的字体大小px单位
labelSize: {
type: [String, Number],
default: 14
},
// 是否禁止点击文本操作checkbox(默认 false )
labelDisabled: {
type: Boolean,
default: false
},
// 图标颜色
iconColor: {
type: String,
default: '#fff'
},
// 图标的大小单位px
iconSize: {
type: [String, Number],
default: 12
},
// 竖向配列时,是否显示下划线
borderBottom: {
type: Boolean,
default: false
},
// 图标与文字的对齐方式
iconPlacement: {
type: String,
default: 'left'
},
...uni.$uv?.props?.radioGroup
}
}

View File

@ -0,0 +1,115 @@
<template>
<view
class="uv-radio-group"
:class="bemClass"
:style="[$uv.addStyle(this.customStyle)]"
>
<slot></slot>
</view>
</template>
<script>
import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
import props from './props.js';
/**
* radioRroup 单选框父组件
* @description 单选框用于有一个选择用户只能选择其中一个的场景。搭配uv-radio使用
* @tutorial https://www.uvui.cn/components/radio.html
* @property {String | Number | Boolean} value 绑定的值
* @property {Boolean} disabled 是否禁用所有radio默认 false
* @property {String} shape 外观形状shape-方形circle-圆形(默认 circle )
* @property {String} activeColor 选中时的颜色应用到所有子Radio组件默认 '#2979ff'
* @property {String} inactiveColor 未选中的颜色 (默认 '#c8c9cc' )
* @property {String} name 标识符
* @property {String | Number} size 组件整体的大小单位px默认 18
* @property {String} placement 布局方式row-横向column-纵向 (默认 'row'
* @property {String} label 文本
* @property {String} labelColor label的颜色 (默认 '#303133'
* @property {String | Number} labelSize label的字体大小px单位 (默认 14
* @property {Boolean} labelDisabled 是否禁止点击文本操作checkbox(默认 false )
* @property {String} iconColor 图标颜色 (默认 '#ffffff'
* @property {String | Number} iconSize 图标的大小单位px (默认 12
* @property {Boolean} borderBottom placement为row时是否显示下边框 (默认 false
* @property {String} iconPlacement 图标与文字的对齐方式 (默认 'left'
* @property {Object} customStyle 组件的样式,对象形式
* @event {Function} change 任一个radio状态发生变化时触发
* @example <uv-radio-group v-model="value"></uv-radio-group>
*/
export default {
name: 'uv-radio-group',
mixins: [mpMixin, mixin, props],
computed: {
// 这里computed的变量都是子组件uv-radio需要用到的由于头条小程序的兼容性差异子组件无法实时监听父组件参数的变化
// 所以需要手动通知子组件这里返回一个parentData变量供watch监听在其中去通知每一个子组件重新从父组件(uv-radio-group)
// 拉取父组件新的变化后的参数
parentData() {
const value = this.value || this.modelValue;
return [value, this.disabled, this.inactiveColor, this.activeColor, this.size, this.labelDisabled, this.shape,
this.iconSize, this.borderBottom, this.placement]
},
bemClass() {
// this.bem为一个computed变量在mixin中
return this.bem('radio-group', ['placement'])
},
},
watch: {
// 当父组件需要子组件需要共享的参数发生了变化,手动通知子组件
parentData() {
if (this.children.length) {
this.children.map(child => {
// 判断子组件(uv-radio)如果有init方法的话就就执行(执行的结果是子组件重新从父组件拉取了最新的值)
typeof(child.init) === 'function' && child.init()
})
}
},
},
data() {
return {
}
},
created() {
this.children = []
},
methods: {
// 将其他的radio设置为未选中的状态
unCheckedOther(childInstance) {
this.children.map(child => {
// 所有子radio中被操作组件实例的checked的值无需修改
if (childInstance !== child) {
child.checked = false
}
})
const {
name
} = childInstance
// 通过emit事件设置父组件通过v-model双向绑定的值
// #ifdef VUE2
this.$emit('input', name)
// #endif
// #ifdef VUE3
this.$emit('update:modelValue',name)
// #endif
// 发出事件
this.$emit('change', name)
},
}
}
</script>
<style lang="scss" scoped>
@import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
.uv-radio-group {
flex: 1;
&--row {
@include flex;
flex-wrap: wrap;
}
&--column {
@include flex(column);
}
}
</style>

View File

@ -0,0 +1,65 @@
export default {
props: {
// radio的名称
name: {
type: [String, Number, Boolean],
default: ''
},
// 形状square为方形circle为圆型
shape: {
type: String,
default: ''
},
// 是否禁用
disabled: {
type: [String, Boolean],
default: ''
},
// 是否禁止点击提示语选中单选框
labelDisabled: {
type: [String, Boolean],
default: ''
},
// 选中状态下的颜色如设置此值将会覆盖parent的activeColor值
activeColor: {
type: String,
default: ''
},
// 未选中的颜色
inactiveColor: {
type: String,
default: ''
},
// 图标的大小单位px
iconSize: {
type: [String, Number],
default: ''
},
// label的字体大小px单位
labelSize: {
type: [String, Number],
default: ''
},
// label提示文字因为nvue下直接slot进来的文字由于特殊的结构无法修改样式
label: {
type: [String, Number, Boolean],
default: ''
},
// 整体的大小
size: {
type: [String, Number],
default: ''
},
// 图标颜色
iconColor: {
type: String,
default: ''
},
// label的颜色
labelColor: {
type: String,
default: ''
},
...uni.$uv?.props?.radio
}
}

View File

@ -0,0 +1,342 @@
<template>
<view
class="uv-radio"
@tap.stop="wrapperClickHandler"
:style="[radioStyle]"
:class="[`uv-radio-label--${parentData.iconPlacement}`, parentData.borderBottom && parentData.placement === 'column' && 'uv-border-bottom']"
>
<view
class="uv-radio__icon-wrap"
@tap.stop="iconClickHandler"
:class="iconClasses"
:style="[iconWrapStyle]"
>
<slot name="icon">
<uv-icon
class="uv-radio__icon-wrap__icon"
name="checkbox-mark"
:size="elIconSize"
:color="elIconColor"
/>
</slot>
</view>
<slot>
<text
class="uv-radio__text"
@tap.stop="labelClickHandler"
:style="{
color: elDisabled ? elInactiveColor : elLabelColor,
fontSize: elLabelSize,
lineHeight: elLabelSize
}"
>{{label}}</text>
</slot>
</view>
</template>
<script>
import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
import props from './props.js';
/**
* radio 单选框
* @description 单选框用于有一个选择用户只能选择其中一个的场景。搭配uv-radio-group使用
* @tutorial https://www.uvui.cn/components/radio.html
* @property {String | Number} name radio的名称
* @property {String} shape 形状square为方形circle为圆型
* @property {Boolean} disabled 是否禁用
* @property {String | Boolean} labelDisabled 是否禁止点击提示语选中单选框
* @property {String} activeColor 选中时的颜色如设置parent的active-color将失效
* @property {String} inactiveColor 未选中的颜色
* @property {String | Number} iconSize 图标大小单位px
* @property {String | Number} labelSize label字体大小单位px
* @property {String | Number} label label提示文字因为nvue下直接slot进来的文字由于特殊的结构无法修改样式
* @property {String | Number} size 整体的大小
* @property {String} iconColor 图标颜色
* @property {String} labelColor label的颜色
* @property {Object} customStyle 组件的样式,对象形式
*
* @event {Function} change 某个radio状态发生变化时触发(选中状态)
* @example <uv-radio :labelDisabled="false">门掩黄昏,无计留春住</uv-radio>
*/
export default {
name: "uv-radio",
mixins: [mpMixin, mixin, props],
data() {
return {
checked: false,
// 当你看到这段代码的时候,
// 父组件的默认值因为头条小程序不支持在computed中使用this.parent.shape的形式
// 故只能使用如此方法
parentData: {
iconSize: 12,
labelSize: 14,
labelDisabled: null,
disabled: null,
shape: null,
activeColor: null,
inactiveColor: null,
size: 18,
value: null,
modelValue: null,
iconColor: null,
placement: 'row',
borderBottom: false,
iconPlacement: 'left'
}
}
},
computed: {
// 是否禁用如果父组件uv-raios-group禁用的话将会忽略子组件的配置
elDisabled() {
return this.disabled !== '' ? this.disabled : this.parentData.disabled !== null ? this.parentData.disabled : false;
},
// 是否禁用label点击
elLabelDisabled() {
return this.labelDisabled !== '' ? this.labelDisabled : this.parentData.labelDisabled !== null ? this.parentData.labelDisabled :
false;
},
// 组件尺寸对应size的值默认值为21px
elSize() {
return this.size ? this.size : (this.parentData.size ? this.parentData.size : 21);
},
// 组件的勾选图标的尺寸默认12px
elIconSize() {
return this.iconSize ? this.iconSize : (this.parentData.iconSize ? this.parentData.iconSize : 12);
},
// 组件选中激活时的颜色
elActiveColor() {
return this.activeColor ? this.activeColor : (this.parentData.activeColor ? this.parentData.activeColor : '#2979ff');
},
// 组件选未中激活时的颜色
elInactiveColor() {
return this.inactiveColor ? this.inactiveColor : (this.parentData.inactiveColor ? this.parentData.inactiveColor :
'#c8c9cc');
},
// label的颜色
elLabelColor() {
return this.labelColor ? this.labelColor : (this.parentData.labelColor ? this.parentData.labelColor : '#606266')
},
// 组件的形状
elShape() {
return this.shape ? this.shape : (this.parentData.shape ? this.parentData.shape : 'circle');
},
// label大小
elLabelSize() {
return this.$uv.addUnit(this.labelSize ? this.labelSize : (this.parentData.labelSize ? this.parentData.labelSize :
'15'))
},
elIconColor() {
const iconColor = this.iconColor ? this.iconColor : (this.parentData.iconColor ? this.parentData.iconColor :
'#ffffff');
// 图标的颜色
if (this.elDisabled) {
// disabled状态下已勾选的radio图标改为elInactiveColor
return this.checked ? this.elInactiveColor : 'transparent'
} else {
return this.checked ? iconColor : 'transparent'
}
},
iconClasses() {
let classes = []
// 组件的形状
classes.push('uv-radio__icon-wrap--' + this.elShape)
if (this.elDisabled) {
classes.push('uv-radio__icon-wrap--disabled')
}
if (this.checked && this.elDisabled) {
classes.push('uv-radio__icon-wrap--disabled--checked')
}
// 支付宝,头条小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
// #ifdef MP-ALIPAY || MP-TOUTIAO
classes = classes.join(' ')
// #endif
return classes
},
iconWrapStyle() {
// radio的整体样式
const style = {}
style.backgroundColor = this.checked && !this.elDisabled ? this.elActiveColor : '#ffffff'
style.borderColor = this.checked && !this.elDisabled ? this.elActiveColor : this.elInactiveColor
style.width = this.$uv.addUnit(this.elSize)
style.height = this.$uv.addUnit(this.elSize)
// 如果是图标在右边的话,移除它的右边距
if (this.parentData.iconPlacement === 'right') {
style.marginRight = 0
}
return style
},
radioStyle() {
const style = {}
if (this.parentData.borderBottom && this.parentData.placement === 'row') {
error('检测到您将borderBottom设置为true需要同时将uv-radio-group的placement设置为column才有效')
}
// 当父组件设置了显示下边框并且排列形式为纵向时,给内容和边框之间加上一定间隔
if (this.parentData.borderBottom && this.parentData.placement === 'column') {
// ios像素密度高需要多一点的距离
style.paddingBottom = this.$uv.os() === 'ios' ? '12px' : '8px'
}
return this.$uv.deepMerge(style, this.$uv.addStyle(this.customStyle))
}
},
mounted() {
this.init()
},
methods: {
init() {
// 支付宝小程序不支持provide/inject所以使用这个方法获取整个父组件在created定义避免循环引用
this.updateParentData()
if (!this.parent) {
error('uv-radio必须搭配uv-radio-group组件使用')
}
// 设置初始化时,是否默认选中的状态
this.$nextTick(()=>{
let parentValue = null;
// #ifdef VUE2
parentValue = this.parentData.value;
// #endif
// #ifdef VUE3
parentValue = this.parentData.modelValue;
// #endif
this.checked = this.name === parentValue
})
},
updateParentData() {
this.getParentData('uv-radio-group')
},
// 点击图标
iconClickHandler(e) {
this.preventEvent(e)
// 如果整体被禁用,不允许被点击
if (!this.elDisabled) {
this.setRadioCheckedStatus()
}
},
// 横向两端排列时,点击组件即可触发选中事件
wrapperClickHandler(e) {
this.parentData.iconPlacement === 'right' && this.iconClickHandler(e)
},
// 点击label
labelClickHandler(e) {
this.preventEvent(e)
// 如果按钮整体被禁用或者label被禁用则不允许点击文字修改状态
if (!this.elLabelDisabled && !this.elDisabled) {
this.setRadioCheckedStatus()
}
},
emitEvent() {
// uv-radio的checked不为true时(意味着未选中),才发出事件,避免多次点击触发事件
if (!this.checked) {
this.$emit('change', this.name)
// 尝试调用uv-form的验证方法进行一定延迟否则微信小程序更新可能会不及时
this.$nextTick(() => {
this.$uv.formValidate(this, 'change')
})
}
},
// 改变组件选中状态
// 这里的改变的依据是更改本组件的checked值为true同时通过父组件遍历所有uv-radio实例
// 将本组件外的其他uv-radio的checked都设置为false(都被取消选中状态),因而只剩下一个为选中状态
setRadioCheckedStatus() {
this.emitEvent()
// 将本组件标记为选中状态
this.checked = true
typeof this.parent.unCheckedOther === 'function' && this.parent.unCheckedOther(this)
}
}
}
</script>
<style lang="scss" scoped>
$show-border: 1;
$show-border-bottom: 1;
@import '@/uni_modules/uv-ui-tools/libs/css/variable.scss';
@import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
@import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
$uv-radio-wrap-margin-right: 6px !default;
$uv-radio-wrap-font-size: 20px !default;
$uv-radio-wrap-border-width: 1px !default;
$uv-radio-wrap-border-color: #c8c9cc !default;
$uv-radio-line-height: 0 !default;
$uv-radio-circle-border-radius: 100% !default;
$uv-radio-square-border-radius: 3px !default;
$uv-radio-checked-color: #fff !default;
$uv-radio-checked-background-color: red !default;
$uv-radio-checked-border-color: #2979ff !default;
$uv-radio-disabled-background-color: #ebedf0 !default;
$uv-radio-disabled--checked-color: #c8c9cc !default;
$uv-radio-label-margin-left: 5px !default;
$uv-radio-label-margin-right: 12px !default;
$uv-radio-label-color: $uv-content-color !default;
$uv-radio-label-font-size: 15px !default;
$uv-radio-label-disabled-color: #c8c9cc !default;
.uv-radio {
/* #ifndef APP-NVUE */
@include flex(row);
/* #endif */
overflow: hidden;
flex-direction: row;
align-items: center;
&-label--left {
flex-direction: row
}
&-label--right {
flex-direction: row-reverse;
justify-content: space-between
}
&__icon-wrap {
/* #ifndef APP-NVUE */
box-sizing: border-box;
// nvue下border-color过渡有问题
transition-property: border-color, background-color, color;
transition-duration: 0.2s;
/* #endif */
color: $uv-content-color;
@include flex;
align-items: center;
justify-content: center;
color: transparent;
text-align: center;
margin-right: $uv-radio-wrap-margin-right;
font-size: $uv-radio-wrap-font-size;
border-width: $uv-radio-wrap-border-width;
border-color: $uv-radio-wrap-border-color;
border-style: solid;
/* #ifdef MP-TOUTIAO */
// 头条小程序兼容性问题需要设置行高为0否则图标偏下
&__icon {
line-height: $uv-radio-line-height;
}
/* #endif */
&--circle {
border-radius: $uv-radio-circle-border-radius;
}
&--square {
border-radius: $uv-radio-square-border-radius;
}
&--checked {
color: $uv-radio-checked-color;
background-color: $uv-radio-checked-background-color;
border-color: $uv-radio-checked-border-color;
}
&--disabled {
background-color: $uv-radio-disabled-background-color !important;
}
&--disabled--checked {
color: $uv-radio-disabled--checked-color !important;
}
}
&__label {
/* #ifndef APP-NVUE */
word-wrap: break-word;
/* #endif */
margin-left: $uv-radio-label-margin-left;
margin-right: $uv-radio-label-margin-right;
color: $uv-radio-label-color;
font-size: $uv-radio-label-font-size;
&--disabled {
color: $uv-radio-label-disabled-color;
}
}
}
</style>