Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Tree): implement component #2757

Draft
wants to merge 4 commits into
base: v3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions docs/content/3.components/tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
description:
links:
- label: Tree
icon: i-custom-radix-vue
to: https://www.radix-vue.com/components/tree.html
- label: GitHub
icon: i-simple-icons-github
to: https://github.com/nuxt/ui/tree/v3/src/runtime/components/Tree.vue
---

## Usage

### Items

::component-code
---
ignore:
- items
external:
- items
props:
items:
- label: Node 1
children:
- label: Node 1.1
- label: Node 1.2
- label: Node 1.3
children:
- label: Node 1.3.1
- label: Node 1.3.2
- label: Node 2
children:
- label: Node 2.1
- label: Node 2.2
- label: Node 3
---
::

## Examples

## API

### Props

:component-props

### Slots

:component-slots

### Emits

:component-emits

## Theme

:component-theme
3 changes: 2 additions & 1 deletion playground/app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const components = [
'table',
'textarea',
'toast',
'tooltip'
'tooltip',
'tree'
]

const items = components.map(component => ({ label: upperName(component), to: `/components/${component}` }))
Expand Down
58 changes: 58 additions & 0 deletions playground/app/pages/components/tree.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<script setup lang="ts">
const items = ref([{
id: 1,
label: 'Level one 1',
children: [
{
id: 4,
label: 'Level two 1-1',
children: [
{
id: 9,
label: 'Level three 1-1-1'
},
{
id: 10,
label: 'Level three 1-1-2'
}
]
}
]
},
{
id: 2,
label: 'Level one 2',
children: [
{
id: 5,
label: 'Level two 2-1'
},
{
id: 6,
label: 'Level two 2-2'
}
]
},
{
id: 3,
label: 'Level one 3',
disabled: true,
children: [
{
id: 7,
label: 'Level two 3-1'
},
{
id: 8,
label: 'Level two 3-2'
}
]
}
])
</script>

<template>
<div>
<UTree :items="items" :get-key="(item) => item.label" />
</div>
</template>
115 changes: 115 additions & 0 deletions src/runtime/components/Tree.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<script lang="ts">
import { tv, type VariantProps } from 'tailwind-variants'
import type { TreeRootProps, TreeRootEmits } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/tree'
import type { AvatarProps, ChipProps } from '../types'
import type { DynamicSlots } from '../types/utils'

const appConfig = _appConfig as AppConfig & { ui: { tree: Partial<typeof theme> } }

const tree = tv({ extend: tv(theme), ...(appConfig.ui?.tree || {}) })

type TreeVariants = VariantProps<typeof tree>

export interface TreeItem {
label?: string
icon?: string
avatar?: AvatarProps
chip?: ChipProps
disabled?: boolean
slot?: string
children?: TreeItem[]
}

export interface TreeProps<T> extends Omit<TreeRootProps<T>, 'dir'> {
size?: TreeVariants['size']
/**
* The key used to get the label from the item.
* @defaultValue 'label'
*/
labelKey?: string
/**
* The icon displayed when an item is selected.
* @defaultValue appConfig.ui.icons.check
*/
selectedIcon?: string
/**
* The element or component this component should render as.
* @defaultValue 'div'
*/
as?: any
class?: any
ui?: Partial<typeof tree.slots>
}

export interface TreeEmits extends TreeRootEmits {}

type SlotProps<T> = (props: { item: T, index: number, level: number, hasChildren: boolean }) => any

export type TreeSlots<T extends { slot?: string }> = {
'item': SlotProps<T>
'item-leading': SlotProps<T>
'item-label': SlotProps<T>
'item-trailing': SlotProps<T>
} & DynamicSlots<T, SlotProps<T>>
</script>

<script setup lang="ts" generic="T extends TreeItem">
import { computed } from 'vue'
import { TreeRoot, TreeItem as TreeItemComponent, useForwardPropsEmits } from 'radix-vue'
import { reactiveOmit, createReusableTemplate } from '@vueuse/core'
import { get } from '../utils'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'

const props = withDefaults(defineProps<TreeProps<T>>(), {
labelKey: 'label'
})
const emits = defineEmits<TreeEmits>()
const slots = defineSlots<TreeSlots<T>>()

const rootProps = useForwardPropsEmits(reactiveOmit(props, 'class', 'ui'), emits)

const [DefineItemTemplate, ReuseItemTemplate] = createReusableTemplate<{ item: TreeItem, index: number, level: number, hasChildren: boolean }>()

const ui = computed(() => tree({
size: props.size,

Check failure on line 78 in src/runtime/components/Tree.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 20)

Unexpected trailing comma
}))
</script>

<template>
<DefineItemTemplate v-slot="{ item, index, level, hasChildren }">
<slot :name="item.slot || 'item'" v-bind="{ item: item as T, index, level, hasChildren }">
<slot :name="item.slot ? `${item.slot}-leading`: 'item-leading'" v-bind="{ item: item as T, index, level, hasChildren }">
<UIcon v-if="item.icon" :name="item.icon" :class="ui.itemLeadingIcon({ class: props.ui?.itemLeadingIcon })" />
<UAvatar v-else-if="item.avatar" v-bind="item.avatar" :size="((props.ui?.itemLeadingAvatarSize || ui.itemLeadingAvatarSize()) as AvatarProps['size'])" :class="ui.itemLeadingAvatar({ class: props.ui?.itemLeadingAvatar })" />
</slot>

