PickerItem.vue 914 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <script setup lang="ts">
  2. export interface PickerItemOption {
  3. label: string
  4. value: string | number
  5. }
  6. const props = defineProps<{
  7. modelValue: string | number
  8. options: PickerItemOption[]
  9. }>()
  10. const emits = defineEmits<{
  11. 'update:modelValue': [string | number]
  12. }>()
  13. function bindPickerChange(e: { detail: { value: number | string } }) {
  14. const currentIndex = e.detail.value as number
  15. emits('update:modelValue', props.options[currentIndex].value)
  16. }
  17. const index = ref(0)
  18. watch(() => props.modelValue, (currentValue) => {
  19. const currentIndex = props.options.findIndex(option => option.value === currentValue)
  20. index.value = currentIndex === -1 ? 0 : currentIndex
  21. }, { immediate: true })
  22. </script>
  23. <template>
  24. <picker :value="index" :range="options" range-key="label" @change="bindPickerChange">
  25. <view>
  26. {{ options[index].label }}
  27. </view>
  28. </picker>
  29. </template>
  30. <style scoped>
  31. </style>