zhengyiming
2 天以前 e662aa7d894a0b259dc1816e79514c1f0d38da9f
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<template>
  <ChooseInput :modelValue="inputValue" @click="handleOpen()" v-bind="$attrs"></ChooseInput>
  <Popup v-model:visible="popupVisible" position="bottom">
    <Picker
      :modelValue="[modelValue]"
      :columns="options"
      @cancel="popupVisible = false"
      @confirm="handleConfirm"
    ></Picker>
  </Popup>
</template>
 
<script setup lang="ts">
import ChooseInput from './ChooseInput.vue';
import { Popup, Picker } from '@nutui/nutui-taro';
import { convertOptions, ValueEnum } from 'senin-mini/utils';
import { computed, ref } from 'vue';
 
defineOptions({
  name: 'ChooseInputWithPicker',
});
 
type Props = {
  enumLabelKey?: string;
  enumValueKey?: string;
  valueEnum?: ValueEnum;
  modelValue: string | number;
};
 
const props = withDefaults(defineProps<Props>(), {
  enumLabelKey: 'name',
  enumValueKey: 'id',
});
 
const emit = defineEmits<{
  (e: 'update:modelValue', val: string | number): void;
}>();
 
const options = computed(() =>
  convertOptions(props.valueEnum, props.enumLabelKey, props.enumValueKey)
);
 
const inputValue = computed(
  () => options.value?.find((x) => x.value === props.modelValue)?.text ?? ''
);
 
const popupVisible = ref(false);
 
function handleConfirm({ selectedValue, selectedOptions }) {
  emit('update:modelValue', selectedOptions[0].value);
  popupVisible.value = false;
}
 
function handleOpen() {
  popupVisible.value = true;
}
</script>