wupengfei
2025-02-18 a261170567648a5b425077049835ce8b7fb0c466
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<template>
  <ChooseInput :modelValue="inputValue" @click="handleOpen()"></ChooseInput>
</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 { Portal } from 'senin-mini/components';
import { computed, h } 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 ?? ''
);
 
function handleOpen() {
  const _modelValue = [props.modelValue];
  Portal.add((key) => {
    return h(
      Portal.Container,
      { keyNumber: key, delayOpen: true },
      {
        default: ({ open, onClose }) =>
          h(
            Popup,
            {
              visible: open.value,
              'onUpdate:visible': (value) => !value && onClose(),
              position: 'bottom',
            },
            {
              default: () =>
                h(Picker, {
                  modelValue: _modelValue,
                  columns: options.value,
                  onCancel: onClose,
                  onConfirm: ({ selectedValue, selectedOptions }) => {
                    console.log('selectedValue: ', selectedValue, selectedOptions);
                    emit('update:modelValue', selectedOptions[0].value);
                    onClose();
                  },
                }),
            }
          ),
      }
    );
  });
}
</script>