Skip to content

Commit

Permalink
refactor(Switch): Upgrade Switch to Ant Design 5 (apache#30731)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrusoare authored Oct 30, 2024
1 parent 9bb69ab commit bc5da63
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 22 deletions.
7 changes: 4 additions & 3 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ import Mousetrap from 'mousetrap';
import Button from 'src/components/Button';
import Timer from 'src/components/Timer';
import ResizableSidebar from 'src/components/ResizableSidebar';
import { AntdDropdown, AntdSwitch, Skeleton } from 'src/components';
import { AntdDropdown, Skeleton } from 'src/components';
import { Switch } from 'src/components/Switch';
import { Input } from 'src/components/Input';
import { Menu } from 'src/components/Menu';
import Icons from 'src/components/Icons';
Expand Down Expand Up @@ -698,15 +699,15 @@ const SqlEditor: FC<Props> = ({
<Menu.Item css={{ display: 'flex', justifyContent: 'space-between' }}>
{' '}
<span>{t('Render HTML')}</span>{' '}
<AntdSwitch
<Switch
checked={renderHTMLEnabled}
onChange={handleToggleRenderHTMLEnabled}
/>{' '}
</Menu.Item>
<Menu.Item css={{ display: 'flex', justifyContent: 'space-between' }}>
{' '}
<span>{t('Autocomplete')}</span>{' '}
<AntdSwitch
<Switch
checked={autocompleteEnabled}
onChange={handleToggleAutocompleteEnabled}
/>{' '}
Expand Down
5 changes: 4 additions & 1 deletion superset-frontend/src/components/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ export const InteractiveSwitch = ({ checked, ...rest }: SwitchProps) => {
/>
);
};
const defaultCheckedValue = true;

InteractiveSwitch.args = {
checked: false,
checked: defaultCheckedValue,
disabled: false,
loading: false,
title: 'Switch',
defaultChecked: defaultCheckedValue,
autoFocus: true,
};

InteractiveSwitch.argTypes = {
Expand Down
41 changes: 41 additions & 0 deletions superset-frontend/src/components/Switch/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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, screen } from 'spec/helpers/testing-library';
import { Switch } from '.';

const mockedProps = {
label: 'testLabel',
dataTest: 'dataTest',
checked: false,
};

test('should render', () => {
const { container } = render(<Switch {...mockedProps} />);
expect(container).toBeInTheDocument();
});

test('should have the correct checked prop', () => {
render(<Switch {...mockedProps} />);

const switchElement = screen.getByRole('switch');

expect(switchElement).toBeInTheDocument();
expect(switchElement).not.toBeChecked();
});
12 changes: 3 additions & 9 deletions superset-frontend/src/components/Switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import { styled } from '@superset-ui/core';
import BaseSwitch, { SwitchProps } from 'antd/lib/switch';
import { SwitchProps } from 'antd-v5/lib/switch';
import { Switch as AntdSwitch } from 'antd-v5';

const StyledSwitch = styled(BaseSwitch)`
.ant-switch-checked {
background-color: ${({ theme }) => theme.colors.primary.base};
}
`;

export const Switch = (props: SwitchProps) => <StyledSwitch {...props} />;
export const Switch = (props: SwitchProps) => <AntdSwitch {...props} />;
export type { SwitchProps };
1 change: 0 additions & 1 deletion superset-frontend/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export {
Modal as AntdModal,
Select as AntdSelect,
Slider as AntdSlider,
Switch as AntdSwitch,
Tabs as AntdTabs,
Tooltip as AntdTooltip,
} from 'antd';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
import { SupersetTheme, t } from '@superset-ui/core';
import { AntdSwitch } from 'src/components';
import { Switch } from 'src/components/Switch';
import InfoTooltip from 'src/components/InfoTooltip';
import ValidatedInput from 'src/components/Form/LabeledErrorBoundInput';
import { FieldPropTypes } from '../../types';
Expand Down Expand Up @@ -296,7 +296,7 @@ export const forceSSLField = ({
sslForced,
}: FieldPropTypes) => (
<div css={(theme: SupersetTheme) => infoTooltip(theme)}>
<AntdSwitch
<Switch
disabled={sslForced && !isEditMode}
checked={db?.parameters?.encryption || sslForced}
onChange={changed => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ jest.mock('@superset-ui/core', () => ({
isFeatureEnabled: jest.fn().mockReturnValue(true),
}));

jest.mock('src/components', () => ({
AntdSwitch: ({
jest.mock('src/components/Switch', () => ({
Switch: ({
checked,
onChange,
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
isFeatureEnabled,
FeatureFlag,
} from '@superset-ui/core';
import { AntdSwitch } from 'src/components';
import { Switch } from 'src/components/Switch';
import InfoTooltip from 'src/components/InfoTooltip';
import { isEmpty } from 'lodash';
import { infoTooltip, toggleStyle } from './styles';
Expand Down Expand Up @@ -80,7 +80,7 @@ const SSHTunnelSwitch = ({

return isSSHTunnelEnabled ? (
<div css={(theme: SupersetTheme) => infoTooltip(theme)}>
<AntdSwitch
<Switch
checked={isChecked}
onChange={handleOnChange}
data-test="ssh-tunnel-switch"
Expand Down
4 changes: 2 additions & 2 deletions superset-frontend/src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import {
loadingCardCount,
mq,
} from 'src/views/CRUD/utils';
import { AntdSwitch } from 'src/components';
import { Switch } from 'src/components/Switch';
import getBootstrapData from 'src/utils/getBootstrapData';
import { TableTab } from 'src/views/CRUD/types';
import SubMenu, { SubMenuProps } from 'src/features/home/SubMenu';
Expand Down Expand Up @@ -340,7 +340,7 @@ function Welcome({ user, addDangerToast }: WelcomeProps) {
name: (
<WelcomeNav>
<div className="switch">
<AntdSwitch checked={checked} onClick={handleToggle} />
<Switch checked={checked} onClick={handleToggle} />
<span>{t('Thumbnails')}</span>
</div>
</WelcomeNav>
Expand Down
4 changes: 4 additions & 0 deletions superset-frontend/src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const baseConfig: ThemeConfig = {
handleSizeHover: 10,
handleLineWidthHover: 2,
},
Switch: {
colorPrimaryHover: supersetTheme.colors.primary.base,
colorTextTertiary: supersetTheme.colors.grayscale.light1,
},
},
};

Expand Down

0 comments on commit bc5da63

Please sign in to comment.