12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useEffect, useMemo, useRef, useState } from 'react'
  2. import Taro from '@tarojs/taro';
  3. import { ScrollView, View, Image, Text } from '@tarojs/components';
  4. import deleteIcon from '@/assets/icons/landlord/delete.png';
  5. import './style.less'
  6. export default (props) => {
  7. const { className, action, del, onDelete } = props;
  8. const contextRef = useRef()
  9. const contentHeightRef = useRef(0)
  10. const [actStyle, setActStyle]= useState()
  11. const uqClass = useMemo(() => `f-${Math.random().toString(36).substring(2)}`, [])
  12. const classes = [className, 'slideview-wrapper', uqClass].filter(Boolean).join(' ');
  13. const handleScrollLeft = () => {
  14. if (contextRef.current) {
  15. contextRef.current.scrollIntoView('.slideview-content')
  16. }
  17. }
  18. const handleScrollRight = () => {
  19. if (contextRef.current) {
  20. contextRef.current.scrollIntoView('.slideview-actions')
  21. }
  22. }
  23. const handleDelete = (e) => {
  24. if (onDelete) {
  25. onDelete(e)
  26. }
  27. }
  28. useEffect(() => {
  29. Taro.nextTick(() => {
  30. Taro.createSelectorQuery()
  31. .select(`.${uqClass}`)
  32. .node()
  33. .exec((res) => {
  34. contextRef.current = res[0].node;
  35. })
  36. })
  37. }, [])
  38. useEffect(() => {
  39. Taro.nextTick(() => {
  40. // 不清楚为什么, 此处获取高度一直不准确
  41. const t = setTimeout(() => {
  42. Taro.createSelectorQuery()
  43. .select(`.${uqClass}`)
  44. .boundingClientRect()
  45. .exec((res) => {
  46. if (res && res[0]) {
  47. const { height } = res[0]
  48. if (height && height !== contentHeightRef.current) {
  49. setActStyle({
  50. height: `${height}px`,
  51. })
  52. contentHeightRef.current = height
  53. }
  54. }
  55. })
  56. clearTimeout(t)
  57. }, 500)
  58. })
  59. })
  60. return (
  61. <ScrollView
  62. className={classes}
  63. upperThreshold={20}
  64. lowerThreshold={20}
  65. scrollX
  66. scrollWithAnimation
  67. enhanced
  68. onScrollToUpper={handleScrollLeft}
  69. onScrollToLower={handleScrollRight}
  70. >
  71. <View className='slideview-content'>{props.children}</View>
  72. <View className='slideview-actions' style={actStyle}>
  73. <View className='slideview-action'>
  74. {
  75. del && (
  76. <View className='slideview-action_delete' onClick={handleDelete}>
  77. <Image src={deleteIcon} />
  78. <View>删除</View>
  79. </View>
  80. )
  81. }
  82. {action}
  83. </View>
  84. </View>
  85. </ScrollView>
  86. )
  87. }