• taro3.x: 列表item折叠folder实现


    toggleFolder 里面设置setVideos(videos)不更新,单独设置setFolder更新页面
    import React, { useEffect, useState } from 'react'
    import { useShareAppMessage } from '@tarojs/taro'
    import { View, Video, Button, Text } from '@tarojs/components'
    import classnames from 'classnames'
    
    import app from '@services/request'
    import api from '@services/api'
    import './index.scss'
    
    const Videos = () => {
        const [folder, setFolder] = useState<boolean>(false)
        const [videos, setVideos] = useState<any[]>([])
    
        useShareAppMessage((res: any) => {
            const index = res.target.dataset.index
            const target = videos[index]
            return {
                title: target.title,
                imageUrl: target.image_path,
                path: `/pages/video/index?id=${target.id}`
            }
        })
    
        useEffect(() => {
            app.request({
                url: app.testApiUrl(api.getVideoList),
                data: {
                    page: 1,
                    limit: 20
                }
            }).then((result: any) => {
                setVideos(result.data)
            })
        }, [])
    
        const toggleFolder = (index: number) => {
            const target = videos[index]
            if (target.folder) {
                target.folderText = '展开'
                target.folderIcon = 'icon-down'
            } else {
                target.folderText = '收起'
                target.folderIcon = 'icon-up'
            }
            target.folder = !target.folder
            setFolder(!folder)
        }
    
        return (
            <View className="videos">
                {
                    videos.map((item: any, index: number) => (
                        <View key={index} className="item">
                            <View className="item-video">
                                <Video
                                    id='video'
                                    style={{  '100%' }}
                                    src={item.video_path}
                                    poster={item.image_path}
                                    controls={true}
                                    loop={false}
                                    muted={false}
                                />
                            </View>
                            <View className="item-text">
                                <View className="title">{item.title}</View>
                                <Button className="button" openType="share" data-index={index}>
                                    <Text className="iconfont icon-ArtboardCopy"></Text>
                                </Button>
                            </View>
                            {
                                item.folder &&
                                <View className="item-text">
                                    <View className="sub-title">{item.description}</View>
                                </View>
                            }
                            <View className="item-folder" onClick={() => toggleFolder(index)}>
                                <Text className={classnames('iconfont', item.folderIcon || 'icon-down')}></Text>
                                <Text className="folder-text">{item.folderText || '展开'}</Text>
                            </View>
                        </View>
                    ))
                }
            </View>
        )
    }
    
    export default React.memo(Videos)
  • 相关阅读:
    全区停水,测什么时候来水。
    和电脑打国标麻将,一定要注意
    这是一个可怕的交易
    起一卦,还是这样。什么时候能好起来。
    Linux内核源码真是个好东西
    命宫——天机化忌
    bzoj1024[SCOI2009]生日快乐
    bzoj2561最小生成树
    bzoj2423[HAOI2010]最长公共子序列
    bzoj2705[SDOI2012]Longge的问题
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13856605.html
Copyright © 2020-2023  润新知