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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(control-utils): add infotooltipwithtrigger (#442)
* PR to merge packges for controls migration * fix: add bootstrap * fix: change tooltip to tsx * fix: types * fix: tsx error * appease linter, enhance a11y * addressing PR feedback * tweaking bootstrap types * fix test * test enter key * moar tests * code > key * ugh fine Co-authored-by: Phillip Kelley-Dotson <pkd@pkd.lan> Co-authored-by: David Aaron Suddjian <aasuddjian@gmail.com>
- Loading branch information
1 parent
ec869d7
commit 488e945
Showing
4 changed files
with
122 additions
and
1 deletion.
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; | ||
} | ||
|
||
export default function InfoTooltipWithTrigger({ | ||
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); | ||
}); | ||
}); |