feat: Detail组件重构完成

This commit is contained in:
kailong321200875
2021-10-19 09:59:58 +08:00
parent 34221f387f
commit 7f5ef99ccc
12 changed files with 832 additions and 147 deletions

View File

@@ -0,0 +1,141 @@
<template>
<div class="avatars-wrap">
<template v-if="tooltip">
<el-tooltip
v-for="(item, $index) in avatarsData"
:key="$index"
:content="item.text"
placement="top"
>
<div
:class="
showAvatar ? 'avatars-item-img' : ['avatars-item', `avatars-${item.type || 'default'}`]
"
>
<el-avatar v-if="showAvatar" :size="40" :src="item.url">
<img :src="defaultImg" />
</el-avatar>
<span v-else>{{ item.text.substr(0, 1) }}</span>
</div>
</el-tooltip>
<div v-if="max && data.length - max > 0" :class="['avatars-item', 'avatars-item-img']">
<span>+{{ data.length - max }}</span>
</div>
</template>
<template v-else>
<div
v-for="(item, $index) in avatarsData"
:key="$index"
:class="
showAvatar ? 'avatars-item-img' : ['avatars-item', `avatars-${item.type || 'default'}`]
"
>
<el-avatar v-if="showAvatar" :size="40" :src="item.url">
<img :src="defaultImg" />
</el-avatar>
<span v-else>{{ item.text.substr(0, 1) }}</span>
</div>
<div v-if="max && data.length - max > 0" :class="['avatars-item', 'avatars-item-img']">
<span>+{{ data.length - max }}</span>
</div>
</template>
</div>
</template>
<script setup lang="ts" name="Avatars">
import { PropType, computed } from 'vue'
import { deepClone } from '@/utils'
import { AvatarConfig } from './types'
import defaultImg from '@/assets/img/default-avatar.png'
const props = defineProps({
// 展示的数据
data: {
type: Array as PropType<AvatarConfig[]>,
default: () => []
},
// 最大展示数量
max: {
type: Number as PropType<number>,
default: 0
},
// 是否使用头像
showAvatar: {
type: Boolean as PropType<boolean>,
default: false
},
// 是否显示完整名称
tooltip: {
type: Boolean as PropType<boolean>,
default: true
}
})
const avatarsData = computed(() => {
if (props.max) {
if (props.data.length <= props.max) {
return props.data
} else {
const data = deepClone(props.data).splice(0, props.max)
return data
}
} else {
return props.data
}
})
</script>
<style lang="less" scoped>
.avatars-wrap {
display: flex;
.avatars-item {
display: inline-block;
width: 40px;
height: 40px;
line-height: 40px;
color: #fff;
text-align: center;
background: #2d8cf0;
border: 1px solid #fff;
border-radius: 50%;
}
.avatars-item-img {
display: inline-block;
border-radius: 50%;
.el-avatar--circle {
border: 1px solid #fff;
}
}
.avatars-item-img + .avatars-item-img {
margin-left: -12px;
}
.avatars-item + .avatars-item {
margin-left: -12px;
}
.avatars-default {
color: #bae7ff;
background: #096dd9;
}
.avatars-success {
color: #f6ffed;
background: #52c41a;
}
.avatars-danger {
color: #fff1f0;
background: #f5222d;
}
.avatars-warning {
color: #fffbe6;
background: #faad14;
}
}
</style>

View File

@@ -0,0 +1,5 @@
export interface AvatarConfig {
text: string
type?: string
url?: string
}

View File

