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, 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 () => (
0 ? styles.border : '' ]}> {props.categoryChildList.length > 0 ? ( { forms.wikiCategoryId = props.wikiCategoryId; forms.wikiCategoryIdChild = ''; onSearch(); }}> 全部 ) : ( )}
{ // // if (val > forms.maxIndex) { // forms.maxIndex = val; // carouselRef.value?.to(0); // } forms.currentIndex = val; }}> {props.categoryChildList.map((item: any) => ( { forms.wikiCategoryId = item.id; onSearch(); }}> {item.name} ))} {state.showSlide && (
onChangeSlide('left')}>
onChangeSlide('right')}>
)}
{ forms.keyword = val; onSearch(); }} />
{childList.value.length > 0 && (
{childList.value.map((music: any, index: number) => ( <> {music.childrenList.length > 0 ? ( { onSearch(); }} key={music.id} class={styles.popSelect}> {selectChildObj(music.childrenList, index).name || music.name} ) : ( { forms.wikiCategoryIdChild = music.id; onSearch(); }}> {music.name} )} ))}
)}
); } });