-
Notifications
You must be signed in to change notification settings - Fork 14.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(input): Migrate Input component to Ant Design 5 #30730
Changes from all commits
fbe7a9c
84144ae
9612d07
898d829
88d9159
9613ca6
d8f6b45
5e9045f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,6 @@ import { | |
import rison from 'rison'; | ||
import { debounce } from 'lodash'; | ||
import { FixedSizeList as List } from 'react-window'; | ||
import { AntdInput } from 'src/components'; | ||
import Icons from 'src/components/Icons'; | ||
import { Input } from 'src/components/Input'; | ||
import { useToasts } from 'src/components/MessageToasts/withToasts'; | ||
|
@@ -55,6 +54,7 @@ import { | |
supersetGetCache, | ||
} from 'src/utils/cachedSupersetGet'; | ||
import { useVerboseMap } from 'src/hooks/apiResources/datasets'; | ||
import { InputRef } from 'antd-v5'; | ||
import { MenuItemTooltip } from '../DisabledMenuItemTooltip'; | ||
import DrillByModal from './DrillByModal'; | ||
import { getSubmenuYOffset } from '../utils'; | ||
|
@@ -114,11 +114,12 @@ export const DrillByMenuItems = ({ | |
const { addDangerToast } = useToasts(); | ||
const [isLoadingColumns, setIsLoadingColumns] = useState(true); | ||
const [searchInput, setSearchInput] = useState(''); | ||
const [debouncedSearchInput, setDebouncedSearchInput] = useState(''); | ||
const [dataset, setDataset] = useState<Dataset>(); | ||
const [columns, setColumns] = useState<Column[]>([]); | ||
const [showModal, setShowModal] = useState(false); | ||
const [currentColumn, setCurrentColumn] = useState(); | ||
const ref = useRef<AntdInput>(null); | ||
const ref = useRef<InputRef>(null); | ||
const showSearch = | ||
loadDrillByOptions || columns.length > SHOW_COLUMNS_SEARCH_THRESHOLD; | ||
const handleSelection = useCallback( | ||
|
@@ -138,11 +139,11 @@ export const DrillByMenuItems = ({ | |
|
||
useEffect(() => { | ||
if (open) { | ||
ref.current?.input.focus({ preventScroll: true }); | ||
ref.current?.input?.focus({ preventScroll: true }); | ||
} else { | ||
// Reset search input when menu is closed | ||
ref.current?.setValue(''); | ||
setSearchInput(''); | ||
setDebouncedSearchInput(''); | ||
} | ||
}, [open]); | ||
|
||
|
@@ -207,19 +208,27 @@ export const DrillByMenuItems = ({ | |
hasDrillBy, | ||
]); | ||
|
||
const handleInput = debounce( | ||
(value: string) => setSearchInput(value), | ||
FAST_DEBOUNCE, | ||
const debouncedSetSearchInput = useMemo( | ||
() => | ||
debounce((value: string) => { | ||
setDebouncedSearchInput(value); | ||
}, FAST_DEBOUNCE), | ||
[], | ||
); | ||
|
||
const handleInput = (value: string) => { | ||
setSearchInput(value); | ||
debouncedSetSearchInput(value); | ||
}; | ||
|
||
const filteredColumns = useMemo( | ||
() => | ||
columns.filter(column => | ||
(column.verbose_name || column.column_name) | ||
.toLowerCase() | ||
.includes(searchInput.toLowerCase()), | ||
.includes(debouncedSearchInput.toLowerCase()), | ||
), | ||
[columns, searchInput], | ||
[columns, debouncedSearchInput], | ||
); | ||
|
||
const submenuYOffset = useMemo( | ||
|
@@ -311,6 +320,7 @@ export const DrillByMenuItems = ({ | |
margin: ${theme.gridUnit * 2}px ${theme.gridUnit * 3}px; | ||
box-shadow: none; | ||
`} | ||
value={searchInput} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for this. It seems unrelated to the upgrade? What is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is said binding of the state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this approach does not work. Watch the example video. I cannot even type right now, it will lag really bad. COVID.Vaccine.Dashboard.mp4 |
||
/> | ||
)} | ||
{isLoadingColumns ? ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { InputProps, TextAreaProps } from 'antd-v5/lib/input'; | ||
import { InputNumberProps } from 'antd-v5/lib/input-number'; | ||
import { AntdThemeProvider } from 'src/components/AntdThemeProvider'; | ||
import { Input, TextArea, InputNumber } from '.'; | ||
|
||
export default { | ||
title: 'Input', | ||
component: Input, | ||
}; | ||
|
||
export const InteractiveInput = (args: InputProps) => ( | ||
<AntdThemeProvider> | ||
<Input {...args} /> | ||
</AntdThemeProvider> | ||
); | ||
|
||
export const InteractiveInputNumber = (args: InputNumberProps) => ( | ||
<AntdThemeProvider> | ||
<InputNumber {...args} /> | ||
</AntdThemeProvider> | ||
); | ||
|
||
export const InteractiveTextArea = (args: TextAreaProps) => ( | ||
<AntdThemeProvider> | ||
<TextArea {...args} /> | ||
</AntdThemeProvider> | ||
); | ||
|
||
InteractiveInput.args = { | ||
allowClear: false, | ||
disabled: false, | ||
showCount: false, | ||
type: 'text', | ||
variant: 'outlined', | ||
}; | ||
|
||
InteractiveInput.argTypes = { | ||
defaultValue: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
|
||
id: { | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
|
||
maxLength: { | ||
control: { | ||
type: 'number', | ||
}, | ||
}, | ||
|
||
status: { | ||
control: { | ||
type: 'select', | ||
}, | ||
options: ['error', 'warning'], | ||
}, | ||
|
||
size: { | ||
control: { | ||
type: 'select', | ||
}, | ||
options: ['large', 'middle', 'small'], | ||
}, | ||
variant: { | ||
control: { | ||
type: 'select', | ||
}, | ||
options: ['outlined', 'borderless', 'filled'], | ||
}, | ||
}; | ||
|
||
InteractiveTextArea.args = { | ||
...InteractiveInput.args, | ||
autoSize: false, | ||
}; | ||
|
||
InteractiveTextArea.argTypes = InteractiveInput.argTypes; | ||
|
||
InteractiveInputNumber.args = { | ||
autoFocus: false, | ||
disabled: false, | ||
keyboard: true, | ||
max: Number.MAX_SAFE_INTEGER, | ||
min: Number.MIN_SAFE_INTEGER, | ||
readonly: false, | ||
step: 1, | ||
stringMode: false, | ||
}; | ||
|
||
InteractiveInputNumber.argTypes = { | ||
controls: { | ||
control: { type: 'boolean' }, | ||
}, | ||
|
||
decimalSeperator: { | ||
control: { type: 'text' }, | ||
}, | ||
|
||
placeholder: { | ||
control: { type: 'text' }, | ||
}, | ||
|
||
defaultValue: { | ||
control: { type: 'number' }, | ||
}, | ||
|
||
precision: { | ||
control: { type: 'number' }, | ||
}, | ||
|
||
status: InteractiveInput.argTypes.status, | ||
size: InteractiveInput.argTypes.size, | ||
variant: InteractiveInput.argTypes.variant, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { render } from 'spec/helpers/testing-library'; | ||
import { Input, InputNumber, TextArea } from '.'; | ||
|
||
test('should render Input', () => { | ||
const { container } = render(<Input />); | ||
expect(container).toBeInTheDocument(); | ||
}); | ||
|
||
test('should render InputNumber', () => { | ||
const { container } = render(<InputNumber />); | ||
expect(container).toBeInTheDocument(); | ||
}); | ||
|
||
test('should render TextArea', () => { | ||
const { container } = render(<TextArea />); | ||
expect(container).toBeInTheDocument(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,8 @@ test('renders two inputs', () => { | |
test('receives null on non-numeric', async () => { | ||
render(<BoundsControl {...defaultProps} />); | ||
const minInput = screen.getAllByRole('spinbutton')[0]; | ||
userEvent.type(minInput, 'text'); | ||
userEvent.type(minInput, '1'); | ||
userEvent.clear(minInput); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will be giving a false positive for this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. InputNumber only accepts numbers by default on v5 so typing 'text' does not fire onChange. This test might be obsolete alltogether. |
||
await waitFor(() => | ||
expect(defaultProps.onChange).toHaveBeenCalledWith([null, null]), | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need this anymore? It seems unrelated to the upgrade
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no setValue method for Input after the migration so this would give type errors. Binding the value to the already kept state makes this obsolete. Another option would be
ref.current?.input?.value = ''
which seemed worse to me.Relevant issue: ant-design/ant-design#34425