Extract value and label props from select options which are objects by default
This commit is contained in:
parent
3aeb85f62f
commit
a9f07045f7
|
@ -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}>
|
||||||
|
|
Loading…
Reference in New Issue