forked from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseAction.ts
37 lines (30 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
35
36
37
import * as React from 'react';
import type { ActionType } from '../interface';
type ActionTypes = ActionType | ActionType[];
function toArray<T>(val?: T | T[]) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
export default function useAction(
mobile: boolean,
action: ActionTypes,
showAction?: ActionTypes,
hideAction?: ActionTypes,
): [showAction: Set<ActionType>, hideAction: Set<ActionType>] {
return React.useMemo(() => {
const mergedShowAction = toArray(showAction ?? action);
const mergedHideAction = toArray(hideAction ?? action);
const showActionSet = new Set(mergedShowAction);
const hideActionSet = new Set(mergedHideAction);
if (mobile) {
if (showActionSet.has('hover')) {
showActionSet.delete('hover');
showActionSet.add('click');
}
if (hideActionSet.has('hover')) {
hideActionSet.delete('hover');
hideActionSet.add('click');
}
}
return [showActionSet, hideActionSet];
}, [mobile, action, showAction, hideAction]);
}