fix(core): fix form extension not handling default field type. Fix #804

This commit is contained in:
Federico Terzi 2021-10-21 21:39:47 +02:00
parent 7588bdb1cf
commit 869d8dbbaa

View File

@ -55,9 +55,43 @@ fn convert_fields(fields: &Params) -> HashMap<String, FormField> {
let mut form_field = None; let mut form_field = None;
if let Value::Object(params) = field { if let Value::Object(params) = field {
if let Some(Value::String(field_type)) = params.get("type") { form_field = match params.get("type") {
form_field = match field_type.as_str() { Some(Value::String(field_type)) if field_type == "choice" => Some(FormField::Choice {
"text" => Some(FormField::Text { default: params
.get("default")
.and_then(|val| val.as_string())
.cloned(),
values: params
.get("values")
.and_then(|val| val.as_array())
.map(|arr| {
arr
.iter()
.flat_map(|choice| choice.as_string())
.cloned()
.collect()
})
.unwrap_or_default(),
}),
Some(Value::String(field_type)) if field_type == "list" => Some(FormField::List {
default: params
.get("default")
.and_then(|val| val.as_string())
.cloned(),
values: params
.get("values")
.and_then(|val| val.as_array())
.map(|arr| {
arr
.iter()
.flat_map(|choice| choice.as_string())
.cloned()
.collect()
})
.unwrap_or_default(),
}),
// By default, it's considered type 'text'
_ => Some(FormField::Text {
default: params default: params
.get("default") .get("default")
.and_then(|val| val.as_string()) .and_then(|val| val.as_string())
@ -68,42 +102,6 @@ fn convert_fields(fields: &Params) -> HashMap<String, FormField> {
.cloned() .cloned()
.unwrap_or(false), .unwrap_or(false),
}), }),
"choice" => Some(FormField::Choice {
default: params
.get("default")
.and_then(|val| val.as_string())
.cloned(),
values: params
.get("values")
.and_then(|val| val.as_array())
.map(|arr| {
arr
.iter()
.flat_map(|choice| choice.as_string())
.cloned()
.collect()
})
.unwrap_or_default(),
}),
"list" => Some(FormField::List {
default: params
.get("default")
.and_then(|val| val.as_string())
.cloned(),
values: params
.get("values")
.and_then(|val| val.as_array())
.map(|arr| {
arr
.iter()
.flat_map(|choice| choice.as_string())
.cloned()
.collect()
})
.unwrap_or_default(),
}),
_ => None,
}
} }
} }