1
0
Fork 0
mirror of synced 2024-07-01 12:30:41 +12:00

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

View file

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