官方文档

安装

  1. yarn add react-vant@next

DateTimePicker组件

DateTimePicker默认只支持touch事件,要在桌面端能用,需安装vant touch emulator
为了方便日期格式转换,使用dayjs

  1. yarn add @vant/touch-emulator
  2. yarn add dayjs

注意这里给日历添加了遮罩层

  1. import { DatetimePicker } from 'react-vant'
  2. import '@vant/touch-emulator'
  3. import dayjs from 'dayjs'
  4. // 添加遮罩层
  5. const DateTimeWrapper = styled.div`
  6. position: absolute;
  7. width: 100%;
  8. background: rgba(0, 0, 0, 0.3);
  9. height: 100%;
  10. z-index: 1;
  11. .rv-picker {
  12. position: absolute;
  13. bottom: 0;
  14. left: 0;
  15. width: 100%;
  16. }
  17. `
  18. function Money() {
  19. const [datePickerShow, setDatePickerShow] = useState(false)
  20. const [pickedDate, setPickedDate] = useState(dayjs())
  21. const [displayDate, setDisplayDate] = useState(dayjs().format('YYYY/MM/DD'))
  22. useEffect(() => {
  23. setDisplayDate(dayjs(pickedDate).format('YYYY/MM/DD'))
  24. }, [pickedDate])
  25. const confirmPickedDate = (value: Date) => {
  26. setDatePickerShow(!datePickerShow) // 隐藏日期组件需写在前面,否则报错
  27. setPickedDate(dayjs(value))
  28. }
  29. return (
  30. <MyLayout scrollTop={9999}>
  31. {datePickerShow ? (
  32. <DateTimeWrapper
  33. onClick={(e) => {
  34. // 判断点击的是遮罩层自身
  35. if (e.target === e.currentTarget) {
  36. setDatePickerShow(false)
  37. }
  38. }}
  39. className="datetimewrapper"
  40. >
  41. <DatetimePicker
  42. title="选择年月日"
  43. type="date"
  44. minDate={new Date(2020, 0, 1)}
  45. maxDate={new Date()}
  46. value={new Date()}
  47. onCancel={() => setDatePickerShow(false)}
  48. onConfirm={(value: Date) => confirmPickedDate(value)}
  49. />
  50. </DateTimeWrapper>
  51. ) : (
  52. ''
  53. )}
  54. </MyLayout>
  55. )
  56. }
  57. export default Money

Notify组件代替window.alert

  1. import { Notify } from 'react-vant';
  2. Notify.show('通知内容');
  3. Notify.show({ type: 'primary', message: '通知内容' })
  4. // type: 'success' 'warning' 'danger'

Dialog组件代替window.confirm

  1. import { Dialog } from 'react-vant';
  2. Dialog.confirm({
  3. title: '提示',
  4. message: '是否删除该标签?',
  5. })
  6. .then(() => {
  7. const result = deleteTag(tag.id)
  8. if (result === 'success') {
  9. history.goBack()
  10. Notify.show({ type: 'success', message: '删除成功' })
  11. }
  12. })
  13. .catch(() => {
  14. console.log('catch')
  15. })

自定义样式

  1. @media (min-width: 500px) {
  2. .rv-popup--top,
  3. .rv-overlay {
  4. width: 500px;
  5. left: 50%;
  6. transform: translateX(-50%);
  7. }
  8. }

使用 Dialog 和 Field 组件封装弹出输入框组件

  1. import { useTags } from 'hooks/useTags'
  2. import React, { useEffect, useRef, useState } from 'react'
  3. import { Dialog, Field, FieldInstance } from 'react-vant'
  4. type Category = '-' | '+'
  5. interface Props {
  6. visible: boolean
  7. setVisible: (value: boolean) => void
  8. category: Category
  9. addTag: (type: Category, tag: string) => void
  10. }
  11. export const Prompt: React.FC<Props> = (props) => {
  12. const { visible, setVisible, category, addTag } = props
  13. const [inputValue, setInputValue] = useState('')
  14. const fieldRef = useRef<FieldInstance>(null)
  15. useEffect(() => {
  16. fieldRef.current?.focus() // 输入框获取焦点
  17. }, [visible])
  18. return (
  19. <Dialog
  20. visible={visible}
  21. title="请输入标签名"
  22. showCancelButton
  23. onConfirm={() => {
  24. setVisible(false)
  25. const value = inputValue
  26. setInputValue('')
  27. console.log(value)
  28. addTag(category, value)
  29. }}
  30. onCancel={() => {
  31. setVisible(false)
  32. setInputValue('')
  33. }}
  34. >
  35. <Field
  36. ref={fieldRef}
  37. placeholder="在这里输入标签名"
  38. value={inputValue}
  39. onChange={setInputValue}
  40. />
  41. </Dialog>
  42. )
  43. }