Skip to content

Commit

Permalink
feat(vizgallery): Double-click viz type to submit form (#20513)
Browse files Browse the repository at this point in the history
* Make double-clicking on viz type submit the form.

* Add tests.
  • Loading branch information
codyml committed Jun 28, 2022
1 parent 480ee38 commit 280b4be
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
21 changes: 21 additions & 0 deletions superset-frontend/src/addSlice/AddSliceContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ test('renders an enabled button if datasource and viz type are selected', async
).toHaveLength(0);
});

test('double-click viz type does nothing if no datasource is selected', async () => {
const wrapper = await getWrapper();
wrapper.instance().gotoSlice = jest.fn();
wrapper.update();
wrapper.instance().onVizTypeDoubleClick();
expect(wrapper.instance().gotoSlice).not.toBeCalled();
});

test('double-click viz type submits if datasource is selected', async () => {
const wrapper = await getWrapper();
wrapper.instance().gotoSlice = jest.fn();
wrapper.update();
wrapper.setState({
datasource,
visType: 'table',
});

wrapper.instance().onVizTypeDoubleClick();
expect(wrapper.instance().gotoSlice).toBeCalled();
});

test('formats Explore url', async () => {
const wrapper = await getWrapper();
wrapper.setState({
Expand Down
8 changes: 8 additions & 0 deletions superset-frontend/src/addSlice/AddSliceContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export default class AddSliceContainer extends React.PureComponent<
this.gotoSlice = this.gotoSlice.bind(this);
this.newLabel = this.newLabel.bind(this);
this.loadDatasources = this.loadDatasources.bind(this);
this.onVizTypeDoubleClick = this.onVizTypeDoubleClick.bind(this);
}

exploreUrl() {
Expand Down Expand Up @@ -251,6 +252,12 @@ export default class AddSliceContainer extends React.PureComponent<
return !(this.state.datasource?.value && this.state.visType);
}

onVizTypeDoubleClick() {
if (!this.isBtnDisabled()) {
this.gotoSlice();
}
}

newLabel(item: Dataset) {
return (
<Tooltip
Expand Down Expand Up @@ -368,6 +375,7 @@ export default class AddSliceContainer extends React.PureComponent<
<VizTypeGallery
className="viz-gallery"
onChange={this.changeVisType}
onDoubleClick={this.onVizTypeDoubleClick}
selectedViz={this.state.visType}
/>
</StyledStepDescription>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,22 @@ describe('VizTypeControl', () => {
within(visualizations).queryByText('Pie Chart'),
).not.toBeInTheDocument();
});

it('Submit on viz type double-click', () => {
renderWrapper();
userEvent.click(screen.getByRole('button', { name: 'ballot All charts' }));
const visualizations = screen.getByTestId(getTestId('viz-row'));
userEvent.click(
within(visualizations).getByText('Time-series Bar Chart v2'),
);

expect(defaultProps.onChange).not.toBeCalled();
userEvent.dblClick(
within(visualizations).getByText('Time-series Line Chart'),
);

expect(defaultProps.onChange).toHaveBeenCalledWith(
'echarts_timeseries_line',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import scrollIntoView from 'scroll-into-view-if-needed';

interface VizTypeGalleryProps {
onChange: (vizType: string | null) => void;
onDoubleClick: () => void;
selectedViz: string | null;
className?: string;
}
Expand Down Expand Up @@ -380,12 +381,14 @@ interface ThumbnailProps {
entry: VizEntry;
selectedViz: string | null;
setSelectedViz: (viz: string) => void;
onDoubleClick: () => void;
}

const Thumbnail: React.FC<ThumbnailProps> = ({
entry,
selectedViz,
setSelectedViz,
onDoubleClick,
}) => {
const theme = useTheme();
const { key, value: type } = entry;
Expand All @@ -400,6 +403,7 @@ const Thumbnail: React.FC<ThumbnailProps> = ({
tabIndex={0}
className={isSelected ? 'selected' : ''}
onClick={() => setSelectedViz(key)}
onDoubleClick={onDoubleClick}
data-test="viztype-selector-container"
>
<img
Expand Down Expand Up @@ -429,6 +433,7 @@ interface ThumbnailGalleryProps {
vizEntries: VizEntry[];
selectedViz: string | null;
setSelectedViz: (viz: string) => void;
onDoubleClick: () => void;
}

/** A list of viz thumbnails, used within the viz picker modal */
Expand Down Expand Up @@ -487,7 +492,7 @@ const doesVizMatchSelector = (viz: ChartMetadata, selector: string) =>
(viz.tags || []).indexOf(selector) > -1;

export default function VizTypeGallery(props: VizTypeGalleryProps) {
const { selectedViz, onChange, className } = props;
const { selectedViz, onChange, onDoubleClick, className } = props;
const { mountedPluginMetadata } = usePluginContext();
const searchInputRef = useRef<HTMLInputElement>();
const [searchInputValue, setSearchInputValue] = useState('');
Expand Down Expand Up @@ -793,6 +798,7 @@ export default function VizTypeGallery(props: VizTypeGalleryProps) {
vizEntries={getVizEntriesToDisplay()}
selectedViz={selectedViz}
setSelectedViz={onChange}
onDoubleClick={onDoubleClick}
/>
</RightPane>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const VizTypeControl = ({
key={modalKey}
selectedViz={selectedViz}
onChange={setSelectedViz}
onDoubleClick={onSubmit}
/>
</UnpaddedModal>
</>
Expand Down

0 comments on commit 280b4be

Please sign in to comment.