<span v-if="get(item, props.labelKey) || !!slots[item.slot ? `${item.slot}-label`: 'item-label']" :class="ui.itemLabel({ class: props.ui?.itemLabel })">
<slot :name="item.slot ? `${item.slot}-label`: 'item-label'" v-bind="{ item: item as T, index, level, hasChildren }">
{{ get(item, props.labelKey) }}
</slot>
</span>

<span :class="ui.itemTrailing({ class: props.ui?.itemTrailing })">
<slot :name="item.slot ? `${item.slot}-trailing`: 'item-trailing'" v-bind="{ item: item as T, index, level, hasChildren }" />
<!-- <UIcon :name="selectedIcon || appConfig.ui.icons.check" :class="ui.itemTrailingIcon({ class: props.ui?.itemTrailingIcon })" />-->

Check failure on line 98 in src/runtime/components/Tree.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 20)

Expected indentation of 8 spaces but found 0 spaces

Check failure on line 98 in src/runtime/components/Tree.vue

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 20)

Expected space before '-->'
</span>
</slot>
</DefineItemTemplate>

<TreeRoot v-slot="{ flattenItems }" v-bind="rootProps" :class="ui.root({ class: [props.class, props.ui?.root] })" :get-key="(item) => item.label">
<TreeItemComponent
v-for="item in flattenItems"
v-bind="item.bind"
:key="item._id"
:class="ui.item({ class: [props.ui?.item] })"
:style="{ 'padding-left': `${item.level - 0.5}rem` }"
:data-disabled="item.value.disabled ? 'true' : undefined"
>
<ReuseItemTemplate :item="item.value" :index="item.index" :level="item.level" :has-children="item.hasChildren" />
</TreeItemComponent>
</TreeRoot>
</template>
1 change: 1 addition & 0 deletions src/runtime/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ export * from '../components/Textarea.vue'
export * from '../components/Toast.vue'
export * from '../components/Toaster.vue'
export * from '../components/Tooltip.vue'
export * from '../components/Tree.vue'
export * from './form'
export * from './locale'
1 change: 1 addition & 0 deletions src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ export { default as textarea } from './textarea'
export { default as toast } from './toast'
export { default as toaster } from './toaster'
export { default as tooltip } from './tooltip'
export { default as tree } from './tree'
56 changes: 56 additions & 0 deletions src/theme/tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { ModuleOptions } from '../module'

export default (options: Required<ModuleOptions>) => ({
slots: {
root: 'list-none min-w-32',
item: ['group relative w-full flex items-center gap-2 px-2 py-1.5 text-sm select-none outline-none rounded-[calc(var(--ui-radius)*1.5)] data-disabled:cursor-not-allowed data-disabled:opacity-75 text-[var(--ui-text)] hover:text-[var(--ui-text-highlighted)] hover:bg-[var(--ui-bg-elevated)]/50 focus:bg-[var(--ui-bg-elevated)]/50', options.theme.transitions && 'transition-colors before:transition-colors'],
itemLeadingIcon: 'shrink-0',
itemLeadingAvatar: 'shrink-0',
itemLeadingAvatarSize: '',
itemTrailing: 'ms-auto inline-flex',
itemTrailingIcon: 'shrink-0',
itemLabel: 'truncate'
},
variants: {
size: {
xs: {
label: 'p-1 text-xs gap-1',
item: 'p-1 text-xs gap-1',
itemLeadingIcon: 'size-4',
itemLeadingAvatarSize: '3xs',
itemTrailingIcon: 'size-4'
},
sm: {
label: 'p-1.5 text-xs gap-1.5',
item: 'p-1.5 text-xs gap-1.5',
itemLeadingIcon: 'size-4',
itemLeadingAvatarSize: '3xs',
itemTrailingIcon: 'size-4'
},
md: {
label: 'p-1.5 text-sm gap-1.5',
item: 'p-1.5 text-sm gap-1.5',
itemLeadingIcon: 'size-5',
itemLeadingAvatarSize: '2xs',
itemTrailingIcon: 'size-5'
},
lg: {
label: 'p-2 text-sm gap-2',
item: 'p-2 text-sm gap-2',
itemLeadingIcon: 'size-5',
itemLeadingAvatarSize: '2xs',
itemTrailingIcon: 'size-5'
},
xl: {
item: 'p-2 text-base gap-2',
itemLeadingIcon: 'size-6',
itemLeadingAvatarSize: 'xs',
itemTrailingIcon: 'size-6'
}
}
},
compoundVariants: [],
defaultVariants: {
size: 'md'
}
})
17 changes: 17 additions & 0 deletions test/components/Tree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, it, expect } from 'vitest'
import Tree, { type TreeProps, type TreeSlots } from '../../src/runtime/components/Tree.vue'
import ComponentRender from '../component-render'

describe('Tree', () => {
it.each([
// Props
['with as', { props: { as: 'div' } }],
['with class', { props: { class: '' } }],
['with ui', { props: { ui: {} } }],
// Slots
['with default slot', { slots: { default: () => 'Default slot' } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: TreeProps, slots?: Partial<TreeSlots> }) => {
const html = await ComponentRender(nameOrHtml, options, Tree)
expect(html).toMatchSnapshot()
})
})
Loading