Make selected value of Slider optional (#115)

The Slider's value should be able to be deselected.
This commit is contained in:
Cecile Tonglet 2021-06-28 13:07:54 +01:00 committed by GitHub
parent 2e240fdb07
commit ec208f13e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View file

@ -6,7 +6,7 @@ pub struct Example {
props: ExampleProps, props: ExampleProps,
float: f64, float: f64,
integer: i32, integer: i32,
log_level: LogLevel, log_level: Option<LogLevel>,
link: ComponentLink<Self>, link: ComponentLink<Self>,
} }
@ -32,7 +32,7 @@ impl Component for Example {
props, props,
float: 1.2, float: 1.2,
integer: 30, integer: 30,
log_level: LogLevel::Info, log_level: None,
link, link,
} }
} }
@ -46,7 +46,7 @@ impl Component for Example {
self.integer = value; self.integer = value;
} }
Msg::LogLevelUpdate(value) => { Msg::LogLevelUpdate(value) => {
self.log_level = value; self.log_level = Some(value);
} }
Msg::Noop => {} Msg::Noop => {}
} }

View file

@ -28,7 +28,7 @@ pub struct SliderProps<T: Clone + PartialEq + 'static> {
pub value_label: Option<Cow<'static, str>>, pub value_label: Option<Cow<'static, str>>,
pub onchange: Callback<T>, pub onchange: Callback<T>,
pub values: Vec<(T, Option<Cow<'static, str>>)>, pub values: Vec<(T, Option<Cow<'static, str>>)>,
pub selected: T, pub selected: Option<T>,
} }
pub enum Msg { pub enum Msg {
@ -104,7 +104,7 @@ impl<T: Clone + PartialEq + 'static> Component for Slider<T> {
.get(position) .get(position)
.unwrap_or_else(|| self.props.values.last().expect("No value in the vec")); .unwrap_or_else(|| self.props.values.last().expect("No value in the vec"));
if *value != self.props.selected { if Some(value) != self.props.selected.as_ref() {
self.props.onchange.emit(value.clone()); self.props.onchange.emit(value.clone());
} }
@ -137,7 +137,7 @@ impl<T: Clone + PartialEq + 'static> Component for Slider<T> {
.props .props
.values .values
.iter() .iter()
.position(|(value, _)| *value == self.props.selected) .position(|(value, _)| Some(value) == self.props.selected.as_ref())
.map(|i| i.saturating_sub(1)) .map(|i| i.saturating_sub(1))
.unwrap_or(0); .unwrap_or(0);
let (value, _) = self.props.values[index].clone(); let (value, _) = self.props.values[index].clone();
@ -151,7 +151,7 @@ impl<T: Clone + PartialEq + 'static> Component for Slider<T> {
.props .props
.values .values
.iter() .iter()
.position(|(value, _)| *value == self.props.selected) .position(|(value, _)| Some(value) == self.props.selected.as_ref())
.map(|i| i.saturating_add(1)) .map(|i| i.saturating_add(1))
.unwrap_or(0); .unwrap_or(0);
let (value, _) = self let (value, _) = self
@ -188,7 +188,7 @@ impl<T: Clone + PartialEq + 'static> Component for Slider<T> {
.props .props
.values .values
.iter() .iter()
.position(|(value, _)| *value == self.props.selected); .position(|(value, _)| Some(value) == self.props.selected.as_ref());
let labels = if self.props.values.len() > 1 { let labels = if self.props.values.len() > 1 {
self.props self.props
.values .values