index.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // #9463 bug,未更换声轨点击确定不应该重新加载,现在会导致切换错误
  19. const partIndexChanged = ref(false);
  20. const { partListNames, partIndex } = toRefs(props)
  21. let idx = partListNames.value.findIndex((item: any) => item.value === partIndex.value);
  22. idx = idx > -1 ? idx : 0;
  23. const selectIndex = ref(idx);
  24. const columns = computed(() => {
  25. return partListNames.value
  26. })
  27. // console.log(1111,partListNames.value, partIndex.value, selectIndex.value, columns.value, 999999)
  28. /**
  29. * 默认选中的
  30. * picker组件,3.x的版本可以使用defaultIndex,4.x的版本只能使用v-model传递
  31. * */
  32. const selValues = ref([partIndex.value]);
  33. const myPicker = ref();
  34. onMounted(() => {
  35. // console.log(myPicker.value,99999,selValues.value,props.partIndex)
  36. });
  37. return () => (
  38. <div class={styles.container}>
  39. <div class={styles.top}>
  40. <div class={styles.title}>请选择您练习的乐器</div>
  41. {/* <Icon name="cross" size={24} onClick={() => emit('close')} /> */}
  42. <span class={styles.closeIcon} onClick={() => emit("close")}></span>
  43. </div>
  44. <Picker
  45. ref={myPicker}
  46. class={styles.picker}
  47. defaultIndex={props.partIndex}
  48. v-model={selValues.value}
  49. showToolbar={false}
  50. columns={columns.value}
  51. visibleItemCount={Math.ceil(document.body.clientHeight / 44 / 3)}
  52. onChange={(row) => {
  53. // console.log(1111,'选择的索引', row)
  54. if (!partIndexChanged.value) partIndexChanged.value = true
  55. selectIndex.value = row.selectedValues[0]
  56. }}
  57. />
  58. <Button class={styles.button} type="primary" round block onClick={() => {
  59. console.log(1111,selectIndex.value)
  60. if (partIndexChanged.value) {
  61. emit('close', selectIndex.value)
  62. } else {
  63. emit('close', partIndex.value)
  64. }
  65. }
  66. }>
  67. 确定
  68. </Button>
  69. </div>
  70. )
  71. },
  72. })