Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Components as props in labels #940

Merged
merged 13 commits into from
Feb 22, 2023
7 changes: 7 additions & 0 deletions docs/components_page/components/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ If you need more granular control over checkboxes and radio buttons, you can als

{{example:components/input/radio_check_standalone.py:standalone_radio_check}}

## Components in labels

You can include components in labels for `Checklist`, `RadioItems`, `Checkbox`, `RadioButton`, and `Switch`.

{{example:components/input/components_in_labels.py:components_in_labels}}


## Color picker

When using `Input` with `type="color"`, the user may specify a color, either by using a visual color picker or by entering the color in a text field in #rrggbb format.
Expand Down
31 changes: 31 additions & 0 deletions docs/components_page/components/input/components_in_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from dash import Dash, html
import dash_bootstrap_components as dbc

flights = html.Div([html.Div(className="fa fa-plane pe-1"), "Flights"])
car = html.Div([html.Div(className="fa fa-car pe-1"), "Rental Car"])
hotel = html.Div([html.Div(className="fa fa-hotel pe-1"), "Hotel"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

These icons aren't showing up for me when I run the docs because Font Awesome stylesheet isn't actually linked.

We can add the link in docs/templates/partials/head.html, though I would actually have a preference for changing this example to use Bootstrap icons, since we're already linking the CSS anyway. So less for the reader of the docs to load when they visit.


radioitems = html.Div(
[
dbc.Label("Booking"),
dbc.RadioItems(
options=[
{"label": flights, "value": 1},
{"label": car, "value": 2},
{"label": hotel, "value": 3},
],
value=1,
id="radioitems-input",
),
]
)

checkbox = dbc.Checkbox(
id="standalone-checkbox",
label=html.Div(
["I agree to the ", html.A("Terms and Conditions", href="#")]
),
value=False,
)

components_in_labels = html.Div([radioitems, html.Hr(), checkbox])
2 changes: 1 addition & 1 deletion src/components/input/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Checkbox.propTypes = {
/**
* The label of the <input> element
*/
label: PropTypes.string,
label: PropTypes.node,

/**
* The id of the label
Expand Down
3 changes: 1 addition & 2 deletions src/components/input/Checklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ Checklist.propTypes = {
/**
* The checkbox's label
*/
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
label: PropTypes.node.isRequired,

/**
* The value of the checkbox. This value corresponds to the items
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/RadioButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ RadioButton.propTypes = {
/**
* The label of the <input> element
*/
label: PropTypes.string,
label: PropTypes.node,

/**
* The id of the label
Expand Down
3 changes: 1 addition & 2 deletions src/components/input/RadioItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ RadioItems.propTypes = {
/**
* The radio item's label
*/
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
label: PropTypes.node.isRequired,

/**
* The value of the radio item. This value corresponds to the items
Expand Down
2 changes: 1 addition & 1 deletion src/components/input/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Switch.propTypes = {
/**
* The label of the <input> element
*/
label: PropTypes.string,
label: PropTypes.node,

/**
* The id of the label
Expand Down
40 changes: 40 additions & 0 deletions tests/test_components_as_props
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from dash import Dash, dcc, html
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can also drop dcc import

from dash_bootstrap_components import Checklist, Checkbox, RadioButton, RadioItems, Switch


def test_mdcap001_components_as_props(dash_dcc):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think dash_dcc is a custom fixture in the dash repo, but swapping this out for dash_duo worked for me when I tested locally.

app = Dash(__name__)

app.layout = html.Div(
[
dbc.Checklist(
[
{"label": html.H2("H2 label"), "value": "h2"},
{"label": html.A("Link in checklist", href="#"), "value": "a"},
],
id="checklist",
),
dbc.RadioItems(
[
{"label": html.H3("on"), "value": "on"},
{"label": html.P("off"), "value": "off"},
],
id="radio-items",
),
dbc.Checkbox(label= html.H4("h4"), value= "h4", id="checkbox"),
dbc.RadioButton(label=html.H6("h6"), value="h6", id="radiobutton),
dbc.Switch(label=html.H5("h5"), value="h5", id="switch)
]
)

dash_dcc.start_server(app)

dash_dcc.wait_for_text_to_equal("#checklist h2", "H2 label")
dash_dcc.wait_for_text_to_equal("#checklist a", "Link in checklist")

dash_dcc.wait_for_text_to_equal("#radio-items h3", "on")
dash_dcc.wait_for_text_to_equal("#radio-items p", "off")

dash_dcc.wait_for_text_to_equal("#checkbox h4", "h4")
dash_dcc.wait_for_text_to_equal("#radiobutton h6", "h6")
dash_dcc.wait_for_text_to_equal(#switch h5", "h5")