This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 271
feat(control-utils): add infotooltipwithtrigger #442
Merged
kristw
merged 13 commits into
apache-superset:master
from
preset-io:phillip/SO-264-partitionchart-controls-packages
May 7, 2020
+122
−1
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5dc5d23
PR to merge packges for controls migration
5efe029
fix: add bootstrap
9acf813
fix: change tooltip to tsx
bf1296b
fix: types
00561e3
fix: tsx error
ed63e91
appease linter, enhance a11y
suddjian 3fa07d2
addressing PR feedback
suddjian dc8cb7e
tweaking bootstrap types
suddjian 7f27efe
fix test
suddjian 694c63d
test enter key
suddjian 8cac2d1
moar tests
suddjian c0ce659
code > key
suddjian 1c4be62
ugh fine
suddjian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/superset-ui-control-utils/src/InfoTooltipWithTrigger.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
|
||
const tooltipStyle: React.CSSProperties = { wordWrap: 'break-word' }; | ||
|
||
interface Props { | ||
label?: string; | ||
tooltip?: string; | ||
icon?: string; | ||
onClick?: () => void; | ||
placement?: string; | ||
bsStyle?: string; | ||
className?: string; | ||
} | ||
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. Make those with defaults optional. interface Props {
label: string;
icon: string;
tooltip?: string;
onClick?: () => void;
placement?: string;
bsStyle?: string;
className?: string;
} 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. +1 @ktmud with the small change to use 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.
|
||
|
||
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/superset-ui-control-utils/test/InfoTooltipWithTrigger.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Prefer to remove
// @ts-ignore
and add proper typing support by installing@types/react-bootstrap
.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.
If I remember correctly, bootstrap 3 doesn't have types support. We ran into this problem in incubator-superset as well.
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.
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
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.
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.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.
Nice! That means we can add types for bootstrap back in incubator-superset too
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.
@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?
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.
Sorry, should've used
@types/react-bootstrap @ ^0.32.21
. This is also what's used inincubator-superset
.Btw, it's preferable to have
@types/*
independencies
instead ofdevDependencies
so that dependent packages can have easier access to typings when the library is used innpm link
.