@@ -0,0 +1,206 @@
<template>
<div class="detail__wrap">
<div v-if="title" class="detail__wrap--header" @click="toggleClick">
<div class="detail__wrap--title">
<div>
{{ title }}
<el-tooltip v-if="message" :content="message" placement="right">
<i class="el-icon-warning-outline"></i>
</el-tooltip>
</div>
</div>
<i
v-if="collapsed"
:class="['el-icon-arrow-down', { 'el-icon-arrow-down-transform': !show }]"
></i>
</div>
<el-collapse-transition>
<div v-show="show" class="detail__content" :style="contentStyleObj">
<el-row>
<el-col v-for="(item, $index) in schema" :key="$index" :span="item.span || 12">
<div class="detail__content--item" :class="{ 'detail__content--flex': !vertical }">
<div class="content__item--label" :style="labelStyleObj">
<slot :name="item.field" :row="item">
{{ item.label }}
</slot>
</div>
<div class="content__item--message" :style="messageStyleObj">
<slot :name="`${item.field}Content`" :row="data">
{{ data[item.field] }}
</slot>
</div>
</div>
</el-col>
</el-row>
</div>
</el-collapse-transition>
</div>
</template>
<script setup lang="ts" name="Detail">
import { PropType, ref, computed } from 'vue'
import { SchemaConfig } from './types'
const props = defineProps({
// 详情标题
title: {
type: String as PropType<string>,
default: ''
},
// 是否可折叠
collapsed: {
type: Boolean as PropType<boolean>,
default: true
},
// 辅助提示
message: {
type: String as PropType<string>,
default: ''
},
// 是否需要边框
border: {
type: Boolean as PropType<boolean>,
default: true
},
// 需要展示的数据
data: {
type: Object as PropType<IObj>,
required: true
},
// 布局展示的数据
schema: {
type: Array as PropType<SchemaConfig[]>,
required: true
},
// 是否标题和内容各占一行 垂直布局
vertical: {
type: Boolean as PropType<boolean>,
default: false
},
// 标题宽度
labelWidth: {
type: String as PropType<string>,
default: '150px'
},
// 标题位置
labelAlign: {
type: String as PropType<string>,
default: 'left'
},
// 边框颜色
borderColor: {
type: String as PropType<string>,
default: '#f0f0f0'
},
// 标题背景颜色
labelBg: {
type: String as PropType<string>,
default: '#fafafa'
},
classic: {
type: Boolean as PropType<boolean>,
default: false
}
})
const show = ref<boolean>(true)
const contentStyleObj = computed(() => {
return {
borderTop: props.border ? `1px solid ${props.borderColor}` : '',
borderLeft: props.border ? `1px solid ${props.borderColor}` : ''
}
})
const labelStyleObj = computed((): any => {
return {
width: props.vertical ? `100%` : props.labelWidth,
textAlign: props.labelAlign,
backgroundColor: props.border && !props.classic ? props.labelBg : '',
borderRight: props.border && !props.classic ? `1px solid ${props.borderColor}` : '',
borderBottom: props.border && !props.classic ? `1px solid ${props.borderColor}` : ''
}
})
const messageStyleObj = computed(() => {
return {
width: props.vertical ? `100%` : `calc(100% - ${props.labelWidth})`,
borderRight: props.border && !props.classic ? `1px solid ${props.borderColor}` : '',
borderBottom: props.border && !props.classic ? `1px solid ${props.borderColor}` : ''
}
})
function toggleClick() {
if (props.collapsed) {
show.value = !show.value
}
}
</script>
<style lang="less" scoped>
.detail__wrap {
padding: 10px;
background: #fff;
border-radius: 2px;
.detail__wrap--header {
display: flex;
height: 32px;
margin-bottom: 10px;
justify-content: space-between;
align-items: center;
// cursor: pointer;
.detail__wrap--title {
position: relative;
display: inline-block;
margin-left: 10px;
font-family: MicrosoftYaHei-Bold;
font-size: 18px;
font-weight: 700;
color: rgba(0, 0, 0, 0.85);
&::after {
position: absolute;
top: 1px;
left: -10px;
width: 4px;
height: 90%;
background: var(--main-color);
content: '';
}
}
.el-icon-arrow-down {
transition: all 0.2s;
}
.el-icon-arrow-down-transform {
transform: rotate(-180deg);
}
}
.detail__content {
:deep(.el-row) {
flex-wrap: wrap;
}
.detail__content--flex {
display: flex;
height: 100%;
}
.content__item--label {
padding: 8px 16px;
font-size: 14px;
line-height: 20px;
color: #918e8d;
}
.content__item--message {
padding: 8px 16px;
flex: 1;
font-size: 14px;
line-height: 20px;
color: #606266;
overflow-wrap: break-word;
}
}
}
</style>

View File

@@ -0,0 +1,5 @@
export interface SchemaConfig {
field: string // 字段名
label?: string // label名
span?: number // 列的数量
}

View File

