ant-design/components/_util/transButton.tsx
希亚的西红柿 9acde83991
feat: add tabindex option for paragraph button (#48567)
* feat: add tabIndex option for paragraph

* test: update snapshot

* test: update test case

* revert: revert the testcase

* test: update test case

* fix: fix the coverage test

* chore: code optimize

* Update index.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update CopyBtn.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update transButton.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update copy.test.tsx

Signed-off-by: lijianan <574980606@qq.com>

* Update CopyBtn.tsx

Signed-off-by: lijianan <574980606@qq.com>

---------

Signed-off-by: lijianan <574980606@qq.com>
Co-authored-by: lijianan <574980606@qq.com>
2024-04-24 18:06:38 +08:00

74 lines
1.6 KiB
TypeScript

/**
* 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';
interface TransButtonProps extends React.HTMLAttributes<HTMLDivElement> {
onClick?: (e?: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
noStyle?: boolean;
autoFocus?: boolean;
disabled?: boolean;
tabIndex?: number;
}
const inlineStyle: React.CSSProperties = {
border: 0,
background: 'transparent',
padding: 0,
lineHeight: 'inherit',
display: 'inline-block',
};
const TransButton = React.forwardRef<HTMLDivElement, TransButtonProps>((props, ref) => {
const onKeyDown: React.KeyboardEventHandler<HTMLDivElement> = (event) => {
const { keyCode } = event;
if (keyCode === KeyCode.ENTER) {
event.preventDefault();
}
};
const onKeyUp: React.KeyboardEventHandler<HTMLDivElement> = (event) => {
const { keyCode } = event;
const { onClick } = props;
if (keyCode === KeyCode.ENTER && onClick) {
onClick();
}
};
const { style, noStyle, disabled, tabIndex = 0, ...restProps } = props;
let mergedStyle: React.CSSProperties = {};
if (!noStyle) {
mergedStyle = {
...inlineStyle,
};
}
if (disabled) {
mergedStyle.pointerEvents = 'none';
}
mergedStyle = {
...mergedStyle,
...style,
};
return (
<div
role="button"
tabIndex={tabIndex}
ref={ref}
{...restProps}
onKeyDown={onKeyDown}
onKeyUp={onKeyUp}
style={mergedStyle}
/>
);
});
export default TransButton;