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

Add tooltips to even more widgets #2594

Merged
23 commits merged into from Jan 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions e2e/scripts/st_tooltips.py
Expand Up @@ -13,26 +13,26 @@
# limitations under the License.

import streamlit as st
from datetime import datetime
from datetime import time

st.text_input("some input text", "default text", help="tooltip")
st.button("some button", help="tooltip")
st.selectbox("selectbox", ("a", "b", "c"), 0, help="tooltip")
st.time_input("time", datetime(2019, 7, 6, 21, 15), help="tooltip")
st.date_input("date", datetime(2019, 7, 6, 21, 15), help="tooltip")


def future_tests():
from datetime import datetime
from datetime import time
import numpy as np
from numpy.random import randn
from plotly import figure_factory
import pandas as pd

st.button("some button", help="tooltip")
st.checkbox("some checkbox", help="tooltip")
st.number_input("number input", value=1, help="tooltip")
st.radio("some radio", ("a", "b", "c"), 0, help="tooltip")

st.selectbox("selectbox", ("a", "b", "c"), 0, help="tooltip")
st.time_input("time", datetime(2019, 7, 6, 21, 15), help="tooltip")
st.date_input("date", datetime(2019, 7, 6, 21, 15), help="tooltip")
st.write("here is some text", help="tooltip")
st.markdown("here is some text", help="tooltip")
st.header("some header", help="tooltip")
Expand Down
20 changes: 16 additions & 4 deletions e2e/specs/st_tooltips.spec.js
Expand Up @@ -24,14 +24,26 @@ describe("tooltips on widgets", () => {
cy.get(".stTextInput .stTooltipIcon").should("have.length", 1);
});

it("displays tooltip on button", () => {
cy.get(".stButton .stTooltipIcon").should("have.length", 1);
});

it("displays tooltip on selectbox", () => {
cy.get(".stSelectbox .stTooltipIcon").should("have.length", 1);
});

it("displays tooltip on time_input", () => {
cy.get(".stTimeInput .stTooltipIcon").should("have.length", 1);
});

it("displays tooltip on date_input", () => {
cy.get(".stDateInput .stTooltipIcon").should("have.length", 1);
});

