Files
mini-programs/src/publish_pages/publishBall/components/WechatSwitch/WechatSwitch.tsx
2025-09-12 16:29:03 +08:00

68 lines
2.2 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 React, { useCallback, useState } from 'react'
import { View, Text, Input } from '@tarojs/components'
import { Checkbox } from '@nutui/nutui-react-taro'
import styles from './index.module.scss'
interface FormSwitchProps {
value: boolean
onChange: (checked: boolean) => void
subTitle: string
wechatId?: string
}
const FormSwitch: React.FC<FormSwitchProps> = ({ value, onChange, subTitle, wechatId }) => {
const [editWechat, setEditWechat] = useState(false)
const [wechatIdValue, setWechatIdValue] = useState('')
const [wechat, setWechat] = useState(wechatId)
const editWechatId = () => {
setEditWechat(true)
}
const setWechatId = useCallback((e: any) => {
const value = e.target.value
onChange && onChange(value)
setWechatIdValue(value)
}, [onChange])
const fillWithPhone = () => {
if (wechat) {
setWechatIdValue(wechat)
}
}
return (
<>
<View className={styles['wechat-contact-section']}>
<View className={styles['wechat-contact-item']}>
<Checkbox
className={styles['wechat-contact-checkbox']}
checked={value}
onChange={onChange}
/>
<View className={styles['wechat-contact-content']}>
<Text className={styles['wechat-contact-text']}>{subTitle}</Text>
</View>
</View>
{
!editWechat && wechatId && (
<View className={styles['wechat-contact-id']}>
<Text className={styles['wechat-contact-text']}>: {wechatId.replace(/(\d{3})(\d{4})(\d{4})/, '$1 $2 $3')}</Text>
<View className={styles['wechat-contact-edit']} onClick={editWechatId}></View>
</View>
)
}
{
editWechat && (
<View className={styles['wechat-contact-id']}>
<View className={styles['wechat-contact-edit-input']}>
<Input value={wechatIdValue} onInput={setWechatId} placeholder='请输入正确微信号' />
</View>
<View className={styles['wechat-contact-edit']} onClick={fillWithPhone}>{wechat}</View>
</View>
)
}
</View>
</>
)
}
export default FormSwitch