File size: 868 Bytes
4d70170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * (Use with the DisabledChild mixin)
 * Allow disabling an entire tree of components implementing the DisabledChild mixin.
 */

import { computed, inject, provide, reactive, watch } from 'vue'

export function useDisabledParent(props: { disabled?: boolean }) {
  const injectedDisableData = reactive({
    value: props.disabled || false,
  })

  provide('VueDisableMixin', {
    data: injectedDisableData,
  })

  watch(
    () => props.disabled,
    (value, oldValue) => {
      if (value !== oldValue) {
        injectedDisableData.value = value
      }
    },
  )
}

export function useDisabledChild(props: { disabled?: boolean }) {
  const injectDisable = inject<{ data: { value: boolean } } | undefined>(
    'VueDisableMixin',
    null,
  )

  return {
    finalDisabled: computed(() => props.disabled || (injectDisable && injectDisable.data.value)),
  }
}