Skip to content

Commit

Permalink
docs:Collapse Dropdown 文档
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWXY committed Apr 11, 2024
1 parent ee62ad7 commit 66388fd
Show file tree
Hide file tree
Showing 78 changed files with 795 additions and 85,808 deletions.
8 changes: 4 additions & 4 deletions packages/components/Collapse/Collapse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ref, provide, watch } from 'vue'
import { each } from 'lodash-es'
import type {
NameType,
CollapseItemName,
CollapseProps,
CollapseEmits,
CollapseContext
Expand All @@ -18,7 +18,7 @@ defineOptions({
const props = defineProps<CollapseProps>()
const emits = defineEmits<CollapseEmits>()
const activeNames = ref<NameType[]>(props.modelValue)
const activeNames = ref<CollapseItemName[]>(props.modelValue)
watch(
() => props.modelValue,
val => {
Expand All @@ -30,7 +30,7 @@ if (props.accordion && activeNames.value.length > 1) {
debugWarn(COMPONENT_NAME, 'accordion mode should only have one active item')
}
function handleItemClick(item: NameType) {
function handleItemClick(item: CollapseItemName) {
let _activeNames = [...activeNames.value]
// 手风琴模式
if (props.accordion) {
Expand All @@ -50,7 +50,7 @@ function handleItemClick(item: NameType) {
updateActiveNames(_activeNames)
}
function updateActiveNames(val: NameType[]) {
function updateActiveNames(val: CollapseItemName[]) {
activeNames.value = val
each(['update:modelValue', 'change'], e =>
emits(e as 'update:modelValue' & 'change', val)
Expand Down
8 changes: 5 additions & 3 deletions packages/components/Collapse/CollapseItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ function handleClick() {
}"
@click="handleClick"
>
<slot name="title">
{{ title }}
</slot>
<span class="er-collapse-item__title">
<slot name="title">
{{ title }}
</slot>
</span>
<er-icon icon="angle-right" class="header-angle" />
</div>
<transition name="slide" v-on="transitionEvents">
Expand Down
14 changes: 7 additions & 7 deletions packages/components/Collapse/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { Ref } from "vue";
export type NameType = string | number;
export type CollapseItemName = string | number;

export interface CollapseProps {
modelValue: NameType[];
modelValue: CollapseItemName[];
accordion?: boolean;
}

export interface CollapseItemProps {
name: NameType;
name: CollapseItemName;
title?: string;
disabled?: boolean;
}

export interface CollapseContext {
activeNames: Ref<NameType[]>;
handleItemClick(name: NameType): void;
activeNames: Ref<CollapseItemName[]>;
handleItemClick(name: CollapseItemName): void;
}

export interface CollapseEmits {
(e: "update:modelValue", value: NameType[]): void;
(e: "change", value: NameType[]): void;
(e: "update:modelValue", value: CollapseItemName[]): void;
(e: "change", value: CollapseItemName[]): void;
}
22 changes: 13 additions & 9 deletions packages/components/Dropdown/Dropdown.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
import { ref, computed, provide } from 'vue'
import { omit } from 'lodash-es'
import { omit, isNil } from 'lodash-es'
import type {
DropdownProps,
DropdownEmits,
Expand All @@ -10,6 +10,7 @@ import type {
} from './types'
import { DROPDOWN_CTX_KEY } from './constants'
import type { TooltipInstance } from '../Tooltip/types'
import { useId } from '@eric-ui/hooks'
import DropdownItem from './DropdownItem.vue'
import Tooltip from '../Tooltip/Tooltip.vue'
Expand All @@ -19,7 +20,7 @@ defineOptions({
inheritAttrs: false
})
const props = withDefaults(defineProps<DropdownProps>(), {
hideAfterClick: true,
hideOnClick: true,
menuOptions: () => [] as MenuOption[]
})
const emits = defineEmits<DropdownEmits>()
Expand All @@ -29,15 +30,15 @@ const tooltipProps = computed(() =>
omit(props, ['menuOptions', 'hideAfterClick'])
)
function handleItemClick(e: unknown) {
function handleItemClick(e: MenuOption) {
if ((e as MenuOption).disabled) return
emits('select', e as MenuOption)
props.hideAfterClick && tooltipRef.value?.hide()
props.hideOnClick && tooltipRef.value?.hide()
!isNil(e.command) && emits('command', e.command)
}
defineExpose<DropdownInstance>({
show: () => tooltipRef.value?.show(),
hide: () => tooltipRef.value?.hide()
handleOpen: () => tooltipRef.value?.show(),
handleClose: () => tooltipRef.value?.hide()
})
provide<DropdownContext>(DROPDOWN_CTX_KEY, {
Expand All @@ -56,7 +57,10 @@ provide<DropdownContext>(DROPDOWN_CTX_KEY, {
<template #content>
<ul class="er-dropdown__menu">
<slot name="dropdown">
<template v-for="item in menuOptions" :key="item.name">
<template
v-for="item in items"
:key="item.command ?? useId().value"
>
<dropdown-item v-bind="item" />
</template>
</slot>
Expand All @@ -66,6 +70,6 @@ provide<DropdownContext>(DROPDOWN_CTX_KEY, {
</div>
</template>
<style>
<style scoped>
@import './style.css';
</style>
10 changes: 6 additions & 4 deletions packages/components/Dropdown/DropdownItem.vue
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
<script setup lang="ts">
import { type MenuOption } from './types'
import { type DropdownItemProps } from './types'
import { DROPDOWN_CTX_KEY } from './constants'
import { useId } from '@eric-ui/hooks'
import { inject } from 'vue'
defineOptions({
name: 'ErDropdownItem'
})
const props = defineProps<MenuOption>()
const props = defineProps<DropdownItemProps>()
const ctx = inject(DROPDOWN_CTX_KEY)
function handleClick() {
if (props.disabled) return
ctx?.handleItemClick(props)
}
</script>

<template>
<li v-if="divided" role="separator" class="divided-placeholder"></li>
<li
:id="`dropdown-item-${name}`"
:id="`dropdown-item-${command ?? useId().value}`"
:class="{
'er-dropdown__item': true,
'is-disabled': disabled,
Expand All @@ -32,6 +34,6 @@ function handleClick() {
</li>
</template>
<style>
<style scoped>
@import './style.css';
</style>
18 changes: 9 additions & 9 deletions packages/components/Dropdown/types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import type { VNode } from "vue";
import type { TooltipProps } from "../Tooltip/types";

export interface MenuOption {
label: string | VNode;
name: string | number;
export interface DropdownItemProps {
command?: string | number;
label?: string | VNode;
disabled?: boolean;
divided?: boolean;
}

export interface DropdownProps extends TooltipProps {
menuOptions?: MenuOption[];
hideAfterClick?: boolean;
items?: DropdownItemProps[];
hideOnClick?: boolean;
}

export interface DropdownEmits {
(e: "visible-change", value: boolean): void;
(e: "select", value: MenuOption): void;
(e: "command", value: DropdownItemProps["command"]): void;
}

export interface DropdownInstance {
show(): void;
hide(): void;
handleOpen(): void;
handleClose(): void;
}

export interface DropdownContext {
handleItemClick(item: MenuOption): void;
handleItemClick(item: DropdownItemProps): void;
}
Loading

0 comments on commit 66388fd

Please sign in to comment.