fix(page-header): backIcon 插槽在html 文件中无效

Signed-off-by: forget skyrim <11513667+forget-skyrim@user.noreply.gitee.com>
This commit is contained in:
forget skyrim 2022-12-06 21:54:20 +00:00 committed by Gitee
parent 38b2941990
commit b22e4cb098
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 30 additions and 4 deletions

View File

@ -1,7 +1,7 @@
<template>
<div class="lay-page-header">
<div class="lay-page-header__left" @click="emits('back')">
<slot name="backIcon"
<slot :name="backIconSlotName"
><i class="layui-icon" :class="[backIcon]"></i
></slot>
<div class="lay-page-header__title">{{ backText }}</div>
@ -19,7 +19,8 @@ export default {
};
</script>
<script lang="ts" setup>
import { useSlots } from "vue";
import { convertSlotName } from "../../utils";
import { getCurrentInstance, useSlots } from "vue";
import "./index.less";
export interface PageHeaderProps {
@ -34,6 +35,7 @@ const props = withDefaults(defineProps<PageHeaderProps>(), {
});
const emits = defineEmits(["back"]);
const slots = useSlots();
const instance = getCurrentInstance()!;
const backIconSlotName = convertSlotName(instance, "backIcon");
</script>

View File

@ -1,4 +1,4 @@
import { Component, VNode, VNodeTypes } from "vue";
import { Component, ComponentInternalInstance, VNode, VNodeTypes } from "vue";
export enum ShapeFlags {
ELEMENT = 1,
@ -31,3 +31,27 @@ export const isArrayChildren = (
): children is VNode[] => {
return Boolean(vn && vn.shapeFlag & ShapeFlags.ARRAY_CHILDREN);
};
/**
* back-icon backIcon
* @param vm
* @param name
*/
export function convertSlotName(vm: ComponentInternalInstance, name: string) {
const camelCaseName = camelCase(name);
const kebabCaseName = kebabCase(name);
return vm.slots[camelCaseName]
? camelCaseName
: vm.slots[kebabCaseName]
? kebabCaseName
: name
}
export function camelCase(str: string) {
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''))
}
export function kebabCase(key: string) {
const result = key.replace(/([A-Z])/g, ' $1').trim();
return result.split(' ').join('-').toLowerCase()
}