mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-02-01 04:37:57 +01:00
chore(ButtonGroup): use Primitive and improve tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { tv, type VariantProps } from 'tailwind-variants'
|
import { tv, type VariantProps } from 'tailwind-variants'
|
||||||
|
import type { PrimitiveProps } from 'radix-vue'
|
||||||
import type { AppConfig } from '@nuxt/schema'
|
import type { AppConfig } from '@nuxt/schema'
|
||||||
import _appConfig from '#build/app.config'
|
import _appConfig from '#build/app.config'
|
||||||
import theme from '#build/ui/button-group'
|
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>
|
type ButtonGroupVariants = VariantProps<typeof buttonGroup>
|
||||||
|
|
||||||
export interface ButtonGroupProps {
|
export interface ButtonGroupProps extends Omit<PrimitiveProps, 'asChild'> {
|
||||||
size?: ButtonProps['size']
|
size?: ButtonProps['size']
|
||||||
orientation?: ButtonGroupVariants['orientation']
|
orientation?: ButtonGroupVariants['orientation']
|
||||||
class?: any
|
class?: any
|
||||||
ui?: Partial<typeof buttonGroup.slots>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ButtonGroupSlots {
|
export interface ButtonGroupSlots {
|
||||||
@@ -25,17 +25,15 @@ export interface ButtonGroupSlots {
|
|||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { provide, computed } from 'vue'
|
import { provide, computed } from 'vue'
|
||||||
|
import { Primitive } from 'radix-vue'
|
||||||
import { buttonGroupInjectionKey } from '#imports'
|
import { buttonGroupInjectionKey } from '#imports'
|
||||||
|
|
||||||
const props = withDefaults(defineProps<ButtonGroupProps>(), {
|
const props = withDefaults(defineProps<ButtonGroupProps>(), {
|
||||||
|
as: 'div',
|
||||||
orientation: 'horizontal'
|
orientation: 'horizontal'
|
||||||
})
|
})
|
||||||
defineSlots<ButtonGroupSlots>()
|
defineSlots<ButtonGroupSlots>()
|
||||||
|
|
||||||
const ui = computed(() => tv({ extend: buttonGroup, slots: props.ui })({
|
|
||||||
orientation: props.orientation
|
|
||||||
}))
|
|
||||||
|
|
||||||
provide(buttonGroupInjectionKey, computed(() => ({
|
provide(buttonGroupInjectionKey, computed(() => ({
|
||||||
orientation: props.orientation,
|
orientation: props.orientation,
|
||||||
size: props.size
|
size: props.size
|
||||||
@@ -43,7 +41,7 @@ provide(buttonGroupInjectionKey, computed(() => ({
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div :class="ui.base({ class: props.class })">
|
<Primitive :as="as" :class="buttonGroup({ orientation })">
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</Primitive>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -19,10 +19,7 @@ export const buttonGroupVariantWithRoot = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
slots: {
|
base: 'relative',
|
||||||
base: 'relative'
|
|
||||||
},
|
|
||||||
|
|
||||||
variants: {
|
variants: {
|
||||||
orientation: {
|
orientation: {
|
||||||
horizontal: 'inline-flex -space-x-px',
|
horizontal: 'inline-flex -space-x-px',
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ describe('ButtonGroup', () => {
|
|||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
// Props
|
// Props
|
||||||
['with as', { props: { as: 'div' } }],
|
['with as', { props: { as: 'section' } }],
|
||||||
['with class', { props: { class: '' } }],
|
['with class', { props: { class: 'absolute' } }],
|
||||||
['with ui', { props: { ui: {} } }],
|
|
||||||
// Slots
|
// Slots
|
||||||
['with default slot', {
|
['with default slot', {
|
||||||
slots: {
|
slots: {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ exports[`ButtonGroup > renders orientation vertical with default slot correctly
|
|||||||
</div>"
|
</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>"`;
|
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>
|
</button>
|
||||||
</div>"
|
</div>"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
exports[`ButtonGroup > renders with ui correctly 1`] = `"<div class="relative inline-flex -space-x-px"></div>"`;
|
|
||||||
|
|||||||
Reference in New Issue
Block a user