获取records数据,并使用dayjs库格式化日期

  1. function Statistics() {
  2. const [category, setCategory] = useState<'-'|'+'>('-')
  3. const {records} = useRecords()
  4. const {getTag} = useTags()
  5. return (
  6. <Layout>
  7. <CategoryWrapper>
  8. <CategorySection value={category}
  9. onChange={value => setCategory(value)}/>
  10. </CategoryWrapper>
  11. {records.map(record => <div key={record.createAt}>
  12. {record.tagIds.map(tagId => <span>{getTag(tagId)}</span>)}
  13. <hr />
  14. {record.amount}
  15. <hr />
  16. {day(record.createAt).format('YYYY年MM月DD日')}
  17. </div>)}
  18. </Layout>
  19. );
  20. }

将日期转化为今天昨天

  1. const dayTransform = (date: string) => {
  2. const now = day()
  3. if(day(date).isSame(now, 'day')){
  4. return '今天'
  5. } else if(day(date).isSame(now.subtract(1, 'day'), 'day')){
  6. return '昨天'
  7. }
  8. return date
  9. }

对数据进行归类和排序

  1. function Statistics() {
  2. const [category, setCategory] = useState<'-'|'+'>('-')
  3. const {records} = useRecords()
  4. const {getTag} = useTags()
  5. // 按日期归类
  6. const hash:{[date: string]: RecordItem[]} = {}
  7. records.filter(r => r.category === category).map(r => {
  8. const date = day(r.createAt).format('YYYY-MM-DD')
  9. if(!(date in hash)){
  10. hash[date] = []
  11. }
  12. hash[date].push(r)
  13. })
  14. // 排序
  15. const hashArray = Object.entries(hash).sort((a,b) => {
  16. if(a[0] === b[0]) return 0;
  17. if(a[0] > b[0]) return -1;
  18. if(a[0] < b[0]) return 1;
  19. return 0;
  20. })
  21. return (
  22. <Layout>
  23. <CategoryWrapper>
  24. <CategorySection value={category}
  25. onChange={value => setCategory(value)}/>
  26. </CategoryWrapper>
  27. {
  28. hashArray.map(([date, records]) => (
  29. <div key={date}>
  30. <Header>{dayTransform(date)}</Header>
  31. {records.map(record =>
  32. <Item key={record.createAt}>
  33. <div className="tags">
  34. {record.tagIds.map(tagId => <span key={tagId}>{getTag(tagId)}</span>)
  35. .reduce((result,span,index,array)=>
  36. result.concat(index < array.length - 1 ? [span, ','] : [span]), [] as ReactNode[])}
  37. </div>
  38. <div className="note">
  39. {record.note}
  40. </div>
  41. <div className="amount">
  42. ¥{record.amount}
  43. </div>
  44. </Item>)}
  45. </div>
  46. ))
  47. }
  48. </Layout>
  49. );
  50. }

金额汇总

使用number-precision库处理浮点数精度问题

  1. <Header>
  2. <span>{dayTransform(date)}</span>
  3. <span>¥{NP.strip(records.reduce((total, record) => total + record.amount, 0))}</span>
  4. </Header>

React调试工具

image.png

让页面自动滚动到底部

通过scrollTop属性设置滚动

  1. interface Props {
  2. children: React.ReactNode,
  3. className?: string,
  4. scrollTop?: number
  5. }
  6. const Layout = (props: Props) => {
  7. const mainRef = useRef<HTMLDivElement>(null)
  8. useEffect(()=>{
  9. setTimeout(()=>{
  10. if(!mainRef.current) return;
  11. mainRef.current.scrollTop = props.scrollTop!;
  12. },0)
  13. }, [props.scrollTop])
  14. return (
  15. <Wrapper>
  16. <Main className={props.className} ref={mainRef}>
  17. {props.children}
  18. </Main>
  19. <Nav />
  20. </Wrapper>
  21. )
  22. }
  23. Layout.defaultProps = {
  24. scrollTop: 0
  25. }