index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { PropType, computed, defineComponent, ref, toRefs, onMounted } from 'vue'
  2. import { Picker, Button, Icon } from 'vant'
  3. import styles from './index.module.less'
  4. export default defineComponent({
  5. name: 'choosePartName',
  6. props: {
  7. partListNames: {
  8. type: Array as PropType<string[]>,
  9. default: () => [],
  10. },
  11. partIndex: {
  12. type: Number,
  13. default: 0,
  14. },
  15. },
  16. emits: ['close'],
  17. setup(props, { emit }) {
  18. const { partListNames, partIndex } = toRefs(props)
  19. const selectIndex = ref((partListNames.value[partIndex.value] as any).value)
  20. const columns = computed(() => {
  21. return partListNames.value
  22. })
  23. // console.log(partListNames.value, partIndex.value, selectIndex.value, columns.value, 999999)
  24. /**
  25. * 默认选中的
  26. * picker组件,3.x的版本可以使用defaultIndex,4.x的版本只能使用v-model传递
  27. * */
  28. const selValues = ref([partIndex.value]);
  29. const myPicker = ref();
  30. onMounted(() => {
  31. // console.log(myPicker.value,99999,selValues.value,props.partIndex)
  32. });
  33. return () => (
  34. <div class={styles.container}>
  35. <div class={styles.top}>
  36. <div class={styles.title}>请选择您练习的乐器</div>
  37. <Icon name="cross" size={24} onClick={() => emit('close')} />
  38. </div>
  39. <Picker
  40. ref={myPicker}
  41. class={styles.picker}
  42. defaultIndex={props.partIndex}
  43. v-model={selValues.value}
  44. showToolbar={false}
  45. columns={columns.value}
  46. visibleItemCount={Math.ceil(document.body.clientHeight / 44 / 3)}
  47. onChange={(row) => {
  48. // console.log('选择的索引', row)
  49. selectIndex.value = row.selectedValues[0]
  50. }}
  51. />
  52. <Button class={styles.button} type="primary" round block onClick={() => {
  53. emit('close', selectIndex.value)}
  54. }>
  55. 确定
  56. </Button>
  57. </div>
  58. )
  59. },
  60. })