index.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <div class="cip-input-switch">
  3. <el-checkbox v-model="effective" class="cip-input-switch__switch"></el-checkbox>
  4. <slot name="input" :model-value="modelValue" :update-model-value="updateModelValue" :disabled="!effective"></slot>
  5. </div>
  6. </template>
  7. <script>
  8. import { ref, watchEffect, watch } from 'vue'
  9. import { ElCheckbox } from 'element-plus'
  10. import { isNotEmpty } from '@cip/utils/util'
  11. import { generateProps } from '../helper/component-util'
  12. import { componentScheme } from './component.scheme'
  13. export default {
  14. name: 'CipInputSwitch',
  15. components: { ElCheckbox },
  16. props: generateProps(componentScheme),
  17. emits: ['update:modelValue'],
  18. setup (props, { emit }) {
  19. const effective = ref(false)
  20. watchEffect(() => {
  21. if (isNotEmpty(props.modelValue)) {
  22. effective.value = true
  23. }
  24. })
  25. watch(effective, (val) => {
  26. if (!val) {
  27. emit('update:modelValue', undefined)
  28. } else {
  29. }
  30. })
  31. const updateModelValue = (val) => {
  32. if (effective.value) {
  33. emit('update:modelValue', val)
  34. } else {
  35. emit('update:modelValue', undefined)
  36. }
  37. }
  38. return {
  39. effective,
  40. updateModelValue
  41. }
  42. }
  43. }
  44. </script>
  45. <style lang="less">
  46. .cip-input-switch{
  47. display: flex;
  48. &__switch{
  49. margin-right: 8px !important;
  50. }
  51. }
  52. </style>