• taro3.x: 简单点评实现-chooseImage,uploadFile, Textarea


    tsx:

    import React, { useState } from 'react'
    import Taro, { getCurrentInstance } from '@tarojs/taro'
    import { Textarea, View, Text, Image } from '@tarojs/components'
    
    import api from '@services/api'
    import app from '@services/request'
    import NavBar from '@components/navbar'
    import './index.scss'
    
    interface ITextData {
        count?: number,
        value: string
    }
    
    const INIT_TEXT_DATA = {
        value: '',
        count: 0
    }
    
    const HouseCommentForm = () => {
        const textCount = 150
        const router = getCurrentInstance().router
        const [textData, setTextData] = useState<ITextData>(INIT_TEXT_DATA)
        const [imagePath, setImagePath] = useState<string>('')
    
        const handleInputChange = (e: any) => {
            const { cursor, value } = e.detail
            setTextData({
                value,
                count: cursor
            })
        }
    
        const handleUploadImage = () => {
            Taro.chooseImage({
                count: 1,
                success: ((res: any) => {
                    Taro.uploadFile({
                        url: app.areaApiUrl(api.uploadFile),
                        filePath: res.tempFilePaths[0],
                        name: 'file',
                        formData: {
                            file: res.tempFiles[0]
                        },
                        header: {
                            'X-Token': 'zrggSr9n1XFzDeaD'
                        },
                        success: ((result: any) => {
                            setImagePath(JSON.parse(result.data).data)
                        })
                    })
                })
            })
        }
    
        const submitComment = () => {
            app.request({
                url: app.areaApiUrl(api.postHouseComment),
                method: 'POST',
                data: {
                    fang_house_id: router?.params.id,
                    content: textData.value,
                    image_path: imagePath
                }
            })
        }
    
        return (
            <View className="comment-form">
                <NavBar title="写评论" back={true}></NavBar>
                <View className="comment-wrapper">
                    <View className="comment-title">
                        写下对【】的点评
                    </View>
                    <View className="comment-input">
                        <Textarea
                            autoFocus
                            autoHeight
                            maxlength={textCount}
                            placeholderClass="small-desc"
                            className="comment-input-text"
                            placeholder="说说你的看法"
                            onInput={handleInputChange}
                        />
                        <View className="comment-input-image">
                            <View className="image-wrapper">
                                {
                                    imagePath &&
                                    <View className="image-show">
                                        <Text className="iconfont iconclear" onClick={() => setImagePath('')}></Text>
                                        <Image src={imagePath} />
                                    </View>
                                }
                            </View>
                            <View className="upload-btn" onClick={handleUploadImage}>
                                <Text className="text">上传图片</Text>
                            </View>
                        </View>
                        <View className="comment-input-count">{textData.count}/{textCount}</View>
                    </View>
                    <View className="comment-action">
                        <View className="btn btn-primary" onClick={submitComment}>提交评论</View>
                    </View>
                </View>
            </View>
        )
    }
    
    export default HouseCommentForm

    css:

    .comment-form {
        width: 100%;
        height: 100vh;
        background-color: $bg-color;
        font-size: $font-basic;
        .comment-wrapper {
            padding: 0 30px;
            .comment-title {
                margin-top: 30px;
                color: $text-color;
            }
    
            .comment-input {
                position: relative;
                background-color: $white;
                padding: 20px;
                margin: 20px 0;
                border-radius: $border-radius-10;
                &-text {
                    width: 100%;
                }
    
                &-image {
                    .image-wrapper {
                        position: relative;
                        width: 180px;
                        height: 160px;
                        margin: 30px 0;
    
                        .image-show {
                            width: 100%;
                            height: 100%;
                            background-color: $bg-color;
                            Image {
                                width: 100%;
                                height: 100%;
                            }
    
                            .iconfont {
                                position: absolute;
                                top: 10px;
                                right: 10px;
                                width: 40px;
                                height: 40px;
                                color: $text-color;
                            }
                        }
                    }
    
                    .upload-btn {
                        width: 180px;
                        height: 60px;
                        line-height: 60px;
                        text-align: center;
                        border: 1px solid $primary-color;
                        border-radius: $border-radius-10;
                        color: $primary-color;
                    }
                }
    
                .comment-input-count {
                    position: absolute;
                    bottom: 30px;
                    right: 30px;
                    color: $text-color;
                }
            }
        }
    }

    图例:

  • 相关阅读:
    设置win 10 软件开机启动
    win10 无法使用内置管理员账户打开应用
    设置 npm 源为淘宝镜像
    git sh.exe 乱码
    mybatis 获取新增数据的主键
    框架 get 请求乱码
    js在浏览器下的区别小结(部分)
    js调试的一点小知识
    git常用命令
    css水平居中(一)
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13750222.html
Copyright © 2020-2023  润新知