-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathSoftKey.jsx
More file actions
117 lines (104 loc) · 2.51 KB
/
Copy pathSoftKey.jsx
File metadata and controls
117 lines (104 loc) · 2.51 KB
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
import React, { useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import './SoftKey.scss';
const prefixCls = 'kai-softkey';
const Button = props => {
const {
handleClick
} = props;
const handleButtonClick = e => {
e.preventDefault();
handleClick();
};
// We want to avoid losing focus on the parent element
const handleCheckFocus = e => {
e.preventDefault();
if (e.relatedTarget) {
// Revert focus back to previous blurring element
e.relatedTarget.focus();
} else {
// No previous focus target, blur instead
e.currentTarget.blur();
}
};
return (
<button
className={`${prefixCls}-btn`}
onClick={handleButtonClick}
onFocus={handleCheckFocus}
>
{props.icon ? <i class={props.icon} /> : null}
{props.text}
</button>
);
};
const SoftKey = React.memo(
props => {
const {
leftCallback,
rightCallback,
centerCallback,
leftText,
rightText,
centerText,
centerIcon,
} = props;
const handleKeyDown = useCallback(
e => {
switch (e.key) {
case 'SoftLeft':
leftCallback();
break;
case 'SoftRight':
rightCallback();
break;
case 'Enter':
// Action case press center key
centerCallback();
break;
default:
break;
}
},
[leftCallback, rightCallback, centerCallback]
);
useEffect(
() => {
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
},
[handleKeyDown]
);
return (
<div className={`${prefixCls} visible`}>
<Button pos="left" text={leftText} handleClick={leftCallback} />
<Button
pos="center"
text={centerText}
icon={centerIcon}
handleClick={centerCallback}
/>
<Button pos="right" text={rightText} handleClick={rightCallback} />
</div>
);
}
);
SoftKey.propTypes = {
leftText: PropTypes.string,
centerText: PropTypes.string,
rightText: PropTypes.string,
centerIcon: PropTypes.string,
leftCallback: PropTypes.func,
centerCallback: PropTypes.func,
rightCallback: PropTypes.func,
};
SoftKey.defaultProps = {
leftText: '',
centerText: '',
rightText: '',
centerIcon: null,
leftCallback: () => {},
centerCallback: () => {},
rightCallback: () => {},
};
export default SoftKey;