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 class, id and placeholder properties to Select #1187

Merged
merged 3 commits into from
May 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions yew-components/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ pub struct Props<T: Clone> {
/// Options are available to choose.
#[prop_or_default]
pub options: Vec<T>,
/// Classes applied to the `<select>` tag
#[prop_or_default]
pub class: String,
/// ID for the `<select>` tag
#[prop_or_default]
pub id: String,
/// Placeholder value, shown at the top as a disabled option
#[prop_or(String::from("↪"))]
pub placeholder: String,
/// Callback to handle changes.
pub on_change: Callback<T>,
}
Expand Down Expand Up @@ -124,9 +133,15 @@ where
};

html! {
<select ref=self.select_ref.clone() disabled=self.props.disabled onchange=self.on_change()>
<select
ref=self.select_ref.clone()
id=self.props.id.clone()
class=self.props.class.clone()
disabled=self.props.disabled
onchange=self.on_change()
>
<option value="" disabled=true selected=selected.is_none()>
{ "↪" }
{ self.props.placeholder.clone() }
</option>
{ for self.props.options.iter().map(view_option) }
</select>
Expand Down Expand Up @@ -160,4 +175,28 @@ mod tests {
<Select<u8> on_change=on_change />
};
}

#[test]
fn can_create_select_with_class() {
let on_change = Callback::<u8>::default();
html! {
<Select<u8> on_change=on_change class="form-control" />
};
}

#[test]
fn can_create_select_with_id() {
let on_change = Callback::<u8>::default();
html! {
<Select<u8> on_change=on_change id="test-select" />
};
}

#[test]
fn can_create_select_with_placeholder() {
let on_change = Callback::<u8>::default();
html! {
<Select<u8> on_change=on_change placeholder="--Please choose an option--" />
};
}
}