Enum patternfly_yew::Validator
source · pub enum Validator<T, S> {
None,
Custom(Rc<dyn Fn(ValidationContext<T>) -> S>),
}Variants§
None
Custom(Rc<dyn Fn(ValidationContext<T>) -> S>)
Implementations§
source§impl<T, S> Validator<T, S>
impl<T, S> Validator<T, S>
sourcepub fn is_custom(&self) -> bool
pub fn is_custom(&self) -> bool
Examples found in repository?
src/form/input.rs (line 135)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
TextInputMsg::Init => {
ctx.props().onvalidate.emit(ValidationContext {
value: self.value(ctx),
initial: true,
});
}
TextInputMsg::Changed(data) => {
self.value = Some(data.clone());
ctx.props().onchange.emit(data.clone());
ctx.props().onvalidate.emit(data.into());
}
TextInputMsg::Input(data) => {
ctx.props().oninput.emit(data);
if let Some(value) = self.extract_value() {
self.value = Some(value.clone());
ctx.props().onchange.emit(value.clone());
ctx.props().onvalidate.emit(value.into());
}
// only re-render if we have a validator
return ctx.props().validator.is_custom();
}
}
true
}sourcepub fn run<C>(&self, ctx: C) -> Option<S>where
C: Into<ValidationContext<T>>,
pub fn run<C>(&self, ctx: C) -> Option<S>where
C: Into<ValidationContext<T>>,
Convert into the context and run
Examples found in repository?
src/form/group.rs (line 228)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Self::Message::Validate(value) => {
let state = ctx.props().validator.run(value);
if self.state != state {
self.state = state;
ctx.props()
.onvalidated
.emit(self.state.clone().unwrap_or_default());
if let Some((validation_ctx, _)) = ctx
.link()
.context::<ValidationFormContext>(Callback::noop())
{
validation_ctx
.push_state(GroupValidationResult(self.id.clone(), self.state.clone()));
}
}
}
}
true
}More examples
src/pagination.rs (line 137)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
fn view(&self, ctx: &Context<Self>) -> Html {
// The pagination menu : "1-20 of nnn"
let mut menu_classes = Classes::from("pf-c-options-menu");
if self.expanded {
menu_classes.push("pf-m-expanded");
}
// The default rust div operator does floor(), we need ceil, so we cast to float before doing the operation
let max_page = ctx
.props()
.total_entries
.map(|m| (m as f64 / ctx.props().selected_choice as f64).ceil() as u32);
let current_page =
(ctx.props().offset as f64 / ctx.props().selected_choice as f64).ceil() as u32;
let is_last_page = if let Some(max) = ctx.props().total_entries {
ctx.props().offset + ctx.props().selected_choice > max
} else {
false
};
let total_entries = ctx
.props()
.total_entries
.map(|m| format!("{}", m))
.unwrap_or_else(|| String::from("unknown"));
// +1 because humans don't count from 0 :)
let showing = format!(
"{} - {}",
ctx.props().offset + 1,
ctx.props().offset + ctx.props().selected_choice
);
let limit_choices = ctx.props().entries_per_page_choices.clone();
let link = ctx.link().clone();
// todo also add max page
let page_number_field_validator = Validator::from(
|ctx: ValidationContext<String>| match ctx.value.parse::<u32>() {
Ok(value) => {
if value > 0 {
InputState::Default
} else {
InputState::Error
}
}
Err(_) => InputState::Error,
},
);
let validator_clone = page_number_field_validator.clone();
let onchange_callback = {
ctx.link().callback(move |input: String| {
match validator_clone.run(ValidationContext::from(input.clone())) {
Some(InputState::Default) => Msg::Page(input.parse::<u32>().unwrap()),
_ => Msg::Page(current_page + 1),
}
})
};
return html! {
<div class="pf-c-pagination" ref={self.global_close.clone()} >
// the selector of how many entries per page to display
<div class="pf-c-pagination__total-items">
<b>{ showing.clone() }</b> {"\u{00a0}of\u{00a0}"}
<b>{ total_entries.clone() }</b>
</div>
<div class={ menu_classes }>
<div class="pf-c-options-menu__toggle pf-m-text pf-m-plain">
<span class="pf-c-options-menu__toggle-text">
<b>{ showing }</b>{"\u{00a0}of\u{00a0}"}
<b>{ total_entries }</b>
</span>
<Button
class="pf-c-options-menu__toggle-button"
id="pagination-options-menu-top-toggle"
//aria-haspopup="listbox"
aria_label="Items per page"
onclick={ctx.link().callback(|_|Msg::ToggleMenu)}
>
<span class="pf-c-options-menu__toggle-button-icon">
{ Icon::CaretDown }
</span>
</Button>
</div>
{{ if self.expanded {
html! {
<ul class="pf-c-options-menu__menu" >
{ for limit_choices.into_iter().map(|limit| { html!{
<li>
<Button
class="pf-c-options-menu__menu-item"
onclick={link.callback(move |_|Msg::SetLimit(limit))}>
{limit} {" per page"}
{{ if ctx.props().selected_choice == limit {
html!{
<div class="pf-c-options-menu__menu-item-icon">
{ Icon::Check }
</div>
}
} else {
html!{}
} }}
</Button>
</li>
}})}
</ul>
}
} else { html! {} }
}}
</div>
// the navigation buttons
<nav class="pf-c-pagination__nav" aria-label="Pagination">
<div class="pf-c-pagination__nav-control pf-m-first">
<Button
variant={Variant::Plain}
onclick={ctx.link().callback(|_|Msg::First)}
disabled={ ctx.props().offset == 0 }
aria_label="Go to first page"
>
{ Icon::AngleDoubleLeft }
</Button>
</div>
<div class="pf-c-pagination__nav-control pf-m-prev">
<Button
aria_label="Go to previous page"
variant={Variant::Plain}
onclick={ctx.link().callback(|_|Msg::Previous)}
disabled= { ctx.props().offset == 0 }
>
{ Icon::AngleLeft }
</Button>
</div>
<div class="pf-c-pagination__nav-page-select">
<TextInput
r#type="number"
validator={page_number_field_validator}
onchange={onchange_callback}
value = {(current_page+1).to_string()}
/>
{{
if let Some(max_page) = max_page {
html!{<span aria-hidden="true">{ "of "} { max_page }</span>}
} else {
html!{}
}
}}
</div>
<div class="pf-c-pagination__nav-control pf-m-next">
<Button
aria_label="Go to next page"
variant={Variant::Plain}
onclick={ctx.link().callback(|_|Msg::Next)}
disabled={max_page.map_or_else(|| false, |m| current_page >= m)}
>
{ Icon::AngleRight }
</Button>
</div>
<div class="pf-c-pagination__nav-control pf-m-last">
<Button
aria_label="Go to last page"
variant={Variant::Plain}
onclick={ctx.link().callback(|_|Msg::Last)}
disabled={is_last_page}
>
{ Icon::AngleDoubleRight }
</Button>
</div>
</nav>
</div>
};
}sourcepub fn run_if<F>(&self, f: F) -> Option<S>where
F: FnOnce() -> ValidationContext<T>,
pub fn run_if<F>(&self, f: F) -> Option<S>where
F: FnOnce() -> ValidationContext<T>,
Only convert when necessary, and run.
Examples found in repository?
More examples
sourcepub fn run_ctx(&self, ctx: ValidationContext<T>) -> Option<S>
pub fn run_ctx(&self, ctx: ValidationContext<T>) -> Option<S>
Run with the provided context.
Trait Implementations§
Auto Trait Implementations§
impl<T, S> !RefUnwindSafe for Validator<T, S>
impl<T, S> !Send for Validator<T, S>
impl<T, S> !Sync for Validator<T, S>
impl<T, S> Unpin for Validator<T, S>
impl<T, S> !UnwindSafe for Validator<T, S>
Blanket Implementations§
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
Convert
self to a value of a Properties struct.source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
Convert
self to a value of a Properties struct.