-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathuseActive.ts
34 lines (28 loc) · 904 Bytes
/
useActive.ts
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
import * as React from 'react';
import CascaderContext from '../context';
/**
* Control the active open options path.
*/
const useActive = (
multiple?: boolean,
open?: boolean,
defaultActiveKey?: React.Key[],
): [React.Key[], (activeValueCells: React.Key[]) => void] => {
const { values } = React.useContext(CascaderContext);
const firstValueCells = values[0];
// Record current dropdown active options
// This also control the open status
const [activeValueCells, setActiveValueCells] = React.useState<React.Key[]>(defaultActiveKey ?? []);
React.useEffect(
() => {
if (!multiple) {
setActiveValueCells(firstValueCells || []);
}
},
/* eslint-disable react-hooks/exhaustive-deps */
[open, firstValueCells],
/* eslint-enable react-hooks/exhaustive-deps */
);
return [activeValueCells, setActiveValueCells];
};
export default useActive;