Skip to content

Commit

Permalink
Components as props in labels (#940)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnMarieW authored Feb 22, 2023
1 parent 397830d commit fd23bc0
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 14 deletions.
4 changes: 2 additions & 2 deletions dash_bootstrap_components/icons.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BOOTSTRAP = (
"https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/"
"https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/"
"font/bootstrap-icons.css"
)
FONT_AWESOME = "https://use.fontawesome.com/releases/v6.1.1/css/all.css"
FONT_AWESOME = "https://use.fontawesome.com/releases/v6.3.0/css/all.css"
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 @@
import dash_bootstrap_components as dbc
from dash import html

flights = html.Div([html.I(className="bi bi-airplane pe-1"), "Flights"])
car = html.Div([html.I(className="bi bi-car-front pe-1"), "Rental Car"])
hotel = html.Div([html.I(className="bi bi-house pe-1"), "Hotel"])

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 docs/templates/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
/>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css"
href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.3/font/bootstrap-icons.css"
/>
1 change: 1 addition & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dash_bootstrap_components",
"docs",
"examples",
"tests",
"noxfile.py",
"tasks.py",
]
Expand Down
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
43 changes: 43 additions & 0 deletions tests/test_components_as_props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import dash_bootstrap_components as dbc
from dash import Dash, html


def test_mdcap001_components_as_props(dash_duo):
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_duo.start_server(app)

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

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

dash_duo.wait_for_text_to_equal("#checkbox+label", "h4")
dash_duo.wait_for_text_to_equal("#radiobutton+label", "h6")
dash_duo.wait_for_text_to_equal("#switch+label", "h5")
5 changes: 2 additions & 3 deletions tests/test_navlink.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from dash import Dash
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from dash_bootstrap_components import NavLink
from dash import dcc, html
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def test_dbnl001_auto_active(dash_duo):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_popover.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import dash.testing.wait as wait
from dash import Dash, html
from dash_bootstrap_components import (
Popover,
PopoverBody,
PopoverHeader,
themes,
)
import dash.testing.wait as wait
from selenium.webdriver.common.action_chains import ActionChains


Expand Down

0 comments on commit fd23bc0

Please sign in to comment.