2022-12-26 19:25:11 +08:00

103 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { CommonEventFunction, TextareaProps } from '@tarojs/components'
import { Button, Image, Text, Textarea, View } from '@tarojs/components'
import type { Ref } from 'react'
import { forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react'
import classNames from 'classnames'
import styles from './index.module.scss'
import Popup from '@/components/popup'
import NormalButton from '@/components/normalButton'
interface Params {
title?: string
value?: string
isCompanyName?: boolean
save: (value: any) => void // 确定保存
}
export interface ModifyModalRef {
setModalShow: (val: boolean) => void
}
const ModifyModal = (props: Params, ref: Ref<ModifyModalRef>) => {
const { title = '修改昵称', value: _value = '', isCompanyName = false } = props
const [value, setValue] = useState('')
const [tipsComp, setTipsComp] = useState<React.ReactNode | null>(null)
// 不合法状态
const isInvalidate = useMemo(() => {
let flag = false
// 仅允输入类型为数字、英文、中文
const Reg = /^[a-zA-Z0-9\u4E00-\u9FA5]+$/
if (!Reg.test(value)) {
flag = true
setTipsComp(() => {
return <View className={styles['warn-tips']}>
</View>
})
return flag
}
// 修改组织名称
if (isCompanyName) {
// TODO 检测是否没机会修改
if (true) {
flag = false
setTipsComp(() => {
return <View className={styles['warn-tips']}>
<View></View>
<View></View>
</View>
})
}
else {
flag = true
setTipsComp(() => {
return <View className={styles['warn-tips']}>
<View></View>
<View></View>
</View>
})
}
}
return flag
}, [value, isCompanyName])
useEffect(() => {
setValue(_value)
}, [_value])
// popup输入长度
const handleTextareaInput: CommonEventFunction<TextareaProps.onInputEventDetail> = (ev) => {
setValue(ev.detail.value.slice(0, 20))
}
// 重置
const handleTextareaReset = () => {
setValue('')
}
const [textareaBottom, setTextareaBottom] = useState(0)
const handleBlur: CommonEventFunction<TextareaProps.onBlurEventDetail> = () => {
setTextareaBottom(0)
}
const handleFocus: CommonEventFunction<TextareaProps.onFocusEventDetail> = (ev) => {
setTextareaBottom(ev.detail.height)
}
const [modalShow, setModalShow] = useState(false)
useImperativeHandle(ref, () => ({ setModalShow }))
return (
<Popup onClose={() => setModalShow(false)} title={title} show={modalShow}>
<View style={{ marginBottom: `${textareaBottom}px` }} className={styles['modify-ickname-content']}>
<View className={styles['modify-ickname-input']}>
<Textarea showConfirmBar={false} auto-focus adjustPosition={false} value={value} onBlur={handleBlur} onFocus={handleFocus} onInput={handleTextareaInput} maxlength={20} />
<Text>{value?.length}/20</Text>
</View>
<View className={styles['modify-ickname-tips']}>2-20</View>
{tipsComp}
<View className={styles['modify-ickname-operation']}>
<NormalButton onClick={handleTextareaReset} type="info" customClassName={classNames(styles.button, styles['modify-ickname-operation-reset'])}></NormalButton>
<NormalButton disabled={isInvalidate} onClick={() => props.save(value)} type="primary" customClassName={classNames(styles.button, styles['modify-ickname-operation-save'])}></NormalButton>
</View>
</View>
</Popup>
)
}
export default memo(forwardRef(ModifyModal))