123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- import {
- PropType,
- computed,
- defineComponent,
- nextTick,
- onMounted,
- reactive,
- ref
- } from 'vue';
- import styles from './index.module.less';
- import {
- NButton,
- NCarousel,
- NCarouselItem,
- NForm,
- NFormItem,
- NImage,
- NPopselect,
- NSpace
- } from 'naive-ui';
- import TheSearch from '/src/components/TheSearch';
- import iconSlideRight from '/src/views/prepare-lessons/images/icon-slide-right.png';
- export default defineComponent({
- name: 'search-group',
- props: {
- categoryChildList: {
- type: Array as PropType<any>,
- default: () => []
- },
- wikiCategoryId: {
- type: String,
- default: ''
- }
- },
- emits: ['search', 'add'],
- expose: ['init'],
- setup(props, { emit }) {
- // const catchStore = useCatchStore();
- const forms = reactive({
- keyword: '',
- wikiCategoryId: props.wikiCategoryId || '',
- wikiCategoryIdChild: '',
- childIds: [] as any,
- currentIndex: 0
- });
- const carouselRef = ref();
- const onSearch = () => {
- emit('search', forms);
- };
- const selectChildObj = (item: any, index: number) => {
- const obj: any = {};
- item?.forEach((child: any) => {
- if (child.id === forms.wikiCategoryIdChild) {
- obj.selected = true;
- obj.name = child.name;
- }
- });
- return obj;
- };
- const childList = computed(() => {
- const categoryChildList = props.categoryChildList || [];
- const child = categoryChildList.find(
- (item: any) => item.id === forms.wikiCategoryId
- );
- if (child && child.childrenList.length) {
- child.childrenList.forEach((child: any) => {
- const i = child.childrenList;
- if (i && i.length > 0) {
- i.forEach((j: any) => {
- j.label = j.name;
- j.value = j.id;
- });
- i.unshift({
- label: '全部',
- value: child.id,
- name: child.name,
- id: child.id
- });
- }
- });
- return (
- [
- {
- label: '全部',
- value: '',
- id: '',
- name: '全部',
- childrenList: []
- },
- ...child.childrenList
- ] || []
- );
- }
- return [];
- });
- const state = reactive({
- showSlide: false
- });
- const onChangeSlide = (type: string) => {
- if (type === 'left') {
- carouselRef.value?.prev();
- } else if (type === 'right') {
- carouselRef.value?.next();
- }
- };
- onMounted(() => {
- nextTick(() => {
- // 最外层宽度
- const carouselContainer = document.querySelector('.carouselContainer');
- const carouselContainerWidth =
- (carouselContainer &&
- carouselContainer.getBoundingClientRect().width) ||
- 0;
- const slideDoms = document.querySelectorAll('.n-carousel__slide');
- let slideWidth = 0;
- slideDoms.forEach(doom => {
- const rect = doom.getBoundingClientRect();
- slideWidth += rect.width;
- });
- if (slideWidth >= carouselContainerWidth) {
- state.showSlide = true;
- }
- });
- });
- return () => (
- <div class={styles.searchGroup}>
- <div
- class={[
- styles.searchCatatory,
- childList.value.length > 0 ? styles.border : ''
- ]}>
- <NSpace size="small" class={styles.btnType}>
- {props.categoryChildList.length > 0 ? (
- <NButton
- type={
- forms.wikiCategoryId === props.wikiCategoryId
- ? 'primary'
- : 'default'
- }
- secondary={
- forms.wikiCategoryId === props.wikiCategoryId ? false : true
- }
- round
- size="small"
- focusable={false}
- onClick={() => {
- forms.wikiCategoryId = props.wikiCategoryId;
- forms.wikiCategoryIdChild = '';
- onSearch();
- }}>
- 全部
- </NButton>
- ) : (
- <span></span>
- )}
- <div class={[styles.carouselGroup]}>
- <NCarousel
- ref={carouselRef}
- slidesPerView={'auto'}
- loop={false}
- class={[styles.carouselContainer, 'carouselContainer']}
- showDots={false}
- // spaceBetween={20}
- draggable={state.showSlide}
- currentIndex={forms.currentIndex}
- onUpdate:currentIndex={(val: any) => {
- //
- // if (val > forms.maxIndex) {
- // forms.maxIndex = val;
- // carouselRef.value?.to(0);
- // }
- forms.currentIndex = val;
- }}>
- {props.categoryChildList.map((item: any) => (
- <NCarouselItem>
- <NButton
- type={
- forms.wikiCategoryId === item.id ? 'primary' : 'default'
- }
- secondary={
- forms.wikiCategoryId === item.id ? false : true
- }
- round
- size="small"
- focusable={false}
- onClick={() => {
- forms.wikiCategoryId = item.id;
- onSearch();
- }}>
- {item.name}
- </NButton>
- </NCarouselItem>
- ))}
- </NCarousel>
- {state.showSlide && (
- <NSpace class={styles.swipeControll}>
- <div onClick={() => onChangeSlide('left')}>
- <NImage
- previewDisabled
- class={[
- styles.leftIcon
- // forms.currentIndex === 0 && styles.disabled
- ]}
- src={iconSlideRight}
- />
- </div>
- <div onClick={() => onChangeSlide('right')}>
- <NImage
- // class={
- // // forms.currentIndex == forms.openTableList.length - 4 &&
- // styles.disabled
- // }
- previewDisabled
- src={iconSlideRight}
- />
- </div>
- </NSpace>
- )}
- </div>
- </NSpace>
- <TheSearch
- class={styles.inputSearch}
- placeholder="请输入名曲鉴赏关键词"
- round
- onSearch={(val: string) => {
- forms.keyword = val;
- onSearch();
- }}
- />
- </div>
- {childList.value.length > 0 && (
- <div class={[styles.collapseWrap]}>
- <NSpace class={[styles.spaceSection]}>
- {childList.value.map((music: any, index: number) => (
- <>
- {music.childrenList.length > 0 ? (
- <NPopselect
- options={music.childrenList}
- trigger="hover"
- v-model:value={forms.wikiCategoryIdChild}
- onUpdate:value={() => {
- onSearch();
- }}
- key={music.id}
- class={styles.popSelect}>
- <span
- class={[
- styles.textBtn,
- selectChildObj(music.childrenList, index).selected &&
- styles.textBtnActive
- ]}>
- {selectChildObj(music.childrenList, index).name ||
- music.name}
- <i class={styles.iconArrow}></i>
- </span>
- </NPopselect>
- ) : (
- <span
- class={[
- styles.textBtn,
- forms.wikiCategoryIdChild === music.id &&
- styles.textBtnActive
- ]}
- onClick={() => {
- forms.wikiCategoryIdChild = music.id;
- onSearch();
- }}>
- {music.name}
- </span>
- )}
- </>
- ))}
- </NSpace>
- </div>
- )}
- </div>
- );
- }
- });
|