search-group-resources.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { PropType, defineComponent, onMounted, reactive, ref } from 'vue';
  2. import styles from './index.module.less';
  3. import { NButton, NCarousel, NCarouselItem, NImage, NSpace } from 'naive-ui';
  4. import TheSearch from '/src/components/TheSearch';
  5. import iconSlideRight from '/src/views/prepare-lessons/images/icon-slide-right.png';
  6. import { nextTick } from 'process';
  7. export default defineComponent({
  8. name: 'search-group',
  9. props: {
  10. categoryChildList: {
  11. type: Array as PropType<any>,
  12. default: () => []
  13. },
  14. wikiCategoryId: {
  15. type: String,
  16. default: ''
  17. }
  18. },
  19. emits: ['search', 'add'],
  20. expose: ['init'],
  21. setup(props, { emit }) {
  22. // const catchStore = useCatchStore();
  23. const forms = reactive({
  24. currentIndex: 0,
  25. keyword: '',
  26. wikiCategoryId: props.wikiCategoryId || '',
  27. maxIndex: 0
  28. });
  29. const state = reactive({
  30. showSlide: false
  31. });
  32. const onSearch = () => {
  33. emit('search', forms);
  34. };
  35. const carouselRef = ref();
  36. const onChangeSlide = (type: string) => {
  37. if (type === 'left') {
  38. carouselRef.value?.prev();
  39. } else if (type === 'right') {
  40. carouselRef.value?.next();
  41. }
  42. };
  43. onMounted(async () => {
  44. // 获取教材分类列表
  45. // await catchStore.getMusicSheetCategory()
  46. // nextTick(() => {
  47. // carouselRef.value?.to(100);
  48. // });
  49. nextTick(() => {
  50. // carouselRef.value?.to(100);
  51. // 最外层宽度
  52. const carouselContainer = document.querySelector('.carouselContainer');
  53. const carouselContainerWidth =
  54. (carouselContainer &&
  55. carouselContainer.getBoundingClientRect().width) ||
  56. 0;
  57. const slideDoms = document.querySelectorAll('.n-carousel__slide');
  58. let slideWidth = 0;
  59. slideDoms.forEach(doom => {
  60. const rect = doom.getBoundingClientRect();
  61. slideWidth += rect.width;
  62. });
  63. if (slideWidth >= carouselContainerWidth) {
  64. state.showSlide = true;
  65. }
  66. });
  67. });
  68. return () => (
  69. <div class={styles.searchGroup}>
  70. <div class={[styles.searchCatatory]}>
  71. <NSpace size="small" class={styles.btnType}>
  72. {props.categoryChildList.length > 0 ? (
  73. <NButton
  74. type={
  75. forms.wikiCategoryId === props.wikiCategoryId
  76. ? 'primary'
  77. : 'default'
  78. }
  79. secondary={
  80. forms.wikiCategoryId === props.wikiCategoryId ? false : true
  81. }
  82. round
  83. size="small"
  84. focusable={false}
  85. onClick={() => {
  86. forms.wikiCategoryId = props.wikiCategoryId;
  87. onSearch();
  88. }}>
  89. 全部
  90. </NButton>
  91. ) : (
  92. ''
  93. )}
  94. <div class={styles.carouselGroup}>
  95. <NCarousel
  96. ref={carouselRef}
  97. slidesPerView={'auto'}
  98. loop={false}
  99. class={[styles.carouselContainer, 'carouselContainer']}
  100. showDots={false}
  101. // spaceBetween={20}
  102. draggable={state.showSlide}
  103. currentIndex={forms.currentIndex}
  104. onUpdate:currentIndex={(val: any) => {
  105. //
  106. // if (val > forms.maxIndex) {
  107. // forms.maxIndex = val;
  108. // carouselRef.value?.to(0);
  109. // }
  110. forms.currentIndex = val;
  111. }}>
  112. {props.categoryChildList.map((item: any) => (
  113. <NCarouselItem>
  114. <NButton
  115. type={
  116. forms.wikiCategoryId === item.id ? 'primary' : 'default'
  117. }
  118. secondary={
  119. forms.wikiCategoryId === item.id ? false : true
  120. }
  121. round
  122. size="small"
  123. focusable={false}
  124. onClick={() => {
  125. forms.wikiCategoryId = item.id;
  126. onSearch();
  127. }}>
  128. {item.name}
  129. </NButton>
  130. </NCarouselItem>
  131. ))}
  132. </NCarousel>
  133. {state.showSlide && (
  134. <NSpace class={styles.swipeControll}>
  135. <div onClick={() => onChangeSlide('left')}>
  136. <NImage
  137. previewDisabled
  138. class={[
  139. styles.leftIcon
  140. // forms.currentIndex === 0 && styles.disabled
  141. ]}
  142. src={iconSlideRight}
  143. />
  144. </div>
  145. <div onClick={() => onChangeSlide('right')}>
  146. <NImage
  147. // class={
  148. // // forms.currentIndex == forms.openTableList.length - 4 &&
  149. // styles.disabled
  150. // }
  151. previewDisabled
  152. src={iconSlideRight}
  153. />
  154. </div>
  155. </NSpace>
  156. )}
  157. </div>
  158. </NSpace>
  159. <TheSearch
  160. class={styles.inputSearch}
  161. placeholder="请输入音乐家关键词"
  162. round
  163. onSearch={(val: string) => {
  164. forms.keyword = val;
  165. onSearch();
  166. }}
  167. />
  168. </div>
  169. </div>
  170. );
  171. }
  172. });