|
| 1 | +import React from 'react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import { fireEvent, render } from '../../../tests/utils'; |
| 5 | +import ConfigProvider from '../../config-provider'; |
| 6 | +import Input from '../../input'; |
| 7 | +import InputNumber from '../../input-number'; |
| 8 | +import zhCN from '../../locale/zh_CN'; |
| 9 | +import Mentions from '../../mentions'; |
| 10 | +import type { AllowClear } from '../hooks'; |
| 11 | + |
| 12 | +describe.each([ |
| 13 | + { name: 'Input', Component: Input, configKey: 'input' }, |
| 14 | + { name: 'TextArea', Component: Input.TextArea, configKey: 'textArea' }, |
| 15 | + { name: 'Mentions', Component: Mentions, configKey: 'mentions' }, |
| 16 | +] as const)('$name allowClear.tabIndex', ({ Component, configKey }) => { |
| 17 | + it('keeps the clear button keyboard accessible by default', async () => { |
| 18 | + const user = userEvent.setup(); |
| 19 | + const { getByRole } = render( |
| 20 | + <Component defaultValue="value" allowClear={{ clearIcon: 'Clear' }} />, |
| 21 | + ); |
| 22 | + |
| 23 | + await user.tab(); |
| 24 | + expect(getByRole('textbox')).toHaveFocus(); |
| 25 | + await user.tab(); |
| 26 | + expect(getByRole('button', { name: 'Clear' })).toHaveFocus(); |
| 27 | + await user.keyboard('{Enter}'); |
| 28 | + expect(getByRole('textbox')).toHaveValue(''); |
| 29 | + }); |
| 30 | + |
| 31 | + it('skips the clear button with tabIndex -1 and still allows clicking to clear', async () => { |
| 32 | + const user = userEvent.setup(); |
| 33 | + const onClear = jest.fn(); |
| 34 | + const { getByRole } = render( |
| 35 | + <> |
| 36 | + <Component |
| 37 | + aria-label="Value" |
| 38 | + defaultValue="value" |
| 39 | + allowClear={{ clearIcon: 'Clear', tabIndex: -1 }} |
| 40 | + onClear={onClear} |
| 41 | + /> |
| 42 | + <input aria-label="Next" /> |
| 43 | + </>, |
| 44 | + ); |
| 45 | + |
| 46 | + await user.tab(); |
| 47 | + expect(getByRole('textbox', { name: 'Value' })).toHaveFocus(); |
| 48 | + await user.tab(); |
| 49 | + expect(getByRole('textbox', { name: 'Next' })).toHaveFocus(); |
| 50 | + await user.tab({ shift: true }); |
| 51 | + expect(getByRole('textbox', { name: 'Value' })).toHaveFocus(); |
| 52 | + await user.click(getByRole('button', { name: 'Clear' })); |
| 53 | + expect(getByRole('textbox', { name: 'Value' })).toHaveValue(''); |
| 54 | + expect(getByRole('textbox', { name: 'Value' })).toHaveFocus(); |
| 55 | + expect(onClear).toHaveBeenCalledTimes(1); |
| 56 | + }); |
| 57 | + |
| 58 | + it('inherits the global tabIndex and lets the component override it with 0', () => { |
| 59 | + const Demo = ({ allowClear }: { allowClear?: AllowClear }) => ( |
| 60 | + <ConfigProvider {...{ [configKey]: { allowClear: { clearIcon: 'Clear', tabIndex: -1 } } }}> |
| 61 | + <Component defaultValue="value" allowClear={allowClear} /> |
| 62 | + </ConfigProvider> |
| 63 | + ); |
| 64 | + const { getByRole, queryByRole, rerender } = render(<Demo />); |
| 65 | + expect(getByRole('button', { name: 'Clear' })).toHaveAttribute('tabindex', '-1'); |
| 66 | + |
| 67 | + rerender(<Demo allowClear />); |
| 68 | + expect(getByRole('button', { name: 'Clear' })).toHaveAttribute('tabindex', '-1'); |
| 69 | + |
| 70 | + rerender(<Demo allowClear={{ tabIndex: 0 }} />); |
| 71 | + expect(getByRole('button', { name: 'Clear' })).toHaveAttribute('tabindex', '0'); |
| 72 | + |
| 73 | + rerender(<Demo allowClear={false} />); |
| 74 | + expect(queryByRole('button', { name: 'Clear' })).not.toBeInTheDocument(); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +type ClearProps = { allowClear?: AllowClear; onClear?: () => void }; |
| 79 | +const clearCases = [ |
| 80 | + { |
| 81 | + name: 'Input', |
| 82 | + configKey: 'input', |
| 83 | + render: (props: ClearProps) => <Input defaultValue="value" {...props} />, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: 'TextArea', |
| 87 | + configKey: 'textArea', |
| 88 | + render: (props: ClearProps) => <Input.TextArea defaultValue="value" {...props} />, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: 'Search', |
| 92 | + configKey: 'input', |
| 93 | + render: (props: ClearProps) => <Input.Search defaultValue="value" {...props} />, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: 'Password', |
| 97 | + configKey: 'input', |
| 98 | + render: (props: ClearProps) => <Input.Password defaultValue="value" {...props} />, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: 'Mentions', |
| 102 | + configKey: 'mentions', |
| 103 | + render: (props: ClearProps) => <Mentions defaultValue="value" {...props} />, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: 'InputNumber', |
| 107 | + configKey: 'inputNumber', |
| 108 | + render: (props: ClearProps) => <InputNumber defaultValue={0} {...props} />, |
| 109 | + }, |
| 110 | +] as const; |
| 111 | + |
| 112 | +describe.each(clearCases)('$name unified allowClear', ({ configKey, render: renderComponent }) => { |
| 113 | + it('removes the clear entry when disabled and restores its configuration when enabled', () => { |
| 114 | + const onClear = jest.fn(); |
| 115 | + const config = { |
| 116 | + clearIcon: <span data-testid="clear-icon">Custom</span>, |
| 117 | + label: 'Remove value', |
| 118 | + tabIndex: -1, |
| 119 | + }; |
| 120 | + const { getByRole, queryByRole, queryByTestId, rerender } = render( |
| 121 | + renderComponent({ allowClear: config, onClear }), |
| 122 | + ); |
| 123 | + expect(getByRole('button', { name: 'Remove value' })).toHaveAttribute('tabindex', '-1'); |
| 124 | + rerender(renderComponent({ allowClear: { ...config, disabled: true }, onClear })); |
| 125 | + expect(queryByRole('button', { name: 'Remove value', hidden: true })).not.toBeInTheDocument(); |
| 126 | + expect(queryByTestId('clear-icon')).not.toBeInTheDocument(); |
| 127 | + expect(onClear).not.toHaveBeenCalled(); |
| 128 | + rerender(renderComponent({ allowClear: { ...config, disabled: false }, onClear })); |
| 129 | + expect(getByRole('button', { name: 'Remove value' })).toHaveAttribute('tabindex', '-1'); |
| 130 | + expect(queryByTestId('clear-icon')).toBeInTheDocument(); |
| 131 | + fireEvent.click(getByRole('button', { name: 'Remove value' })); |
| 132 | + expect(onClear).toHaveBeenCalledTimes(1); |
| 133 | + }); |
| 134 | + |
| 135 | + it('updates the localized label and preserves an explicit label', () => { |
| 136 | + const Demo = ({ |
| 137 | + chinese, |
| 138 | + allowClear = true, |
| 139 | + }: { |
| 140 | + chinese?: boolean; |
| 141 | + allowClear?: AllowClear; |
| 142 | + }) => ( |
| 143 | + <ConfigProvider locale={chinese ? zhCN : undefined}> |
| 144 | + {renderComponent({ allowClear })} |
| 145 | + </ConfigProvider> |
| 146 | + ); |
| 147 | + const { getByRole, rerender } = render(<Demo />); |
| 148 | + expect(getByRole('button', { name: 'Clear' })).toBeInTheDocument(); |
| 149 | + rerender(<Demo chinese />); |
| 150 | + expect(getByRole('button', { name: '清除' })).toBeInTheDocument(); |
| 151 | + rerender(<Demo chinese allowClear={{ label: 'Remove value' }} />); |
| 152 | + expect(getByRole('button', { name: 'Remove value' })).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + if (configKey) { |
| 156 | + it('merges fields before normalization and preserves explicit false and zero overrides', () => { |
| 157 | + const contextAllowClear = { |
| 158 | + clearIcon: <span data-testid="global-clear">Custom</span>, |
| 159 | + disabled: true, |
| 160 | + tabIndex: -1, |
| 161 | + label: 'Global clear', |
| 162 | + }; |
| 163 | + const Demo = ({ allowClear }: ClearProps) => ( |
| 164 | + <ConfigProvider {...{ [configKey]: { allowClear: contextAllowClear } }}> |
| 165 | + {renderComponent({ allowClear })} |
| 166 | + </ConfigProvider> |
| 167 | + ); |
| 168 | + const { getByRole, queryByTestId, rerender } = render(<Demo />); |
| 169 | + expect(queryByTestId('global-clear')).not.toBeInTheDocument(); |
| 170 | + rerender(<Demo allowClear />); |
| 171 | + expect(queryByTestId('global-clear')).not.toBeInTheDocument(); |
| 172 | + rerender(<Demo allowClear={{ tabIndex: 0 }} />); |
| 173 | + expect(queryByTestId('global-clear')).not.toBeInTheDocument(); |
| 174 | + rerender(<Demo allowClear={{ disabled: false }} />); |
| 175 | + expect(getByRole('button', { name: 'Global clear' })).toHaveAttribute('tabindex', '-1'); |
| 176 | + expect(queryByTestId('global-clear')).toBeInTheDocument(); |
| 177 | + rerender(<Demo allowClear={{ disabled: false, tabIndex: 0, label: 'Local clear' }} />); |
| 178 | + expect(getByRole('button', { name: 'Local clear' })).toHaveAttribute('tabindex', '0'); |
| 179 | + expect(queryByTestId('global-clear')).toBeInTheDocument(); |
| 180 | + rerender(<Demo allowClear={false} />); |
| 181 | + expect(queryByTestId('global-clear')).not.toBeInTheDocument(); |
| 182 | + }); |
| 183 | + } |
| 184 | +}); |
0 commit comments