feat: adds Label component (#266)

This commit is contained in:
luoxiaozero 2024-09-20 00:13:40 +08:00 committed by GitHub
parent 282fcd038c
commit 687350102a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 199 additions and 20 deletions

View file

@ -84,6 +84,7 @@ fn TheRouter() -> impl IntoView {
<Route path=path!("/icon") view=IconMdPage/> <Route path=path!("/icon") view=IconMdPage/>
<Route path=path!("/image") view=ImageMdPage/> <Route path=path!("/image") view=ImageMdPage/>
<Route path=path!("/input") view=InputMdPage/> <Route path=path!("/input") view=InputMdPage/>
<Route path=path!("/label") view=LabelMdPage/>
<Route path=path!("/layout") view=LayoutMdPage/> <Route path=path!("/layout") view=LayoutMdPage/>
<Route path=path!("/link") view=LinkMdPage/> <Route path=path!("/link") view=LinkMdPage/>
<Route path=path!("/loading-bar") view=LoadingBarMdPage/> <Route path=path!("/loading-bar") view=LoadingBarMdPage/>

View file

@ -297,6 +297,11 @@ pub(crate) fn gen_nav_data() -> Vec<NavGroupOption> {
value: "/components/input", value: "/components/input",
label: "Input", label: "Input",
}, },
NavItemOption {
group: None,
value: "/components/label",
label: "Label",
},
NavItemOption { NavItemOption {
group: None, group: None,
value: "/components/layout", value: "/components/layout",

View file

@ -8,7 +8,9 @@ cargo add thaw --features=csr
<MessageBar intent=MessageBarIntent::Warning> <MessageBar intent=MessageBarIntent::Warning>
<MessageBarBody> <MessageBarBody>
"If you are using the nightly feature in Leptos, please enable Thaw's nightly as well." <div style="white-space: normal">
"If you are using the nightly feature in Leptos, please enable Thaw's nightly as well."
</div>
</MessageBarBody> </MessageBarBody>
</MessageBar> </MessageBar>

View file

@ -47,6 +47,7 @@ pub fn include_md(_token_stream: proc_macro::TokenStream) -> proc_macro::TokenSt
"IconMdPage" => "../../thaw/src/icon/docs/mod.md", "IconMdPage" => "../../thaw/src/icon/docs/mod.md",
"ImageMdPage" => "../../thaw/src/image/docs/mod.md", "ImageMdPage" => "../../thaw/src/image/docs/mod.md",
"InputMdPage" => "../../thaw/src/input/docs/mod.md", "InputMdPage" => "../../thaw/src/input/docs/mod.md",
"LabelMdPage" => "../../thaw/src/label/docs/mod.md",
"LayoutMdPage" => "../../thaw/src/layout/docs/mod.md", "LayoutMdPage" => "../../thaw/src/layout/docs/mod.md",
"LinkMdPage" => "../../thaw/src/link/docs/mod.md", "LinkMdPage" => "../../thaw/src/link/docs/mod.md",
"LoadingBarMdPage" => "../../thaw/src/loading_bar/docs/mod.md", "LoadingBarMdPage" => "../../thaw/src/loading_bar/docs/mod.md",

View file

@ -58,7 +58,7 @@ impl DrawerSize {
#[derive(Debug, Default, PartialEq)] #[derive(Debug, Default, PartialEq)]
pub enum DrawerModalType { pub enum DrawerModalType {
/// When this type of dialog is open, /// When this type of dialog is open,
/// the rest of the page is dimmed out and cannot be interacted with. /// the rest of the page is dimmed out and cannot be interacted with.
#[default] #[default]
Modal, Modal,
/// When a non-modal dialog is open, /// When a non-modal dialog is open,

View file

@ -154,7 +154,7 @@ view! {
| label | `MaybeProp<String>` | `Default::default()` | The label associated with the field. | | label | `MaybeProp<String>` | `Default::default()` | The label associated with the field. |
| name | `MaybeProp<String>` | `Default::default()` | A string specifying a name for the input control. This name is submitted along with the control's value when the form data is submitted. | | name | `MaybeProp<String>` | `Default::default()` | A string specifying a name for the input control. This name is submitted along with the control's value when the form data is submitted. |
| orientation | `MaybeSignal<FieldOrientation>` | `FieldOrientation::Vertical` | The orientation of the label relative to the field component. | | orientation | `MaybeSignal<FieldOrientation>` | `FieldOrientation::Vertical` | The orientation of the label relative to the field component. |
| reqired | `MaybeSignal<bool>` | `false` | If set to true this field will be marked as required. | | required | `MaybeSignal<bool>` | `false` | If set to true this field will be marked as required. |
| children | `Children` | | | | children | `Children` | | |
### FieldContextProvider Props ### FieldContextProvider Props

View file

@ -6,15 +6,6 @@
margin-bottom: var(--spacingVerticalXXS); margin-bottom: var(--spacingVerticalXXS);
padding-bottom: var(--spacingVerticalXXS); padding-bottom: var(--spacingVerticalXXS);
padding-top: var(--spacingVerticalXXS); padding-top: var(--spacingVerticalXXS);
line-height: var(--lineHeightBase300);
font-size: var(--fontSizeBase300);
font-family: var(--fontFamilyBase);
color: var(--colorNeutralForeground1);
}
.thaw-field__label--required {
padding-left: var(--spacingHorizontalXS);
color: var(--colorPaletteRedForeground3);
} }
.thaw-field--horizontal { .thaw-field--horizontal {

View file

@ -1,5 +1,6 @@
use crate::Label;
use leptos::{context::Provider, either::EitherOf3, prelude::*}; use leptos::{context::Provider, either::EitherOf3, prelude::*};
use thaw_components::{OptionComp, If, Then}; use thaw_components::OptionComp;
use thaw_utils::{class_list, mount_style}; use thaw_utils::{class_list, mount_style};
use uuid::Uuid; use uuid::Uuid;
@ -47,14 +48,13 @@ pub fn Field(
move || { move || {
view! { view! {
<OptionComp value=label.get() let:label> <OptionComp value=label.get() let:label>
<label class="thaw-field__label" for=id.get_value()> <Label
class="thaw-field__label"
required=required
attr:r#for=id.get_value()
>
{label} {label}
<If cond=required> </Label>
<Then slot>
<span class="thaw-field__label--required">*</span>
</Then>
</If>
</label>
</OptionComp> </OptionComp>
} }
} }

View file

@ -0,0 +1,57 @@
# Label
```rust demo
view! {
<Label>"This is a label"</Label>
}
```
### Size
```rust demo
view! {
<Flex>
<Label size=LabelSize::Small>"Small"</Label>
<Label>"Medium"</Label>
<Label size=LabelSize::Large>"Large"</Label>
</Flex>
}
```
### Weight
```rust demo
view! {
<Flex>
<Label>"Label"</Label>
<Label weight=LabelWeight::Semibold>"Strong label"</Label>
</Flex>
}
```
### Disabled
```rust demo
view! {
<Label required=true disabled=true>"Required label"</Label>
}
```
### Required
```rust demo
view! {
<Label required=true>"Required label"</Label>
}
```
### Label Props
| Name | Type | Default | Desciption |
| --- | --- | --- | --- |
| class | `MaybeProp<String>` | `Default::default()` | |
| size | `MaybeSignal<LabelSize>` | `LabelSize::Medium` | A label supports different sizes. |
| weight | `MaybeSignal<LabelWeight>` | `LabelWeight::Regular` | A label supports regular and semibold fontweight. |
| disabled | `MaybeSignal<bool>` | `false` | Renders the label as disabled. |
| required | `MaybeSignal<bool>` | `false` | Displays an indicator that the label is for a required field. |
| children | `Children` | | |

34
thaw/src/label/label.css Normal file
View file

@ -0,0 +1,34 @@
.thaw-label {
line-height: var(--lineHeightBase300);
font-size: var(--fontSizeBase300);
font-family: var(--fontFamilyBase);
color: var(--colorNeutralForeground1);
}
.thaw-label__required {
padding-left: var(--spacingHorizontalXS);
color: var(--colorPaletteRedForeground3);
}
.thaw-label--disabled {
color: var(--colorNeutralForegroundDisabled);
}
.thaw-label--disabled .thaw-label__required {
color: var(--colorNeutralForegroundDisabled);
}
.thaw-label--small {
font-size: var(--fontSizeBase200);
line-height: var(--lineHeightBase200);
}
.thaw-label--large {
font-weight: var(--fontWeightSemibold);
font-size: var(--fontSizeBase400);
line-height: var(--lineHeightBase400);
}
.thaw-label--semibold {
font-weight: var(--fontWeightSemibold);
}

75
thaw/src/label/label.rs Normal file
View file

@ -0,0 +1,75 @@
use leptos::prelude::*;
use thaw_components::{If, Then};
use thaw_utils::{class_list, mount_style};
#[component]
pub fn Label(
#[prop(optional, into)] class: MaybeProp<String>,
/// A label supports different sizes.
#[prop(optional, into)]
size: MaybeSignal<LabelSize>,
/// A label supports regular and semibold fontweight.
#[prop(optional, into)]
weight: MaybeSignal<LabelWeight>,
/// Displays an indicator that the label is for a required field.
#[prop(optional, into)]
required: MaybeSignal<bool>,
/// Renders the label as disabled.
#[prop(optional, into)]
disabled: MaybeSignal<bool>,
children: Children,
) -> impl IntoView {
mount_style("label", include_str!("./label.css"));
view! {
<label class=class_list![
"thaw-label",
("thaw-label--disabled", move || disabled.get()),
move || format!("thaw-label--{}", size.get().as_str()),
move || format!("thaw-label--{}", weight.get().as_str()),
class
]>
{children()} <If cond=required>
<Then slot>
<span aria-hidden="true" class="thaw-label__required">
"*"
</span>
</Then>
</If>
</label>
}
}
#[derive(Debug, Default, Clone)]
pub enum LabelSize {
Small,
#[default]
Medium,
Large,
}
impl LabelSize {
pub fn as_str(&self) -> &'static str {
match self {
Self::Small => "small",
Self::Medium => "medium",
Self::Large => "large",
}
}
}
#[derive(Debug, Default, Clone)]
pub enum LabelWeight {
#[default]
Regular,
Semibold,
}
impl LabelWeight {
pub fn as_str(&self) -> &'static str {
match self {
Self::Regular => "regular",
Self::Semibold => "semibold",
}
}
}

3
thaw/src/label/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod label;
pub use label::*;

View file

@ -24,6 +24,7 @@ mod grid;
mod icon; mod icon;
mod image; mod image;
mod input; mod input;
mod label;
mod layout; mod layout;
mod link; mod link;
mod loading_bar; mod loading_bar;
@ -79,6 +80,7 @@ pub use grid::*;
pub use icon::*; pub use icon::*;
pub use image::*; pub use image::*;
pub use input::*; pub use input::*;
pub use label::*;
pub use layout::*; pub use layout::*;
pub use link::*; pub use link::*;
pub use loading_bar::*; pub use loading_bar::*;

View file

@ -1,5 +1,13 @@
# Space # Space
<MessageBar intent=MessageBarIntent::Warning>
<MessageBarBody>
<div style="white-space: normal">
"The Space component may be removed in future versions. It is recommended to use the Flex component."
</div>
</MessageBarBody>
</MessageBar>
```rust demo ```rust demo
view! { view! {
<Space> <Space>