-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathuseAction.ts
34 lines (26 loc) · 1.02 KB
/
useAction.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 type { ActionType } from '../interface';
type InternalActionType = ActionType | 'touch';
type ActionTypes = InternalActionType | InternalActionType[];
function toArray<T>(val?: T | T[]) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
export default function useAction(
action: ActionTypes,
showAction?: ActionTypes,
hideAction?: ActionTypes,
): [showAction: Set<InternalActionType>, hideAction: Set<InternalActionType>] {
return React.useMemo(() => {
const mergedShowAction = toArray(showAction ?? action);
const mergedHideAction = toArray(hideAction ?? action);
const showActionSet = new Set(mergedShowAction);
const hideActionSet = new Set(mergedHideAction);
if (showActionSet.has('hover') && !showActionSet.has('click')) {
showActionSet.add('touch');
}
if (hideActionSet.has('hover') && !hideActionSet.has('click')) {
hideActionSet.add('touch');
}
return [showActionSet, hideActionSet];
}, [action, showAction, hideAction]);
}