Extract value and label props from select options which are objects by default

This commit is contained in:
Andrew Kingston 2021-04-16 11:04:50 +01:00
parent 3aeb85f62f
commit a9f07045f7
1 changed files with 8 additions and 2 deletions

View File

@ -10,14 +10,20 @@
export let error = null export let error = null
export let placeholder = "Choose an option" export let placeholder = "Choose an option"
export let options = [] export let options = []
export let getOptionLabel = option => option export let getOptionLabel = option => extractProperty(option, "label")
export let getOptionValue = option => option export let getOptionValue = option => extractProperty(option, "value")
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const onChange = e => { const onChange = e => {
dispatch("change", e.detail) dispatch("change", e.detail)
value = e.detail value = e.detail
} }
const extractProperty = (value, property) => {
if (value && typeof value === "object") {
return value[property]
}
return value
}
</script> </script>
<Field {label} {labelPosition} {disabled} {error}> <Field {label} {labelPosition} {disabled} {error}>