@@ -2,9 +2,11 @@ import type { App } from 'vue'
import SvgIcon from './SvgIcon/index.vue' // svg组件
import ComSearch from './Search/index.vue' // search组件
import ComDialog from './Dialog/index.vue' // dialog组件
import ComDetail from './Detail/index.vue' // detail组件
export function setupGlobCom(app: App<Element>): void {
app.component('SvgIcon', SvgIcon)
app.component('ComSearch', ComSearch)
app.component('ComDialog', ComDialog)
app.component('ComDetail', ComDetail)
}

View File

@@ -10,7 +10,7 @@ import { setupDirectives } from '@/directives' // 自定义指令
import { setupGlobCom } from './components'
import { setupElement } from '@/plugins/element-plus'
// import { setupElement } from '@/plugins/element-plus'
import '@/styles/index.less'
@@ -29,7 +29,7 @@ setupRouter(app) // 引入路由
setupDirectives(app)
setupElement(app)
// setupElement(app)
setupGlobCom(app) // 引入全局组件

View File

@@ -185,14 +185,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
title: '弹窗'
}
},
// {
// path: 'detail',
// component: () => import('_v/components-demo/detail/index.vue'),
// name: 'DetailDemo',
// meta: {
// title: '详情组件'
// }
// },
{
path: 'detail',
component: () => import('_v/components-demo/detail/index.vue'),
name: 'DetailDemo',
meta: {
title: '详情'
}
},
{
path: 'qrcode',
component: () => import('_v/components-demo/qrcode/index.vue'),
@@ -201,14 +201,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [
title: '二维码'
}
},
// {
// path: 'avatars',
// component: () => import('_v/components-demo/avatars/index.vue'),
// name: 'AvatarsDemo',
// meta: {
// title: '头像组'
// }
// },
{
path: 'avatars',
component: () => import('_v/components-demo/avatars/index.vue'),
name: 'AvatarsDemo',
meta: {
title: '头像组'
}
},
{
path: 'highlight',
component: () => import('_v/components-demo/highlight/index.vue'),

View File

@@ -0,0 +1,84 @@
<template>
<div>
<el-alert
effect="dark"
:closable="false"
title="头像组组件 -- 基础用法"
type="info"
style="margin-bottom: 20px"
/>
<avatars :data="data" />
<el-alert
effect="dark"
:closable="false"
title="头像组组件 -- 不显示tooltip"
type="info"
style="margin-top: 20px; margin-bottom: 20px"
/>
<avatars :data="data" :tooltip="false" />
<el-alert
effect="dark"
:closable="false"
title="头像组组件 -- 最多展示5"
type="info"
style="margin-top: 20px; margin-bottom: 20px"
/>
<avatars :data="data" :max="5" />
<el-alert
effect="dark"
:closable="false"
title="头像组组件 -- 展示头像"
type="info"
style="margin-top: 20px; margin-bottom: 20px"
/>
<avatars show-avatar :data="data1" />
</div>
</template>
<script setup lang="ts" name="AvatarsDemo">
import { ref } from 'vue'
import { AvatarConfig } from '_c/Avatars/types'
import Avatars from '_c/Avatars/index.vue'
const data = ref<AvatarConfig[]>([
{ text: '陈某某' },
{ text: '李某某', type: 'success' },
{ text: '张某某', type: 'danger' },
{ text: '王某某', type: 'warning' },
{ text: '龙某某' },
{ text: '孙某某' },
{ text: '刘某某' },
{ text: '赵某某' }
])
const data1 = ref<AvatarConfig[]>([
{
text: '陈某某',
url: 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2863969516,2611770076&fm=26&gp=0.jpg'
},
{
text: '李某某',
url: 'https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=465970198,3877503753&fm=26&gp=0.jpg'
},
{
text: '张某某',
url: 'https://dss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1857202600,3614139084&fm=26&gp=0.jpg'
},
{
text: '王某某',
url: 'https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1444080181,4150129244&fm=26&gp=0.jpg'
},
{
text: '龙某某',
url: 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2469786567,2163872639&fm=26&gp=0.jpg'
},
{
text: '孙某某',
url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=4119236579,869456058&fm=26&gp=0.jpg'
},
{ text: '刘某某', url: 'xxxxx' },
{ text: '赵某某' }
])
</script>

View File

@@ -0,0 +1,217 @@
<template>
<div>
<el-alert
effect="dark"
:closable="false"
title="详情组件。"
type="info"
style="margin-bottom: 20px"
/>
<com-detail :data="data" :schema="schema" title="基础示例" message="辅助文字" />
<com-detail
title="不可折叠"
:data="data"
:schema="schema"
:collapsed="false"
message="辅助文字"
style="margin-top: 20px"
/>
<com-detail title="没有辅助文字" :data="data" :schema="schema" style="margin-top: 20px" />
<com-detail
title="没有边框"
:data="data"
:schema="schema"
:border="false"
style="margin-top: 20px"
/>
<com-detail
title="垂直布局"
:data="data"
:vertical="true"
:schema="schema"
style="margin-top: 20px"
/>
<el-form
ref="formRef"
:hide-required-asterisk="true"
:model="form"
:rules="rules"
style="margin-top: 20px"
>
<com-detail title="与表单结合并自定义插槽" :data="form" :schema="fromSchema">
<template #title="scope">
<span class="is-required-item">{{ scope.row.label }}</span>
</template>
<template #titleContent>
<el-form-item prop="title">
<el-input v-model="form.title" placeholder="请输入标题" />
</el-form-item>
</template>
<template #author="scope">
<span class="is-required-item">{{ scope.row.label }}</span>
</template>
<template #authorContent>
<el-form-item prop="author">
<el-input v-model="form.author" placeholder="请输入作者" />
</el-form-item>
</template>
<template #display_time="scope">
<span class="is-required-item">{{ scope.row.label }}</span>
</template>
<template #display_timeContent>
<el-form-item prop="display_time">
<el-date-picker
v-model="form.display_time"
type="datetime"
placeholder="请选择创建时间"
style="width: 100%"
/>
</el-form-item>
</template>
<template #importance="scope">
<span class="is-required-item">{{ scope.row.label }}</span>
</template>
<template #importanceContent>
<el-form-item prop="importance">
<el-select v-model="form.importance" placeholder="请选择重要性" style="width: 100%">
<el-option label="重要" value="3" />
<el-option label="良好" value="2" />
<el-option label="一般" value="1" />
</el-select>
</el-form-item>
</template>
<template #pageviews="scope">
<span class="is-required-item">{{ scope.row.label }}</span>
</template>
<template #pageviewsContent>
<el-form-item prop="pageviews">
<el-input-number
v-model="form.pageviews"
:min="0"
:max="99999999"
style="width: 100%"
/>
</el-form-item>
</template>
</com-detail>
<div style="margin-top: 15px; text-align: center">
<el-button type="primary" @click="saveData">保存(控制台查看数据)</el-button>
</div>
</el-form>
</div>
</template>
<script setup lang="ts" name="DetailDemo">
import { reactive, ref, unref } from 'vue'
const formRef = ref<Nullable<any>>(null)
const requiredRule: {
required: boolean
message: string
} = {
required: true,
message: '该项为必填项'
}
const data = reactive<IObj>({
username: 'chenkl',
nickName: '梦似花落。',
age: 26,
phone: '13655971xxxx',
email: '502431556@qq.com',
addr: '这是一个很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长的地址',
sex: '男',
certy: '35058319940712xxxx'
})
const schema = reactive<IObj[]>([
{
field: 'username',
label: '用户名'
},
{
field: 'nickName',
label: '昵称'
},
{
field: 'phone',
label: '联系电话'
},
{
field: 'email',
label: '邮箱'
},
{
field: 'addr',
label: '地址',
span: 24
}
])
const fromSchema = reactive<IObj[]>([
{
field: 'title',
label: '标题',
span: 24
},
{
field: 'author',
label: '作者'
},
{
field: 'display_time',
label: '创建时间'
},
{
field: 'importance',
label: '重要性'
},
{
field: 'pageviews',
label: '阅读数'
}
])
const form = reactive<IObj>({
id: '', // id
author: '', // 作者
title: '', // 标题
importance: '', // 重要性
display_time: '', // 创建时间
pageviews: 0 // 阅读数
})
const rules = reactive<IObj>({
title: [requiredRule],
author: [requiredRule],
importance: [requiredRule],
display_time: [requiredRule],
pageviews: [requiredRule]
})
function saveData() {
try {
;(unref(formRef) as any).validate((valid) => {
if (valid) {
console.log(form)
} else {
console.log('error submit!!')
return false
}
})
} catch (err) {
console.log(err)
}
}
</script>