chore(ButtonGroup): use Primitive and improve tests

This commit is contained in:
Benjamin Canac
2024-05-29 14:41:13 +02:00
parent 627688cab5
commit dfc63b1bc0
4 changed files with 10 additions and 18 deletions

View File

@@ -1,5 +1,6 @@
<script lang="ts">
import { tv, type VariantProps } from 'tailwind-variants'
import type { PrimitiveProps } from 'radix-vue'
import type { AppConfig } from '@nuxt/schema'
import _appConfig from '#build/app.config'
import theme from '#build/ui/button-group'
@@ -11,11 +12,10 @@ const buttonGroup = tv({ extend: tv(theme), ...(appConfig.ui?.buttonGroup) })
type ButtonGroupVariants = VariantProps<typeof buttonGroup>
export interface ButtonGroupProps {
export interface ButtonGroupProps extends Omit<PrimitiveProps, 'asChild'> {
size?: ButtonProps['size']
orientation?: ButtonGroupVariants['orientation']
class?: any
ui?: Partial<typeof buttonGroup.slots>
}
export interface ButtonGroupSlots {
@@ -25,17 +25,15 @@ export interface ButtonGroupSlots {
<script setup lang="ts">
import { provide, computed } from 'vue'
import { Primitive } from 'radix-vue'
import { buttonGroupInjectionKey } from '#imports'
const props = withDefaults(defineProps<ButtonGroupProps>(), {
as: 'div',
orientation: 'horizontal'
})
defineSlots<ButtonGroupSlots>()
const ui = computed(() => tv({ extend: buttonGroup, slots: props.ui })({
orientation: props.orientation
}))
provide(buttonGroupInjectionKey, computed(() => ({
orientation: props.orientation,
size: props.size
@@ -43,7 +41,7 @@ provide(buttonGroupInjectionKey, computed(() => ({
</script>
<template>
<div :class="ui.base({ class: props.class })">
<Primitive :as="as" :class="buttonGroup({ orientation })">
<slot />
</div>
</Primitive>
</template>

View File

@@ -19,10 +19,7 @@ export const buttonGroupVariantWithRoot = {
}
export default {
slots: {
base: 'relative'
},
base: 'relative',
variants: {
orientation: {
horizontal: 'inline-flex -space-x-px',

View File

@@ -9,9 +9,8 @@ describe('ButtonGroup', () => {
it.each([
// Props
['with as', { props: { as: 'div' } }],
['with class', { props: { class: '' } }],
['with ui', { props: { ui: {} } }],
['with as', { props: { as: 'section' } }],
['with class', { props: { class: 'absolute' } }],
// Slots
['with default slot', {
slots: {

View File

@@ -12,7 +12,7 @@ exports[`ButtonGroup > renders orientation vertical with default slot correctly
</div>"
`;
exports[`ButtonGroup > renders with as correctly 1`] = `"<div class="relative inline-flex -space-x-px" as="div"></div>"`;
exports[`ButtonGroup > renders with as correctly 1`] = `"<section class="relative inline-flex -space-x-px"></section>"`;
exports[`ButtonGroup > renders with class correctly 1`] = `"<div class="relative inline-flex -space-x-px"></div>"`;
@@ -87,5 +87,3 @@ exports[`ButtonGroup > renders with size xs correctly 1`] = `
</button>
</div>"
`;
exports[`ButtonGroup > renders with ui correctly 1`] = `"<div class="relative inline-flex -space-x-px"></div>"`;