PickerViews.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { PickerView, PickerViewColumn, View, Text } from "@tarojs/components";
  2. import './PickerViews.scss'
  3. import { forwardRef, useEffect, useImperativeHandle, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import { rpxToPx } from "@/utils/tools";
  6. // export default function Component(props: { value: any, onChange: Function, items: any, height?: number, showBtns?: boolean, onCancel?: Function, }) {
  7. let detailValue
  8. const Component = forwardRef((props: {
  9. value: any, onChange: Function, items: any, height?: number, showBtns?: boolean, onCancel?: Function,
  10. themeColor?: string, title?: string,
  11. hideTitle?: boolean
  12. }, ref) => {
  13. const { t } = useTranslation()
  14. const [v, setV] = useState(props.value)
  15. var color = props.themeColor ? props.themeColor : '#ff0000'
  16. var alpha = alphaToHex(0.4)
  17. useEffect(() => {
  18. setV(props.value)
  19. }, [props.value])
  20. function onPickerChange(e) {
  21. setV(e.detail.value)
  22. // detailValue = e.detail.value
  23. if (props.showBtns) {
  24. return;
  25. }
  26. props.onChange(e.detail.value)
  27. }
  28. function alphaToHex(alpha) {
  29. var alphaValue = Math.round(alpha * 255); // 将透明度乘以255并四舍五入
  30. var hexValue = alphaValue.toString(16); // 将整数转换为十六进制字符串
  31. if (hexValue.length === 1) {
  32. hexValue = "0" + hexValue; // 如果十六进制字符串只有一位,补零
  33. }
  34. return hexValue;
  35. }
  36. function confirm(e) {
  37. if (process.env.TARO_ENV == 'weapp') {
  38. e.stopPropagation()
  39. }
  40. props.onChange(v)
  41. }
  42. useImperativeHandle(ref, () => ({
  43. getConfirmData: confirm
  44. }));
  45. return <View className='modal_content' style={{paddingBottom:props.showBtns?rpxToPx(120):0}} catchMove>
  46. {
  47. !props.hideTitle && <Text className='modal_title' style={{ color: color }}>{props.title ? props.title : '测试标题 '}</Text>
  48. }
  49. <PickerView value={v}
  50. // color='#fff'
  51. itemStyle={{ color: '#fff' }}
  52. style={{ color: '#fff' }}
  53. onChange={onPickerChange}
  54. indicatorStyle='height: 40px;'
  55. immediateChange={true}
  56. className="picker"
  57. maskClass="picker-mask"
  58. >
  59. {
  60. props.items.map((item, index) => {
  61. return <PickerViewColumn key={index}>
  62. {item.map((obj, j) => {
  63. return (
  64. <Text key={j} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#fff' }}>{obj}</Text>
  65. );
  66. })}
  67. </PickerViewColumn>
  68. })
  69. }
  70. </PickerView >
  71. {
  72. props.showBtns && <View className='modal_operate'>
  73. <View className='modal_btn' style={{ backgroundColor: color + alpha }} onClick={(e) => {
  74. if (process.env.TARO_ENV == 'weapp') {
  75. e.stopPropagation()
  76. }; props.onCancel!()
  77. }}>
  78. <Text className='modal_cancel_text' style={{ color: color, fontWeight: 'bold' }}>{t('feature.common.picker_cancel_btn')}</Text>
  79. </View>
  80. <View className='btn_space' />
  81. <View className='modal_btn' style={{ backgroundColor: color }} onClick={confirm}>
  82. <Text className='modal_confirm_text' style={{ color: '#000', fontWeight: 'bold' }}>{t('feature.common.picker_confirm_btn')}</Text>
  83. </View>
  84. </View>
  85. }
  86. </View>
  87. })
  88. export default Component;