wd-sidebar.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <view :class="`wd-sidebar ${customClass}`" :style="customStyle">
  3. <slot></slot>
  4. <view class="wd-sidebar__padding"></view>
  5. </view>
  6. </template>
  7. <script lang="ts">
  8. export default {
  9. name: 'wd-sidebar',
  10. options: {
  11. addGlobalClass: true,
  12. virtualHost: true,
  13. styleIsolation: 'shared'
  14. }
  15. }
  16. </script>
  17. <script lang="ts" setup>
  18. import { isFunction } from '../common/util'
  19. import { useChildren } from '../composables/useChildren'
  20. import { SIDEBAR_KEY, sidebarProps } from './types'
  21. const props = defineProps(sidebarProps)
  22. const emit = defineEmits(['change', 'update:modelValue'])
  23. const { linkChildren } = useChildren(SIDEBAR_KEY)
  24. linkChildren({ props, setChange })
  25. /**
  26. * 子项状态变更
  27. * @param value 目标值
  28. * @param label 目标值标题
  29. */
  30. function setChange(value: number | string, label: string) {
  31. if (isFunction(props.beforeChange)) {
  32. props.beforeChange({
  33. value: value,
  34. resolve: (pass: boolean) => {
  35. if (pass) {
  36. updateValue(value, label)
  37. }
  38. }
  39. })
  40. } else {
  41. updateValue(value, label)
  42. }
  43. }
  44. /**
  45. * 更新选中状态
  46. * @param value 目标值
  47. * @param label 目标值标题
  48. */
  49. function updateValue(value: number | string, label: string) {
  50. emit('update:modelValue', value)
  51. emit('change', { value, label })
  52. }
  53. </script>
  54. <style lang="scss" scoped>
  55. @import './index.scss';
  56. </style>