-
Notifications
You must be signed in to change notification settings - Fork 0
/
Picker.tsx
121 lines (109 loc) · 3.44 KB
/
Picker.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import React, { useCallback, useEffect, useRef, useState } from 'react';
import Pic, { Option, Wheels } from '@eightfeet/picker';
import classNames from 'classnames';
import s from './Picker.module.less';
interface Props {
id?: Option["id"];
parentId?: Option["parentId"];
title?: Option["title"];
wheels: Wheels;
keyMap?: Option["keyMap"];
defaultValue?: Option["defaultValue"];
style?: Option["style"];
cancelBtnText?: string;
confirmBtnText?: string;
onConfirm?: Option["onConfirm"];
onCancel?: Option["onCancel"];
transitionEnd?: Option["transitionEnd"];
onShow?: Option["onShow"];
onHide?: Option["onHide"];
onChange?: Option["onChange"];
children?: React.ReactNode;
className?: string;
delimiter?: string;
}
const Picker:React.FC<Props> = ({
children,
onConfirm,
className,
delimiter,
defaultValue,
keyMap,
...other
}) => {
const triggerRef = useRef<HTMLDivElement>(null);
const triggerId = useRef(`picktrigger${new Date().getTime()}${window.Math.floor(
window.Math.random() * 100
)}`)
// 创建初始化
const PicRef = useRef<Pic>();
const [selected, setSelected] = useState<any[]>();
const handleOnConfirm = useCallback(
(data) => {
console.log(data);
setSelected(data);
if (onConfirm instanceof Function) {
onConfirm(data);
}
},
[onConfirm],
)
const getDefaultvalue = useCallback(
() => {
let value: any[] = [];
selected?.forEach((item) => {
if (typeof item === 'string') {
value.push(item);
} else {
let key = 'value';
if (keyMap) key = keyMap[key];
value.push(item[key])
}
});
return value;
},
[keyMap, selected],
)
useEffect(() => {
if(triggerRef.current && other.wheels.length) {
PicRef.current?.destroy();
setTimeout(() => {
const { id } = (PicRef.current as any) || {}
const Node = document.getElementById(id);
if(Node) document.body.removeChild(Node);
PicRef.current = new Pic({
keyMap,
...other,
onConfirm: handleOnConfirm,
trigger: `#${triggerId.current}`,
defaultValue: getDefaultvalue() || defaultValue || [],
});
}, 20);
}
}, [defaultValue, getDefaultvalue, handleOnConfirm, keyMap, other, selected])
useEffect(() => {
return () => {
PicRef.current?.destroy();
}
}, []);
const renderSelected = useCallback(
() => {
let str: string[] = [];
selected?.forEach((item) => {
if (typeof item === 'string') {
str.push(item);
} else {
let key = 'display';
if (keyMap) key = keyMap[key];
str.push(item[key])
}
});
return str.join(delimiter || '')
},
[delimiter, keyMap, selected],
)
return <div ref={triggerRef} className={classNames(s.wrap, className)} id={triggerId.current}>
{selected?.length ? renderSelected() : children}
</div>;
}
export default Picker