index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <script lang="ts" setup>
  2. import type { Recordable } from '@vben/types';
  3. import { reactive, ref } from 'vue';
  4. import {
  5. Page,
  6. VbenButton,
  7. VbenButtonGroup,
  8. VbenCheckButtonGroup,
  9. } from '@vben/common-ui';
  10. import { Button, Card, message } from 'ant-design-vue';
  11. import { useVbenForm } from '#/adapter/form';
  12. const radioValue = ref<string | undefined>('a');
  13. const checkValue = ref(['a', 'b']);
  14. const options = [
  15. { label: '选项1', value: 'a' },
  16. { label: '选项2', value: 'b' },
  17. { label: '选项3', value: 'c' },
  18. { label: '选项4', value: 'd' },
  19. { label: '选项5', value: 'e' },
  20. { label: '选项6', value: 'f' },
  21. ];
  22. function resetValues() {
  23. radioValue.value = undefined;
  24. checkValue.value = [];
  25. }
  26. function beforeChange(v: any, isChecked: boolean) {
  27. return new Promise((resolve) => {
  28. message.loading({
  29. content: `正在设置${v}${isChecked ? '选中' : '未选中'}...`,
  30. duration: 0,
  31. key: 'beforeChange',
  32. });
  33. setTimeout(() => {
  34. message.success({ content: `${v} 已设置成功`, key: 'beforeChange' });
  35. resolve(true);
  36. }, 2000);
  37. });
  38. }
  39. const compProps = reactive({
  40. beforeChange: undefined,
  41. disabled: false,
  42. gap: 0,
  43. showIcon: true,
  44. size: 'middle',
  45. } as Recordable<any>);
  46. const [Form] = useVbenForm({
  47. handleValuesChange(values) {
  48. Object.keys(values).forEach((k) => {
  49. if (k === 'beforeChange') {
  50. compProps[k] = values[k] ? beforeChange : undefined;
  51. } else {
  52. compProps[k] = values[k];
  53. }
  54. });
  55. },
  56. schema: [
  57. {
  58. component: 'RadioGroup',
  59. componentProps: {
  60. options: [
  61. { label: '大', value: 'large' },
  62. { label: '中', value: 'middle' },
  63. { label: '小', value: 'small' },
  64. ],
  65. },
  66. defaultValue: compProps.size,
  67. fieldName: 'size',
  68. label: '尺寸',
  69. },
  70. {
  71. component: 'RadioGroup',
  72. componentProps: {
  73. options: [
  74. { label: '无', value: 0 },
  75. { label: '小', value: 5 },
  76. { label: '中', value: 15 },
  77. { label: '大', value: 30 },
  78. ],
  79. },
  80. defaultValue: compProps.gap,
  81. fieldName: 'gap',
  82. label: '间距',
  83. },
  84. {
  85. component: 'Switch',
  86. defaultValue: compProps.showIcon,
  87. fieldName: 'showIcon',
  88. label: '显示图标',
  89. },
  90. {
  91. component: 'Switch',
  92. defaultValue: compProps.disabled,
  93. fieldName: 'disabled',
  94. label: '禁用',
  95. },
  96. {
  97. component: 'Switch',
  98. defaultValue: false,
  99. fieldName: 'beforeChange',
  100. label: '前置回调',
  101. },
  102. ],
  103. showDefaultActions: false,
  104. submitOnChange: true,
  105. });
  106. function onBtnClick(value: any) {
  107. const opt = options.find((o) => o.value === value);
  108. if (opt) {
  109. message.success(`点击了按钮${opt.label},value = ${value}`);
  110. }
  111. }
  112. </script>
  113. <template>
  114. <Page
  115. title="VbenButtonGroup 按钮组"
  116. description="VbenButtonGroup是一个按钮容器,用于包裹一组按钮,协调整体样式。VbenCheckButtonGroup则可以作为一个表单组件,提供单选或多选功能"
  117. >
  118. <Card title="基本用法">
  119. <template #extra>
  120. <Button type="primary" @click="resetValues">清空值</Button>
  121. </template>
  122. <p class="mt-4">按钮组:</p>
  123. <div class="mt-2 flex flex-col gap-2">
  124. <VbenButtonGroup v-bind="compProps" border>
  125. <VbenButton
  126. v-for="btn in options"
  127. :key="btn.value"
  128. variant="link"
  129. @click="onBtnClick(btn.value)"
  130. >
  131. {{ btn.label }}
  132. </VbenButton>
  133. </VbenButtonGroup>
  134. <VbenButtonGroup v-bind="compProps" border>
  135. <VbenButton
  136. v-for="btn in options"
  137. :key="btn.value"
  138. variant="outline"
  139. @click="onBtnClick(btn.value)"
  140. >
  141. {{ btn.label }}
  142. </VbenButton>
  143. </VbenButtonGroup>
  144. </div>
  145. <p class="mt-4">单选:{{ radioValue }}</p>
  146. <div class="mt-2 flex flex-col gap-2">
  147. <VbenCheckButtonGroup
  148. v-model="radioValue"
  149. :options="options"
  150. v-bind="compProps"
  151. />
  152. </div>
  153. <p class="mt-4">单选插槽:{{ radioValue }}</p>
  154. <div class="mt-2 flex flex-col gap-2">
  155. <VbenCheckButtonGroup
  156. v-model="radioValue"
  157. :options="options"
  158. v-bind="compProps"
  159. >
  160. <template #option="{ label, value }">
  161. <div class="flex items-center">
  162. <span>{{ label }}</span>
  163. <span class="ml-2 text-gray-400">{{ value }}</span>
  164. </div>
  165. </template>
  166. </VbenCheckButtonGroup>
  167. </div>
  168. <p class="mt-4">多选{{ checkValue }}</p>
  169. <div class="mt-2 flex flex-col gap-2">
  170. <VbenCheckButtonGroup
  171. v-model="checkValue"
  172. multiple
  173. :options="options"
  174. v-bind="compProps"
  175. />
  176. </div>
  177. </Card>
  178. <Card title="设置" class="mt-4">
  179. <Form />
  180. </Card>
  181. </Page>
  182. </template>