|
1 |
| -<script lang="ts"> |
| 1 | +<!-- <script lang="ts"> |
2 | 2 | import { cloneVNode, computed, defineComponent, h } from 'vue'
|
3 | 3 | import type { PropType, VNode } from 'vue'
|
4 | 4 | import classNames from 'classnames'
|
@@ -64,4 +64,71 @@ export default defineComponent({
|
64 | 64 | return () => h('div', { class: nuxtLabsTheme.UAvatarGroup.base.root }, clones.value)
|
65 | 65 | },
|
66 | 66 | })
|
| 67 | +</script> --> |
| 68 | + |
| 69 | +<script lang="ts"> |
| 70 | +import { cloneVNode, computed, defineComponent, h } from 'vue' |
| 71 | +import type { PropType, VNode } from 'vue' |
| 72 | +import classNames from 'classnames' |
| 73 | +import nuxtLabsTheme from '../../../theme/nuxtLabsTheme' |
| 74 | +import { getSlotsFromChildren } from './Types' |
| 75 | +import UAvatar from './UAvatar.vue' |
| 76 | +import { getVariantPropsWithClassesList } from '@/utils/getVariantProps' |
| 77 | +import type { UAvatarGroup } from '@/Types/componentsTypes/components' |
| 78 | +import { Components } from '@/Types/enums/Components' |
| 79 | + |
| 80 | +export type AvatarSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' |
| 81 | + |
| 82 | +export default defineComponent({ |
| 83 | + name: Components.UAvatarGroup, |
| 84 | + ...getVariantPropsWithClassesList<UAvatarGroup>(), |
| 85 | + |
| 86 | + props: { |
| 87 | + size: { |
| 88 | + type: String as PropType<AvatarSize>, |
| 89 | + default: 'md', |
| 90 | + }, |
| 91 | + max: { |
| 92 | + type: Number, |
| 93 | + default: null, |
| 94 | + }, |
| 95 | + }, |
| 96 | + setup(props, { slots }) { |
| 97 | + const children = computed(() => getSlotsFromChildren(slots)) |
| 98 | + |
| 99 | + // if max is passed as a string, we go ahead and convert it into an integer |
| 100 | + |
| 101 | + const max = computed(() => typeof props.max === 'string' ? Number.parseInt(props.max) : props.max) |
| 102 | + |
| 103 | + const clones = computed(() => children.value.map((node: VNode<unknown, unknown, { [key: string]: any }>, index: number) => { |
| 104 | + const vProps: Record<string, string> = {} |
| 105 | + |
| 106 | + if (!props.max || (max.value && index < max.value)) { |
| 107 | + if (props.size) |
| 108 | + vProps.size = props.size |
| 109 | + |
| 110 | + vProps.class = node.props?.class || '' |
| 111 | + vProps.class += `${classNames( |
| 112 | + nuxtLabsTheme.UAvatarGroup.base.avatarGroupMargin, |
| 113 | + )}` |
| 114 | + |
| 115 | + return cloneVNode(node, vProps) |
| 116 | + } |
| 117 | + |
| 118 | + if (max.value !== undefined && index === max.value) { |
| 119 | + return h(UAvatar, { |
| 120 | + size: props.size, |
| 121 | + text: `${children.value.length - max.value}`, |
| 122 | + name: `${children.value.length - max.value}`, |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + return null |
| 127 | + }).filter(Boolean).reverse()) |
| 128 | + |
| 129 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 130 | + // @ts-expect-error |
| 131 | + return () => h('div', { class: nuxtLabsTheme.UAvatarGroup.base.root }, clones.value) |
| 132 | + }, |
| 133 | +}) |
67 | 134 | </script>
|
0 commit comments