-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathClearButton.tsx
69 lines (58 loc) · 1.64 KB
/
ClearButton.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
'use client';
import type * as React from 'react';
import {Xmark} from '@gravity-ui/icons';
import {Button} from '../../../Button';
import type {ButtonSize} from '../../../Button';
import {Icon} from '../../../Icon';
import {block} from '../../../utils/cn';
import type {InputControlSize} from '../../types';
import i18n from './i18n';
import './ClearButton.scss';
const b = block('clear-button');
const ICON_SIZE = 16;
type Props = {
size: ButtonSize;
className?: string;
onClick: (event: React.MouseEvent<HTMLSpanElement>) => void;
};
export const mapTextInputSizeToButtonSize = (textInputSize: InputControlSize): ButtonSize => {
switch (textInputSize) {
case 's': {
return 'xs';
}
case 'm': {
return 's';
}
case 'l': {
return 'm';
}
case 'xl': {
return 'l';
}
default: {
throw new Error(`Unknown text input size "${textInputSize}"`);
}
}
};
export const ClearButton = (props: Props) => {
const {size, className, onClick} = props;
/**
* Turn off event onBlur on input when use clear button
*/
const preventDefaultHandler: React.MouseEventHandler<HTMLButtonElement> = (event) => {
event.preventDefault();
};
return (
<Button
size={size}
className={b(null, className)}
onClick={onClick}
extraProps={{
onMouseDown: preventDefaultHandler,
'aria-label': i18n('label_clear-button'),
}}
>
<Icon data={Xmark} size={ICON_SIZE} />
</Button>
);
};