diff --git a/src/components/ColorBlock.tsx b/src/components/ColorBlock.tsx index e78b9f3..c8c7efd 100644 --- a/src/components/ColorBlock.tsx +++ b/src/components/ColorBlock.tsx @@ -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; }; @@ -20,14 +21,31 @@ const ColorBlock: React.FC = ({ style, innerClassName, innerStyle, + 'aria-label': ariaLabel, onClick, }) => { const colorBlockCls = `${prefixCls}-color-block`; + const onKeyDown: React.KeyboardEventHandler | undefined = + onClick + ? event => { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + if (!event.repeat) { + event.currentTarget.click(); + } + } + } + : undefined; + return (
{ />, ); + 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( + , + ); + 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( + {}} />, + ); + + expect(getByRole('button', { name: '#ff0000' })).toBeInTheDocument(); + }); });