-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
CustomKeyboard.tsx
167 lines (156 loc) · 4.32 KB
/
CustomKeyboard.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import classnames from 'classnames';
import React from 'react';
import TouchFeedback from 'rmc-feedback';
import { Omit } from '../_util/types';
export type HTMLTableDataProps = Omit<
React.HTMLProps<HTMLTableDataCellElement>,
'onClick'
>;
export interface KeyboardItemProps extends HTMLTableDataProps {
prefixCls?: string;
tdRef?: React.Ref<HTMLTableDataCellElement>;
onClick: (
event: React.MouseEvent<HTMLTableDataCellElement>,
value: string,
) => void;
}
export class KeyboardItem extends React.Component<KeyboardItemProps, any> {
static defaultProps = {
prefixCls: 'am-number-keyboard',
onClick: () => {},
disabled: false,
};
render() {
const {
prefixCls,
onClick,
className,
disabled,
children,
tdRef,
...restProps,
} = this.props;
let value = children;
if (className === 'keyboard-delete') {
value = 'delete';
} else if (className === 'keyboard-hide') {
value = 'hide';
} else if (className === 'keyboard-confirm') {
value = 'confirm';
}
const wrapCls = classnames(`${prefixCls}-item`, className);
return (
<TouchFeedback activeClassName={`${prefixCls}-item-active`}>
<td
ref={tdRef}
// tslint:disable-next-line:jsx-no-multiline-js
onClick={e => {
onClick(e, value as string);
}}
className={wrapCls}
{...restProps}
>
{children}
</td>
</TouchFeedback>
);
}
}
class CustomKeyboard extends React.Component<any, any> {
static defaultProps = {
prefixCls: 'am-number-keyboard',
};
linkedInput: any;
antmKeyboard: HTMLDivElement | null;
confirmDisabled: boolean;
confirmKeyboardItem: HTMLTableDataCellElement | null;
onKeyboardClick = (
e: React.MouseEvent<HTMLTableDataCellElement>,
value: string,
) => {
e.nativeEvent.stopImmediatePropagation();
if (value === 'confirm' && this.confirmDisabled) {
return null;
} else {
if (this.linkedInput) {
this.linkedInput.onKeyboardClick(value);
}
}
}
renderKeyboardItem = (item: string, index: number) => {
return (
<KeyboardItem
onClick={this.onKeyboardClick}
key={`item-${item}-${index}`}
>
{item}
</KeyboardItem>
);
}
render() {
const {
prefixCls,
confirmLabel,
backspaceLabel,
cancelKeyboardLabel,
} = this.props;
const wrapperCls = classnames(
`${prefixCls}-wrapper`,
`${prefixCls}-wrapper-hide`,
);
return (
<div className={wrapperCls} ref={el => (this.antmKeyboard = el)}>
<table>
<tbody>
<tr>
{['1', '2', '3'].map((item, index) =>
// tslint:disable-next-line:jsx-no-multiline-js
this.renderKeyboardItem(item, index),
)}
<KeyboardItem
role="button"
aria-label={backspaceLabel}
className="keyboard-delete"
rowSpan={2}
onClick={this.onKeyboardClick}
/>
</tr>
<tr>
{['4', '5', '6'].map((item, index) =>
// tslint:disable-next-line:jsx-no-multiline-js
this.renderKeyboardItem(item, index),
)}
</tr>
<tr>
{['7', '8', '9'].map((item, index) =>
// tslint:disable-next-line:jsx-no-multiline-js
this.renderKeyboardItem(item, index),
)}
<KeyboardItem
className="keyboard-confirm"
rowSpan={2}
onClick={this.onKeyboardClick}
tdRef={el => (this.confirmKeyboardItem = el)}
>
{confirmLabel}
</KeyboardItem>
</tr>
<tr>
{['.', '0'].map((item, index) =>
// tslint:disable-next-line:jsx-no-multiline-js
this.renderKeyboardItem(item, index),
)}
<KeyboardItem
role="button"
aria-label={cancelKeyboardLabel}
className="keyboard-hide"
onClick={this.onKeyboardClick}
/>
</tr>
</tbody>
</table>
</div>
);
}
}
export default CustomKeyboard;