2019-02-19 11:42:05 +08:00
|
|
|
/**
|
|
|
|
* Wrap of sub component which need use as Button capacity (like Icon component).
|
|
|
|
* This helps accessibility reader to tread as a interactive button to operation.
|
|
|
|
*/
|
|
|
|
import * as React from 'react';
|
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
|
|
|
|
2019-04-19 17:52:27 +08:00
|
|
|
interface TransButtonProps extends React.HTMLAttributes<HTMLDivElement> {
|
2019-04-25 12:18:01 +08:00
|
|
|
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
|
2019-07-22 19:28:23 +08:00
|
|
|
noStyle?: boolean;
|
2020-04-07 15:41:05 +08:00
|
|
|
autoFocus?: boolean;
|
2020-05-13 19:15:40 +08:00
|
|
|
disabled?: boolean;
|
2019-02-19 11:42:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const inlineStyle: React.CSSProperties = {
|
|
|
|
border: 0,
|
|
|
|
background: 'transparent',
|
|
|
|
padding: 0,
|
|
|
|
lineHeight: 'inherit',
|
2019-04-19 17:52:27 +08:00
|
|
|
display: 'inline-block',
|
2019-02-19 11:42:05 +08:00
|
|
|
};
|
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
const TransButton = React.forwardRef<HTMLDivElement, TransButtonProps>((props, ref) => {
|
|
|
|
const onKeyDown: React.KeyboardEventHandler<HTMLDivElement> = event => {
|
2019-02-19 11:42:05 +08:00
|
|
|
const { keyCode } = event;
|
|
|
|
if (keyCode === KeyCode.ENTER) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
const onKeyUp: React.KeyboardEventHandler<HTMLDivElement> = event => {
|
2019-02-19 11:42:05 +08:00
|
|
|
const { keyCode } = event;
|
2020-11-18 11:20:31 +08:00
|
|
|
const { onClick } = props;
|
2019-02-19 11:42:05 +08:00
|
|
|
if (keyCode === KeyCode.ENTER && onClick) {
|
|
|
|
onClick();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
const { style, noStyle, disabled, ...restProps } = props;
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
let mergedStyle: React.CSSProperties = {};
|
2020-05-13 19:15:40 +08:00
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
if (!noStyle) {
|
2020-05-13 19:15:40 +08:00
|
|
|
mergedStyle = {
|
2020-11-18 11:20:31 +08:00
|
|
|
...inlineStyle,
|
2020-05-13 19:15:40 +08:00
|
|
|
};
|
2020-11-18 11:20:31 +08:00
|
|
|
}
|
2019-07-22 19:28:23 +08:00
|
|
|
|
2020-11-18 11:20:31 +08:00
|
|
|
if (disabled) {
|
|
|
|
mergedStyle.pointerEvents = 'none';
|
2019-02-19 11:42:05 +08:00
|
|
|
}
|
2020-11-18 11:20:31 +08:00
|
|
|
|
|
|
|
mergedStyle = {
|
|
|
|
...mergedStyle,
|
|
|
|
...style,
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
|
|
|
ref={ref}
|
|
|
|
{...restProps}
|
|
|
|
onKeyDown={onKeyDown}
|
|
|
|
onKeyUp={onKeyUp}
|
|
|
|
style={mergedStyle}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
2019-02-19 11:42:05 +08:00
|
|
|
|
|
|
|
export default TransButton;
|