use super::*;
#[derive(Debug, Clone, PartialEq)]
pub enum CardSelectableActionsVariant {
Click {
onclick: Option<Callback<MouseEvent>>,
},
SingleSelect { onchange: Option<Callback<()>> },
MultiSelect {
onchange: Callback<CheckboxState>,
checked: CheckboxState,
},
}
#[derive(Debug, Clone, PartialEq, Properties)]
pub struct CardSelectableActionsObjectProperties {
pub action: CardSelectableActionsVariant,
#[prop_or_default]
pub base: CardSelectableActionsObjectBase,
}
#[derive(Debug, Clone, PartialEq, Properties, Default)]
pub struct CardSelectableActionsObjectBase {
#[prop_or_default]
pub has_no_offset: bool,
#[prop_or_default]
pub class: Classes,
#[prop_or_default]
pub id: Option<AttrValue>,
#[prop_or_default]
pub name: Option<AttrValue>,
}
#[function_component(CardSelectableActionsObject)]
pub fn selectable_actions_object(props: &CardSelectableActionsObjectProperties) -> Html {
type Variant = CardSelectableActionsVariant;
match &props.action {
Variant::SingleSelect { onchange } => html! {
<SingleSelectActionRadio base={props.base.clone()} onchange={onchange.clone()} />
},
Variant::MultiSelect { onchange, checked } => html! {
<MultiSelectActionCheckbox base={props.base.clone()} onchange={onchange.clone()} checked={*checked} />
},
Variant::Click { onclick } => html! {
<ClickableInput base={props.base.clone()} onclick={onclick.clone()} />
},
}
}
#[derive(Debug, Clone, PartialEq, Properties)]
struct SingleSelectActionRadioProperties {
base: CardSelectableActionsObjectBase,
onchange: Option<Callback<()>>,
}
struct CommonProps {
id: Option<String>,
name: Option<AttrValue>,
disabled: bool,
base_class: &'static str,
}
fn get_common_props(base: &CardSelectableActionsObjectBase, context: &CardContext) -> CommonProps {
CommonProps {
base_class: "pf-m-standalone",
id: base.id.as_ref().map(|s| s.to_string()),
name: base.name.as_ref().cloned(),
disabled: context.disabled,
}
}
#[function_component(SingleSelectActionRadio)]
fn single_select_action_radio(props: &SingleSelectActionRadioProperties) -> Html {
let context: CardContext = use_context().expect("Couldn't find card context");
let onchange = {
let onchange = props.onchange.clone();
Callback::from(move |_| {
if let Some(f) = onchange.clone() {
f.emit(())
}
})
};
let common = get_common_props(&props.base, &context);
html! {
<Radio
class={common.base_class}
id={common.id}
name={common.name}
disabled={common.disabled}
{onchange}
force_label=true
/>
}
}
#[derive(Debug, Clone, PartialEq, Properties)]
struct MultiSelectActionCheckboxProperties {
base: CardSelectableActionsObjectBase,
onchange: Callback<CheckboxState>,
checked: CheckboxState,
}
#[function_component(MultiSelectActionCheckbox)]
fn multi_select_action_checkbox(props: &MultiSelectActionCheckboxProperties) -> Html {
let context: CardContext = use_context().expect("Couldn't find card context");
let common = get_common_props(&props.base, &context);
html! {
<Checkbox
class={common.base_class}
id={common.id}
name={common.name}
disabled={common.disabled}
onchange={props.onchange.clone()}
checked={props.checked}
label={html!()}
/>
}
}
#[derive(Debug, Clone, PartialEq, Properties)]
struct ClickableInputProperties {
base: CardSelectableActionsObjectBase,
onclick: Option<Callback<MouseEvent>>,
}
#[function_component(ClickableInput)]
fn clickable_input_action_radio(props: &ClickableInputProperties) -> Html {
let context: CardContext = use_context().expect("Couldn't find card context");
if context.selectable {
log::warn!("Using a click action for an entire tile in a selectable card doesn't work. Set `selectable` to `false` in card `{}`", context.card_id);
}
let common = get_common_props(&props.base, &context);
html! {
<Radio
class={common.base_class}
id={common.id}
name={common.name}
disabled={common.disabled}
input_class="pf-v5-screen-reader"
input_onclick={props.onclick.clone()}
force_label=true
/>
}
}