uniapp实现点击标签文本域中显示标签内容

先上一个效果图

实现的效果有:

①.点击标签时,标签改变颜色并处于可删除状态

②.切换标签,文本域中出现标签的内容

③.点击标签右上角的删除可删掉标签,同时清除文本域中标签的内容

④.可输入内容,切换时不影响输入的内容

使用的是uniapp+vue3+uVieww-plus

代码:

javascript 复制代码
<template>
	<view class="mt32">
		<u--textarea v-model="textareaValue" placeholder="请输入未通过的原因!" height="200" count maxlength="30" ></u--textarea>
		<view class="mt32 flex flex-wrap justify-center align-items-center align-center">
			<view class="u-page__tag-item" v-for="(item, index) in radios" :key="index">
				<u-tag :show="item.show" shape="circle" :closable="item.closable" :bgColor="item.bgColor" :color="item.color" borderColor="#DCDCDC" :text="item.content" :plain="!item.checked" :name="index"
					@click="radioClick" @close="close(item)">
				</u-tag>
			</view>
		</view>
		<view class="flex justify-center align-items-center">
			<view class="mt32 tjBtn" @click = "submit">提交</view>
		</view>
		
	</view>
</template>

<script setup>
	import {
		reactive,
		ref,
		toRaw,
		onMounted,
		watch,
		computed
	} from 'vue';
	import {
		onLoad,onReady
	} from '@dcloudio/uni-app'
	
	// const textareaValue = ref('')
	const radios = ref([
		{
			checked: true,
			show:true,
			closable:false,
			bgColor: '#ffffff',
			color:'rgba(0, 0, 0, 0.90)',
			content:'商机对接1'
		},
		{
			checked: false,
			show:true,
			closable:false,
			bgColor: '#ffffff',
			color:'rgba(0, 0, 0, 0.90)',
			content:'商机对接2'
		},
		{
			checked: false,
			show:true,
			bgColor: '#ffffff',
			color:'rgba(0, 0, 0, 0.90)',
			content:'商机对接3'
		},
		{
			checked: false,
			show:true,
			closable:false,
			bgColor: '#ffffff',
			color:'rgba(0, 0, 0, 0.90)',
			content:'商机对接4'
		},
		{
			checked: false,
			show:true,
			closable:false,
			bgColor: '#ffffff',
			color:'rgba(0, 0, 0, 0.90)',
			content:'商机对接5'
		},
	])
	const reason1 = ref('');
	
	const reason2 = ref('');
	
	const textareaValue = computed({
	  get: () => reason1.value + reason2.value,
	  set: (value) => {
		for (let i = 0; i < radios.value.length; i++) {
			if (value.includes(radios.value[i].content)) {
			  reason1.value = radios.value[i].content;
			  reason2.value = value.replace(radios.value[i].content, '');
			  return;
			}
		  }
	  }
	})
	
	const radioClick = (name)=> {
		// console.log('radios.value',name)
		radios.value.map((item, index) => {
			if(index === name){
				item.checked = true
				item.bgColor = 'rgba(0, 82, 217, 0.70)'
				item.color = '#ffffff'
				item.closable = true
				reason1.value = item.content
			}else{
				item.checked = false
				item.bgColor = '#ffffff'
				item.color = 'rgba(0, 0, 0, 0.90)'
				item.closable = false
			}
		})
	}
	const close = (item)=>{
		console.log('关闭')
		item.show = false
		reason1.value = ''
	}
	const submit = ()=> {}
</script>

<style>
page{
	background: #F5F5F5;
}
.u-page__tag-item{
	height: auto;
	margin:0 20rpx 20rpx 0;
}
.u-page__tag-item text {
	display: inline-block;
	padding: 18rpx 32rpx;
}
.tjBtn{
	background: #0052D9;
	color: #fff;
	border-radius: 6px;
	padding: 24rpx 118rpx;
}
</style>

有些样式在全局定义的,自行设置样式。

相关推荐
原则猫10 小时前
前端基础大厦
前端
陈随易11 小时前
编程语言级别的Skill市场,AI Agent 的未来形态
前端·后端·程序员
SoaringHeart12 小时前
Flutter进阶:基于 EasyRefresh 的下拉刷新封装 n_easy_refresh_mixin.dart
前端·flutter
IT_陈寒14 小时前
Vite的热更新突然不香了,排查三小时差点砸键盘
前端·人工智能·后端
子兮曰14 小时前
Agency-Agents 深度解析:400+ AI 专家的"梦之队"如何重塑开发工作流
前端·后端·vibecoding
山河木马15 小时前
渲染管线-计算得到gl_Position(顶点着色器)之后续GPU流程
javascript·webgl·图形学
竹林81815 小时前
用 The Graph 查询链上数据实战:从手搓 RPC 到 Subgraph,我的 NFT 项目数据加载快了 10 倍
前端·javascript
妙码生花15 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十九):点选验证码代码逐行目检
前端·后端·go
Awu122716 小时前
⚡从零开发 Agent CLI(五)实现一个可治理、可扩展的工具系统
前端·人工智能·claude
咪库咪库咪16 小时前
Vue3-生命周期
前端