Skip to content
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: Use Antd Dropdown instead of react-bootstrap in DatasourceControl #11395

Merged
merged 10 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ describe('Datasource control', () => {
cy.visitChartByName('Num Births Trend');
cy.verifySliceSuccess({ waitAlias: '@postJson' });

cy.get('#datasource_menu').click();
cy.get('[data-test="datasource-menu"]').click();

cy.get('script').then(nodes => {
numScripts = nodes.length;
});

cy.get('a').contains('Edit Dataset').click();
cy.get('[data-test="edit-dataset"]').click();

// should load additional scripts for the modal
cy.get('script').then(nodes => {
Expand All @@ -65,8 +65,8 @@ describe('Datasource control', () => {
.focus()
.type(newMetricName, { force: true });
// delete metric
cy.get('#datasource_menu').click();
cy.get('a').contains('Edit Dataset').click();
cy.get('[data-test="datasource_menu"]').click();
cy.get('[data-test="edit-dataset"]').click();
cy.get('.modal-content').within(() => {
cy.get('a[role="tab"]').contains('Metrics').click();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import React from 'react';
import sinon from 'sinon';
import configureStore from 'redux-mock-store';
import { shallow } from 'enzyme';
import { MenuItem } from 'react-bootstrap';
import { Menu } from 'src/common/components';
import DatasourceModal from 'src/datasource/DatasourceModal';
import ChangeDatasourceModal from 'src/datasource/ChangeDatasourceModal';
import DatasourceControl from 'src/explore/components/controls/DatasourceControl';
Expand Down Expand Up @@ -73,16 +73,18 @@ describe('DatasourceControl', () => {
it('show or hide Edit Datasource option', () => {
let wrapper = setup();
expect(wrapper.find('#datasource_menu')).toExist();
expect(wrapper.find('#datasource_menu').dive().find(MenuItem)).toHaveLength(
3,
let menuWrapper = shallow(
<div>{wrapper.find('#datasource_menu').prop('overlay')}</div>,
);
expect(menuWrapper.find(Menu.Item)).toHaveLength(3);

wrapper = setup({
isEditable: false,
});
expect(wrapper.find('#datasource_menu')).toExist();
expect(wrapper.find('#datasource_menu').dive().find(MenuItem)).toHaveLength(
2,
menuWrapper = shallow(
<div>{wrapper.find('#datasource_menu').prop('overlay')}</div>,
);
expect(menuWrapper.find(Menu.Item)).toHaveLength(2);
});
});
27 changes: 27 additions & 0 deletions superset-frontend/src/common/components/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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 React from 'react';
import { Tooltip as BaseTooltip } from 'src/common/components';
import { TooltipProps } from 'antd/lib/tooltip';

const Tooltip = (props: TooltipProps) => (
<BaseTooltip overlayStyle={{ fontSize: '12px' }} {...props} />
);

export default Tooltip;
31 changes: 30 additions & 1 deletion superset-frontend/src/common/components/common.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import React from 'react';
import { action } from '@storybook/addon-actions';
import { withKnobs, boolean, select } from '@storybook/addon-knobs';
import Button from 'src/components/Button';
import Modal from './Modal';
import Tabs, { EditableTabs } from './Tabs';
import AntdPopover from './Popover';
import AntdTooltip from './Tooltip';
import { Menu } from '.';
import { Dropdown } from './Dropdown';
import Button from '../../components/Button';

export default {
title: 'Common Components',
Expand Down Expand Up @@ -146,3 +147,31 @@ export const Popover = () => (
<Button>TRIGGER</Button>
</AntdPopover>
);

export const Tooltip = () => (
<AntdTooltip
title="This is a Tooltip"
trigger={select('Trigger', ['click', 'hover', 'focus'], 'click')}
placement={select(
'Placement',
[
'topLeft',
'top',
'topRight',
'leftTop',
'left',
'leftBottom',
'rightTop',
'right',
'rightBottom',
'bottomLeft',
'bottom',
'bottomRight',
],
'topLeft',
)}
arrowPointAtCenter={boolean('Arrow point at center', false)}
>
<Button>A button with tooltip</Button>
</AntdTooltip>
);
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,12 @@
*/
import React from 'react';
import PropTypes from 'prop-types';
import {
Col,
Collapse,
DropdownButton,
MenuItem,
OverlayTrigger,
Row,
Tooltip,
Well,
} from 'react-bootstrap';
import { Col, Collapse, Row, Well } from 'react-bootstrap';
import { t, styled } from '@superset-ui/core';
import { ColumnOption, MetricOption } from '@superset-ui/chart-controls';

import TooltipWrapper from 'src/components/TooltipWrapper';

import { Dropdown, Menu } from 'src/common/components';
import Tooltip from 'src/common/components/Tooltip';
import Icon from 'src/components/Icon';
import ChangeDatasourceModal from 'src/datasource/ChangeDatasourceModal';
import DatasourceModal from 'src/datasource/DatasourceModal';
Expand All @@ -58,7 +49,7 @@ const defaultProps = {
};

const Styles = styled.div`
#datasource_menu {
.ant-dropdown-trigger {
margin-left: ${({ theme }) => theme.gridUnit}px;
box-shadow: none;
&:active {
Expand Down Expand Up @@ -98,6 +89,7 @@ class DatasourceControl extends React.PureComponent {
this.toggleEditDatasourceModal = this.toggleEditDatasourceModal.bind(this);
this.toggleShowDatasource = this.toggleShowDatasource.bind(this);
this.renderDatasource = this.renderDatasource.bind(this);
this.handleMenuItemClick = this.handleMenuItemClick.bind(this);
}

onDatasourceSave(datasource) {
Expand Down Expand Up @@ -125,6 +117,15 @@ class DatasourceControl extends React.PureComponent {
}));
}

handleMenuItemClick({ key }) {
if (key === '1') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are already here, can you make these keys more human readable and maybe use constants?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

this.toggleChangeDatasourceModal();
}
if (key === '3') {
this.toggleEditDatasourceModal();
}
}

renderDatasource() {
const { datasource } = this.props;
const { showDatasource } = this.state;
Expand Down Expand Up @@ -176,18 +177,32 @@ class DatasourceControl extends React.PureComponent {
showDatasource,
} = this.state;
const { datasource, onChange, value } = this.props;

const datasourceMenu = (
<Menu onClick={this.handleMenuItemClick}>
<Menu.Item key="1">{t('Change Dataset')}</Menu.Item>
<Menu.Item key="2">
<a
href={`/superset/sqllab?datasourceKey=${value}`}
target="_blank"
rel="noopener noreferrer"
>
{t('Explore in SQL Lab')}
</a>
</Menu.Item>
{this.props.isEditable && (
<Menu.Item key="3" data-test="edit-dataset">
{t('Edit Dataset')}
</Menu.Item>
)}
</Menu>
);

return (
<Styles className="DatasourceControl">
<ControlHeader {...this.props} />
<div>
<OverlayTrigger
placement="top"
overlay={
<Tooltip id="toggle-dataset-tooltip">
{t('Expand/collapse dataset configuration')}
</Tooltip>
}
>
<Tooltip title={t('Expand/collapse dataset configuration')}>
<Label
style={{ textTransform: 'none' }}
onClick={this.toggleShowDatasource}
Expand All @@ -199,43 +214,17 @@ class DatasourceControl extends React.PureComponent {
}`}
/>
</Label>
</OverlayTrigger>
<TooltipWrapper
label="change-datasource"
tooltip={t('More dataset related options')}
trigger={['hover']}
</Tooltip>
<Dropdown
overlay={datasourceMenu}
trigger={['click']}
id="datasource_menu"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can get rid of id since you added data-test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually there are some styles for that, in DatasourceControl.less. @kgabryje would you mind seeing if those styles are still necessary/relevant? If they are, this may be the right opportunity to bring them into the component with Emotion Theme variables wired in. One less LESS file!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, 1 less file less 🙂

data-test="datasource-menu"
>
<DropdownButton
title={<Icon name="more-horiz" />}
className=""
bsSize="sm"
id="datasource_menu"
data-test="datasource-menu"
>
<MenuItem eventKey="3" onClick={this.toggleChangeDatasourceModal}>
{t('Change Dataset')}
</MenuItem>
{datasource.type === 'table' && (
<MenuItem
eventKey="3"
href={`/superset/sqllab?datasourceKey=${value}`}
target="_blank"
rel="noopener noreferrer"
>
{t('Explore in SQL Lab')}
</MenuItem>
)}
{this.props.isEditable && (
<MenuItem
data-test="edit-dataset"
eventKey="3"
onClick={this.toggleEditDatasourceModal}
>
{t('Edit Dataset')}
</MenuItem>
)}
</DropdownButton>
</TooltipWrapper>
<Tooltip title={t('More dataset related options')}>
<Icon data-test="datasource-menu" name="more-horiz" />
</Tooltip>
</Dropdown>
</div>
<Collapse in={this.state.showDatasource}>
{this.renderDatasource()}
Expand Down