/*
it("displays tooltip on button", () => {});
it("displays tooltip on checkbox", () => {});
it("displays tooltip on number_input", () => {});
it("displays tooltip on radio", () => {});
it("displays tooltip on selectbox", () => {});
it("displays tooltip on time_input", () => {});
it("displays tooltip on date_input", () => {});
it("displays tooltip on write", () => {});
it("displays tooltip on markdown", () => {});
it("displays tooltip on header", () => {});
Expand Down
7 changes: 6 additions & 1 deletion frontend/src/components/shared/Tooltip/Tooltip.tsx
Expand Up @@ -21,19 +21,22 @@ export interface TooltipProps {
content: ReactNode
placement: Placement
children: ReactNode
inline?: boolean
}

function Tooltip({
content,
placement,
children,
inline,
}: TooltipProps): ReactElement {
return (
<StatefulTooltip
content={content}
placement={PLACEMENT[placement]}
accessibilityType={ACCESSIBILITY_TYPE.tooltip}
showArrow
popoverMargin={10}
overrides={{
Arrow: {
style: {
Expand All @@ -56,7 +59,9 @@ function Tooltip({
}}
>
{/* BaseWeb manipulates its child, so we create a wrapper div for protection */}
<div>{children}</div>
<div style={{ display: inline ? "inline-block" : "block" }}>
{children}
</div>
</StatefulTooltip>
)
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/shared/TooltipIcon/TooltipIcon.tsx
Expand Up @@ -19,6 +19,7 @@ function TooltipIcon({
<Tooltip
content={<StreamlitMarkdown source={content} allowHTML />}
placement={placement}
inline
>
<HelpCircleIcon size={iconSize} />
</Tooltip>
Expand Down
42 changes: 34 additions & 8 deletions frontend/src/components/widgets/Button/Button.tsx
Expand Up @@ -19,6 +19,8 @@ import React, { ReactElement } from "react"
import UIButton, { Kind, Size } from "components/shared/Button"
import { Button as ButtonProto } from "autogen/proto"
import { WidgetStateManager } from "lib/WidgetStateManager"
import Tooltip, { Placement } from "components/shared/Tooltip"
import StreamlitMarkdown from "components/shared/StreamlitMarkdown"

export interface ButtonProps {
disabled: boolean
Expand All @@ -27,6 +29,28 @@ export interface ButtonProps {
width: number
}

interface ButtonTooltipProps {
children: ReactElement
help?: string
}

function ButtonTooltip({ children, help }: ButtonTooltipProps): ReactElement {
if (!help) {
return children
}
return (
<div className="stTooltipIcon">
<Tooltip
inline
content={<StreamlitMarkdown source={help} allowHTML />}
placement={Placement.RIGHT}
>
{children}
</Tooltip>
</div>
)
}

function Button(props: ButtonProps): ReactElement {
const { disabled, element, widgetMgr, width } = props
const style = { width }
Expand All @@ -38,14 +62,16 @@ function Button(props: ButtonProps): ReactElement {

return (
<div className="row-widget stButton" style={style}>
<UIButton
kind={Kind.PRIMARY}
size={Size.SMALL}
disabled={disabled}
onClick={handleClick}
>
{element.label}
</UIButton>
<ButtonTooltip help={element.help}>
<UIButton
kind={Kind.PRIMARY}
size={Size.SMALL}
disabled={disabled}
onClick={handleClick}
>
{element.label}
</UIButton>
</ButtonTooltip>
</div>
)
}
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/components/widgets/DateInput/DateInput.tsx
Expand Up @@ -24,6 +24,8 @@ import { DateInput as DateInputProto } from "autogen/proto"
import { WidgetStateManager, Source } from "lib/WidgetStateManager"
import { StyledWidgetLabel } from "components/widgets/BaseWidget"
import { Theme } from "theme"
import TooltipIcon from "components/shared/TooltipIcon"
import { Placement } from "components/shared/Tooltip"

export interface Props {
disabled: boolean
Expand Down Expand Up @@ -106,7 +108,17 @@ class DateInput extends React.PureComponent<Props, State> {

return (
<div className="stDateInput" style={style}>
<StyledWidgetLabel>{element.label}</StyledWidgetLabel>
<StyledWidgetLabel>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, this applies both to this PR and the other similar ones, but should we consider adding a helper function (or a small wrapper component) that generalizes this code more?

The code below seems replicated a good amount and could be made a single line in most places with a bit of refactoring (although it seems like there are a few places where things slightly differ).

{element.label}
{element.help && (
<div>
<TooltipIcon
content={element.help}
placement={Placement.BOTTOM_RIGHT}
/>
</div>
)}
</StyledWidgetLabel>
<UIDatePicker
formatString="yyyy/MM/dd"
disabled={disabled}
Expand Down
20 changes: 16 additions & 4 deletions frontend/src/components/widgets/Selectbox/Selectbox.tsx
Expand Up @@ -22,6 +22,8 @@ import { WidgetStateManager, Source } from "lib/WidgetStateManager"
import { logWarning } from "lib/log"
import VirtualDropdown from "components/shared/VirtualDropdown"
import { StyledWidgetLabel } from "components/widgets/BaseWidget"
import TooltipIcon from "components/shared/TooltipIcon"
import { Placement } from "components/shared/Tooltip"

export interface Props {
disabled: boolean
Expand Down Expand Up @@ -93,9 +95,9 @@ class Selectbox extends React.PureComponent<Props, State> {
}

public render = (): React.ReactNode => {
const style = { width: this.props.width }
let { options } = this.props.element
let { disabled } = this.props
const { width, element } = this.props
let { options } = element

const value = [
{
Expand All @@ -121,8 +123,18 @@ class Selectbox extends React.PureComponent<Props, State> {
)

return (
<div className="row-widget stSelectbox" style={style}>
<StyledWidgetLabel>{this.props.element.label}</StyledWidgetLabel>
<div className="row-widget stSelectbox" style={{ width }}>
<StyledWidgetLabel>
{element.label}
{element.help && (
<div>
<TooltipIcon
content={element.help}
placement={Placement.BOTTOM_RIGHT}
/>
</div>
)}
</StyledWidgetLabel>
<UISelect
clearable={false}
disabled={disabled}
Expand Down
14 changes: 13 additions & 1 deletion frontend/src/components/widgets/TimeInput/TimeInput.tsx
Expand Up @@ -20,6 +20,8 @@ import { TimeInput as TimeInputProto } from "autogen/proto"
import { TimePicker as UITimePicker } from "baseui/timepicker"
import { WidgetStateManager, Source } from "lib/WidgetStateManager"
import { StyledWidgetLabel } from "components/widgets/BaseWidget"
import TooltipIcon from "components/shared/TooltipIcon"
import { Placement } from "components/shared/Tooltip"

export interface Props {
disabled: boolean
Expand Down Expand Up @@ -100,7 +102,17 @@ class TimeInput extends PureComponent<Props, State> {

return (
<div className="stTimeInput" style={style}>
<StyledWidgetLabel>{element.label}</StyledWidgetLabel>
<StyledWidgetLabel>
{element.label}
{element.help && (
<div>
<TooltipIcon
content={element.help}
placement={Placement.BOTTOM_RIGHT}
/>
</div>
)}
</StyledWidgetLabel>
<UITimePicker
format="24"
value={this.stringToDate(this.state.value)}
Expand Down