mx-condition-dropdown-item.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <view class="fx-row items-center gap-5" @click="handleClick" @touchmove.stop>
  3. <text class="whitespace-nowrap relative" :class="{'text-primary': opening, 'text-fired': fired}">
  4. {{ display }}
  5. </text>
  6. <uv-icon :name="opening?'arrow-up':'arrow-down'" :color="opening?'primary':undefined"/>
  7. </view>
  8. </template>
  9. <script setup>
  10. import {computed} from 'vue';
  11. import {createPropDefine} from "@/utils";
  12. import {useInjectSearchModel} from "@/components/mx-condition/useSearchModelInjection";
  13. import {toValue} from "@vueuse/core";
  14. import {useInjectConditionDropdownPopup} from "@/components/mx-condition-dropdown/useConditionDropdownPopupInjection";
  15. const props = defineProps({
  16. condition: createPropDefine({}, Object),
  17. useValueAsTitle: createPropDefine(false, Boolean)
  18. })
  19. const {popup} = useInjectConditionDropdownPopup()
  20. const {queryParams} = useInjectSearchModel()
  21. const list = computed(() => props.condition.list)
  22. const config = computed(() => props.condition.config)
  23. const current = computed(() => toValue(queryParams)[config.value.key])
  24. const currentItems = computed(() => list.value.filter(i => [].concat(current.value).includes(getValue(i))))
  25. const display = computed(() => {
  26. if (props.useValueAsTitle && currentItems.value.length == 1) return getLabel(currentItems.value[0])
  27. return config.value.title
  28. })
  29. const fired = computed(() => {
  30. // 命中的是默认条件
  31. if (currentItems.value.length == 1 && !getValue(currentItems.value[0])) return false
  32. // 有条件命中了
  33. return !props.useValueAsTitle && currentItems.value.length > 0
  34. })
  35. const opening = computed(() => popup.value?.condition == props.condition) // 当前弹窗打开
  36. const getLabel = (item) => config.value.keyName ? item[config.value.keyName] : item
  37. const getValue = (item) => config.value.keyValue ? item[config.value.keyValue] : item
  38. const handleClick = () => {
  39. popup.value.open(props.condition)
  40. }
  41. </script>
  42. <style scoped lang="scss">
  43. .text-fired::before {
  44. content: ' ';
  45. position: absolute;
  46. top: 0;
  47. right: -3px;
  48. width: 6px;
  49. height: 6px;
  50. border-radius: 3px;
  51. background-color: var(--error-light-color);
  52. }
  53. </style>