Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/ColorBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ColorBlockProps = {
innerClassName?: string;
/** Internal usage. Only used in antd ColorPicker semantic structure only */
innerStyle?: React.CSSProperties;
'aria-label'?: string;
onClick?: React.MouseEventHandler<HTMLDivElement>;
};

Expand All @@ -20,14 +21,31 @@ const ColorBlock: React.FC<ColorBlockProps> = ({
style,
innerClassName,
innerStyle,
'aria-label': ariaLabel,
onClick,
}) => {
const colorBlockCls = `${prefixCls}-color-block`;
const onKeyDown: React.KeyboardEventHandler<HTMLDivElement> | undefined =
onClick
? event => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
if (!event.repeat) {
event.currentTarget.click();
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
: undefined;

return (
<div
aria-label={onClick ? (ariaLabel ?? color) : undefined}
className={clsx(colorBlockCls, className)}
style={style}
onClick={onClick}
onKeyDown={onKeyDown}
role={onClick ? 'button' : undefined}
tabIndex={onClick ? 0 : undefined}
>
<div
className={clsx(`${colorBlockCls}-inner`, innerClassName)}
Expand Down
31 changes: 31 additions & 0 deletions tests/components.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,39 @@ describe('ColorPicker.Components', () => {
/>,
);

const colorBlock = container.querySelector('.test-color-block');
const innerDiv = container.querySelector('.test-color-block-inner');
expect(colorBlock).not.toHaveAttribute('role');
expect(colorBlock).not.toHaveAttribute('tabindex');
expect(colorBlock).not.toHaveAttribute('aria-label');
expect(innerDiv).toHaveClass('my-inner-class');
expect(innerDiv).toHaveStyle({ color: '#903' });
});

it('makes clickable ColorBlock keyboard accessible', () => {
const onClick = vi.fn();
const { getByRole } = render(
<ColorBlock
prefixCls="test"
color="#ff0000"
aria-label="Brand red"
onClick={onClick}
/>,
);
const button = getByRole('button', { name: 'Brand red' });

fireEvent.keyDown(button, { key: 'Enter' });
fireEvent.keyDown(button, { key: ' ' });
fireEvent.keyDown(button, { key: 'Enter', repeat: true });

expect(onClick).toHaveBeenCalledTimes(2);
});

it('uses the color value as the default name for clickable ColorBlock', () => {
const { getByRole } = render(
<ColorBlock prefixCls="test" color="#ff0000" onClick={() => {}} />,
);

expect(getByRole('button', { name: '#ff0000' })).toBeInTheDocument();
});
});
Loading