Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

feat(control-utils): add infotooltipwithtrigger #442

Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 10 additions & 1 deletion packages/superset-ui-control-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
},
"peerDependencies": {
"@superset-ui/translation": "^0.13",
"@superset-ui/validator": "^0.13"
"@superset-ui/validator": "^0.13",
"react": "^16.13.1"
},
"devDependencies": {
"enzyme": "^3.11.0"
},
"dependencies": {
"@types/react-bootstrap": "0.32.21",
"lodash": "^4.17.15",
"react-bootstrap": "^0.33.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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 { kebabCase } from 'lodash';
import { Tooltip, OverlayTrigger } from 'react-bootstrap';
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer to remove // @ts-ignore and add proper typing support by installing @types/react-bootstrap.

Copy link
Member

Choose a reason for hiding this comment

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

If I remember correctly, bootstrap 3 doesn't have types support. We ran into this problem in incubator-superset as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of @ts-ignore, you can add a declaration file. Even if it is an empty declaration

Example:
https://github.com/apache-superset/superset-ui/blob/master/packages/superset-ui-translation/types/external.d.ts

Copy link
Contributor

Choose a reason for hiding this comment

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

I think @types/react-bootstrap @ 0.32.2 works with Bootstrap v3. There might be cases with suboptimal typings, where you can then do @ts-ignore per module.

Copy link
Member

Choose a reason for hiding this comment

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

Nice! That means we can add types for bootstrap back in incubator-superset too

Copy link
Member

Choose a reason for hiding this comment

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

@ktmud I tried adding the types for bootstrap, but that leads to an issue in CI. It looks like our version of typescript isn't happy with those type definitions, any ideas on how to fix?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, should've used @types/react-bootstrap @ ^0.32.21. This is also what's used in incubator-superset.

Btw, it's preferable to have @types/* in dependencies instead of devDependencies so that dependent packages can have easier access to typings when the library is used in npm link.


const tooltipStyle: React.CSSProperties = { wordWrap: 'break-word' };

interface Props {
label?: string;
tooltip?: string;
icon?: string;
onClick?: () => void;
placement?: string;
bsStyle?: string;
className?: string;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Make those with defaults optional.

interface Props {
  label: string;
  icon: string;
  tooltip?: string;
  onClick?: () => void;
  placement?: string;
  bsStyle?: string;
  className?: string;
}

Copy link
Contributor

@kristw kristw May 6, 2020

Choose a reason for hiding this comment

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

+1 @ktmud with the small change to use type for Props instead of interface. (Just convention from guidelines at Airbnb and used throughout the codebase at the moment). Also export the type.

Copy link
Contributor

Choose a reason for hiding this comment

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

interface means the properties are immutable which often runs into problems when you want to extend the component and override the prop types.


export default function InfoTooltipWithTrigger({
kristw marked this conversation as resolved.
Show resolved Hide resolved
label,
tooltip,
bsStyle,
onClick,
icon = 'info-circle',
className = 'text-muted',
placement = 'right',
}: Props) {
const iconClass = `fa fa-${icon} ${className} ${bsStyle ? `text-${bsStyle}` : ''}`;
const iconEl = (
<i
role="button"
tabIndex={0}
className={iconClass}
style={{ cursor: onClick ? 'pointer' : undefined }}
onClick={onClick}
onKeyPress={
onClick &&
((event: React.KeyboardEvent) => {
if (event.key === 'Enter' || event.key === ' ') {
onClick();
}
})
}
/>
);
if (!tooltip) {
return iconEl;
}
return (
<OverlayTrigger
placement={placement}
overlay={
<Tooltip id={`${kebabCase(label)}-tooltip`} style={tooltipStyle}>
{tooltip}
</Tooltip>
}
>
{iconEl}
</OverlayTrigger>
);
}
1 change: 1 addition & 0 deletions packages/superset-ui-control-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import * as sectionModules from './sections';
export const sections = sectionModules;
export { D3_FORMAT_DOCS, D3_FORMAT_OPTIONS, D3_TIME_FORMAT_OPTIONS } from './D3Formatting';
export { formatSelectOptions, formatSelectOptionsForRange } from './selectOptions';
export { default as InfoTooltipWithTrigger } from './InfoTooltipWithTrigger';
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { shallow } from 'enzyme';
import { OverlayTrigger } from 'react-bootstrap';
import InfoTooltipWithTrigger from '../src/InfoTooltipWithTrigger';

describe('InfoTooltipWithTrigger', () => {
it('renders a tooltip', () => {
const wrapper = shallow(<InfoTooltipWithTrigger label="test" tooltip="this is a test" />);
expect(wrapper.find(OverlayTrigger)).toHaveLength(1);
});

it('renders an info icon', () => {
const wrapper = shallow(<InfoTooltipWithTrigger />);
expect(wrapper.find('.fa-info-circle')).toHaveLength(1);
});

it('responds to keypresses', () => {
const clickHandler = jest.fn();
const wrapper = shallow(
<InfoTooltipWithTrigger label="test" tooltip="this is a test" onClick={clickHandler} />,
);
wrapper.find('.fa-info-circle').simulate('keypress', { key: 'Tab' });
expect(clickHandler).toHaveBeenCalledTimes(0);
wrapper.find('.fa-info-circle').simulate('keypress', { key: 'Enter' });
expect(clickHandler).toHaveBeenCalledTimes(1);
wrapper.find('.fa-info-circle').simulate('keypress', { key: ' ' });
expect(clickHandler).toHaveBeenCalledTimes(2);
});

it('has a bsStyle', () => {
const wrapper = shallow(<InfoTooltipWithTrigger bsStyle="something" />);
expect(wrapper.find('.text-something')).toHaveLength(1);
});
});