发送验证码,间隔 60s
image.png
发送验证码
image.png

  1. import React, { useState } from 'react'
  2. import { Input, Button } from 'antd'
  3. interface IVerifyCodeProps {
  4. value?: string
  5. onChange?: (value: string) => void
  6. readyPost?: boolean
  7. phone?: number
  8. style?: React.CSSProperties
  9. }
  10. export const VerifyCode: React.FC<React.PropsWithChildren<IVerifyCodeProps>> =
  11. ({ value, onChange, readyPost, phone, ...props }) => {
  12. const [lastTime, setLastTime] = useState(0)
  13. const counting = (time = 60) => {
  14. if (time < 0) return
  15. setLastTime(time)
  16. setTimeout(() => {
  17. counting(time - 1)
  18. }, 1000)
  19. }
  20. return (
  21. <div
  22. style={{ display: 'inline-flex', width: '100%', alignItems: 'center' }}
  23. >
  24. <Input
  25. {...props}
  26. style={{ marginRight: 5, ...props.style }}
  27. value={value}
  28. onChange={onChange}
  29. />
  30. <div
  31. style={{
  32. flexShrink: 0,
  33. color: '#999',
  34. width: 100,
  35. height: 35,
  36. display: 'flex',
  37. alignItems: 'center',
  38. justifyContent: 'center',
  39. }}
  40. >
  41. {lastTime === 0 && (
  42. <Button
  43. disabled={!readyPost}
  44. block
  45. onClick={counting}
  46. >
  47. 发送验证码
  48. </Button>
  49. )}
  50. {lastTime > 0 && <span>剩余{lastTime}秒</span>}
  51. </div>
  52. </div>
  53. )
  54. }

手机号验证

  1. <Item
  2. label='手机号'
  3. name='phone'
  4. rules={[
  5. { required: true, message: '请输入手机号!' },
  6. { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
  7. ]}
  8. >
  9. <Input
  10. inputMode='numeric'
  11. placeholder='请输入手机号'
  12. />
  13. </Item>