ButtonGroup.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <FlexView v-bind="$props">
  3. <slot />
  4. </FlexView>
  5. </template>
  6. <script setup lang="ts">
  7. import FlexView, { type FlexProps } from '../layout/FlexView.vue';
  8. import { useChildLinkParent } from '../composeabe/ChildItem';
  9. import { computed, provide, ref, toRef, type ComputedRef, type Ref } from 'vue';
  10. export interface ButtonGroupProp extends FlexProps {
  11. /**
  12. * 是否禁用按钮组下的全部按钮
  13. * @default false
  14. */
  15. disabled?: boolean;
  16. /**
  17. * 按钮组间距。为0时会自动合并按钮,使其圆角自动合并
  18. * @default 0
  19. */
  20. gap?: number;
  21. }
  22. defineOptions({
  23. options: {
  24. styleIsolation: "shared",
  25. virtualHost: true,
  26. }
  27. })
  28. const props = withDefaults(defineProps<ButtonGroupProp>(), {
  29. direction: 'row',
  30. disabled: false,
  31. gap: 0,
  32. });
  33. const mergeRadius = computed(() => {
  34. return props.gap === 0;
  35. });
  36. const length = ref(0);
  37. const {
  38. getPosition,
  39. getLength,
  40. resetCounter,
  41. } = useChildLinkParent({
  42. onLengthChanged() {
  43. length.value = getLength();
  44. },
  45. });
  46. export interface ButtonGroupContext {
  47. groupDisabled: Ref<boolean>;
  48. mergeRadius: ComputedRef<boolean>;
  49. length: typeof length;
  50. resetCounter: typeof resetCounter;
  51. getPosition: typeof getPosition;
  52. }
  53. provide<ButtonGroupContext>('buttonGroupContext', {
  54. groupDisabled: toRef(props.disabled),
  55. mergeRadius,
  56. length,
  57. getPosition,
  58. resetCounter,
  59. });
  60. </script>
  61. <style>
  62. </style>