优化
This commit is contained in:
@@ -1,80 +1,88 @@
|
||||
import React, { useState, useCallback, useEffect } from 'react'
|
||||
import { Picker, ConfigProvider } from '@nutui/nutui-react-taro'
|
||||
import { View } from '@tarojs/components'
|
||||
import styles from './index.module.scss'
|
||||
import React, { useState, useCallback, useEffect } from "react";
|
||||
import { Picker, ConfigProvider } from "@nutui/nutui-react-taro";
|
||||
import { View } from "@tarojs/components";
|
||||
import styles from "./index.module.scss";
|
||||
|
||||
interface PickerOption {
|
||||
text: string | number
|
||||
value: string | number
|
||||
text: string | number;
|
||||
value: string | number;
|
||||
}
|
||||
|
||||
interface PickerProps {
|
||||
visible: boolean
|
||||
options?: PickerOption[][]
|
||||
defaultValue?: (string | number)[]
|
||||
onConfirm?: (options: PickerOption[], values: (string | number)[]) => void
|
||||
onChange?: (options: PickerOption[], values: (string | number)[], columnIndex: number) => void
|
||||
visible: boolean;
|
||||
options?: PickerOption[][];
|
||||
defaultValue?: (string | number)[];
|
||||
onConfirm?: (options: PickerOption[], values: (string | number)[]) => void;
|
||||
onChange?: (
|
||||
options: PickerOption[],
|
||||
values: (string | number)[],
|
||||
columnIndex: number
|
||||
) => void;
|
||||
}
|
||||
|
||||
const CustomPicker = ({
|
||||
visible,
|
||||
options = [],
|
||||
defaultValue = [],
|
||||
onConfirm,
|
||||
onChange
|
||||
const CustomPicker = ({
|
||||
visible,
|
||||
options = [],
|
||||
defaultValue = [],
|
||||
onConfirm,
|
||||
onChange,
|
||||
}: PickerProps) => {
|
||||
// 使用内部状态管理当前选中的值
|
||||
const [currentValue, setCurrentValue] = useState<(string | number)[]>(defaultValue)
|
||||
const [currentValue, setCurrentValue] =
|
||||
useState<(string | number)[]>(defaultValue);
|
||||
|
||||
// 当外部 defaultValue 变化时,同步更新内部状态
|
||||
useEffect(() => {
|
||||
handleValuesChange(defaultValue);
|
||||
}, [defaultValue])
|
||||
}, [defaultValue]);
|
||||
|
||||
const confirmPicker = (
|
||||
options: PickerOption[],
|
||||
values: (string | number)[]
|
||||
) => {
|
||||
let description = ''
|
||||
let description = "";
|
||||
options.forEach((option: any) => {
|
||||
description += ` ${option.text}`
|
||||
})
|
||||
|
||||
description += ` ${option.text}`;
|
||||
});
|
||||
|
||||
if (onConfirm) {
|
||||
onConfirm(options, values)
|
||||
onConfirm(options, values);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const handleValuesChange = (valuesList) => {
|
||||
const isSame =
|
||||
Array.isArray(valuesList) &&
|
||||
Array.isArray(currentValue) &&
|
||||
valuesList.length === currentValue.length &&
|
||||
valuesList.every((v: any, idx: number) => v === currentValue[idx])
|
||||
valuesList.every((v: any, idx: number) => v === currentValue[idx]);
|
||||
if (!isSame) {
|
||||
setCurrentValue(valuesList)
|
||||
setCurrentValue(valuesList);
|
||||
}
|
||||
return isSame;
|
||||
}
|
||||
|
||||
const changePicker = useCallback((options: any[], values: any, columnIndex: number) => {
|
||||
// 值相同则不触发更新,避免受控/非受控同步造成的回流循环
|
||||
|
||||
const isSame = handleValuesChange(values)
|
||||
if (onChange && !isSame) {
|
||||
onChange(options, values, columnIndex)
|
||||
}
|
||||
}, [onChange, currentValue])
|
||||
};
|
||||
|
||||
const changePicker = useCallback(
|
||||
(options: any[], values: any, columnIndex: number) => {
|
||||
// 值相同则不触发更新,避免受控/非受控同步造成的回流循环
|
||||
|
||||
const isSame = handleValuesChange(values);
|
||||
if (onChange && !isSame) {
|
||||
onChange(options, values, columnIndex);
|
||||
}
|
||||
},
|
||||
[onChange, currentValue]
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<View className={styles['picker-container']}>
|
||||
<View className={styles["picker-container"]}>
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
nutuiPickerItemHeight: '48px',
|
||||
nutuiPickerItemActiveLineBorder: 'none',
|
||||
nutuiPickerItemTextColor: '#000',
|
||||
nutuiPickerItemFontSize: '20px',
|
||||
nutuiPickerItemHeight: "48px",
|
||||
nutuiPickerItemActiveLineBorder: "none",
|
||||
nutuiPickerItemTextColor: "#000",
|
||||
nutuiPickerItemFontSize: "20px",
|
||||
}}
|
||||
>
|
||||
<Picker
|
||||
@@ -92,7 +100,7 @@ const CustomPicker = ({
|
||||
</ConfigProvider>
|
||||
</View>
|
||||
</>
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default CustomPicker
|
||||
export default CustomPicker;
|
||||
|
||||
@@ -152,25 +152,6 @@ const PopupPicker = ({
|
||||
<View className={styles.evaluateCardWrap}>
|
||||
<NTRPTestEntryCard type={EvaluateScene.userEdit} />
|
||||
</View>
|
||||
// <View className={`${styles["examination-btn"]}}`}>
|
||||
// <View className={`${styles["text-container"]}}`}>
|
||||
// <View className={`${styles["text-title"]}}`}>
|
||||
// 不知道自己的<Text>(NTRP)</Text>水平
|
||||
// </View>
|
||||
// <View className={`${styles["text-btn"]}}`}>
|
||||
// <Text>快速测试</Text>
|
||||
// <Image src={imgs.ICON_ARROW_GREEN} className={`${styles["icon-arrow"]}`}></Image>
|
||||
// </View>
|
||||
// </View>
|
||||
// <View className={`${styles["img-container"]}}`}>
|
||||
// <View className={`${styles["img-box"]}`}>
|
||||
// <Image src={img!}></Image>
|
||||
// </View>
|
||||
// <View className={`${styles["img-box"]}`}>
|
||||
// <Image src={imgs.ICON_EXAMINATION}></Image>
|
||||
// </View>
|
||||
// </View>
|
||||
// </View>
|
||||
)}
|
||||
<Picker
|
||||
visible={visible}
|
||||
|
||||
Reference in New Issue
Block a user