uniapp实现单选框

采用uniapp-vue3实现的一款单选框组件,提供丝滑的动画选中效果,支持不同主题配置,适配web、H5、微信小程序(其他平台小程序未测试过,可自行尝试)

可到插件市场下载尝试: https://ext.dcloud.net.cn/plugin?id=16821

  • 使用示例

  • 示例代码

javascript 复制代码
<template>
	<view>
		<view class="light" style="background-color: white">
			<wo-radio v-model:options="state.items" v-model:defaultValue="state.default" @on-change="changeEvent">
			</wo-radio>
		</view>
		<view class="light">
			<wo-radio v-model:options="state.items" v-model:defaultValue="state.default" v-model:styleObj="state.theme.light" v-slot="slotProps" @on-change="changeEvent">
				<view style="display: flex;">
					<view>{{ slotProps.data.name }}</view>
					<view class="tag">{{ slotProps.data.tag }}</view>
				</view>
			</wo-radio>
		</view>
		<view class="dark">
			<wo-radio v-model:options="state.items" v-model:defaultValue="state.default" v-model:styleObj="state.theme.dark" v-slot="slotProps" @on-change="changeEvent">
				<view style="display: flex;">
					<view>{{ slotProps.data.name }}</view>
					<view class="tag">{{ slotProps.data.tag }}</view>
				</view>
			</wo-radio>
		</view>
	</view>
	
</template>

<script setup lang="ts">
	import { reactive } from 'vue';
	const state = reactive({
	  items: [{
				value: '1',
				name: '苹果味',
				tag: '饮料'
			},
			{
				value: '2',
				name: '香蕉味',
				tag: '酒水'
			},
			{
				value: '3',
				name: '火龙果味',
				tag: '饮料'
			},
			{
				value: '4',
				name: '西瓜味',
				tag: '饮料'
			},
			{
				value: '5',
				name: '哈密瓜味',
				tag: '酒水'
			},
			{
				value: '6',
				name: '榴莲味',
				tag: '酒水'
		}],
		default: '2',
		theme: {
				light: {
					primary: 'blue',
					unselectedRadioBg: '#eaeaea',
					selectedBg: 'hsla(0,0%,100%,0.5)',
					height: 20
				},
				dark: {
					primary: 'blue',
					unselectedRadioBg: 'hsl(223,90%,30%)',
					selectedBg: 'hsla(223,90%,30%,0.5)',
					height: 20
				}
		},
		height: 12
	});
	const changeEvent = (el) => {
		console.log('点击:', el);
	}
</script>

<style lang="scss" scoped>
	.light {
		border-radius: 10px;
		padding: 20rpx;
		font-size: 24rpx;
		background-color: hsl(223,90%,90%);
		margin: 20px;
		height: 300px;
		overflow: auto;
	}
	.dark {
		border-radius: 10px;
		padding: 20rpx;
		font-size: 24rpx;
		background-color: hsl(223,90%,10%);
		color: white;
		margin: 20px;
		height: 300px;
		overflow: auto;
	}
	.tag {
		background-color: #1BA035;
		color: white;
		font-size: 10px;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 4px;
		padding: 0 4px;
		margin-left: 5px;
	}
</style>
相关推荐
Juchecar8 分钟前
Naive UI 学习指南 - Vue3 初学者完全教程
前端·vue.js
用户8168694747259 分钟前
从0到1教你开发一个Mini-ESLint
前端·开源
coding随想9 分钟前
JavaScript中的DOM事件对象Event全解析
前端
专研狂10 分钟前
React 的闭包陷阱 + 状态异步更新机制
前端
zabr15 分钟前
AI黑箱解密:开发者必须了解的AI内部机制真相,原来我们一直被忽悠了
前端·aigc·ai编程
Sokach38627 分钟前
vue3引入tailwindcss 4.1
前端·css
云水边38 分钟前
vue模版中.gitignore和.prettierrc功能区分
前端
尝尝你的优乐美41 分钟前
封装那些Vue3.0中好用的指令
前端·javascript·vue.js
敲代码的彭于晏43 分钟前
localStorage 不够用?试试 IndexedDB !
前端·javascript·浏览器
chxii1 小时前
5.4 4pnpm 使用介绍
前端·javascript·vue.js