Add pretty labels to multiselect and fix stale props issue
This commit is contained in:
parent
087e8ed153
commit
5f697f0e96
|
@ -12,22 +12,22 @@
|
||||||
export let getOptionValue = option => option
|
export let getOptionValue = option => option
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
$: fieldText = getFieldText(value)
|
$: selectedLookupMap = getSelectedLookupMap(value)
|
||||||
$: valueLookupMap = getValueLookupMap(value)
|
$: optionLookupMap = getOptionLookupMap(options)
|
||||||
$: isOptionSelected = option => {
|
$: fieldText = getFieldText(value, optionLookupMap, placeholder)
|
||||||
return valueLookupMap[option] === true
|
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true
|
||||||
}
|
$: toggleOption = makeToggleOption(selectedLookupMap, value)
|
||||||
|
|
||||||
const getFieldText = value => {
|
const getFieldText = (value, map, placeholder) => {
|
||||||
if (value?.length) {
|
if (value?.length) {
|
||||||
const count = value?.length ?? 0
|
const vals = value.map(option => map[option] || "").join(", ")
|
||||||
return `${count} selected option${count === 1 ? "" : "s"}`
|
return `(${value.length}) ${vals}`
|
||||||
} else {
|
} else {
|
||||||
return placeholder || "Choose some options"
|
return placeholder || "Choose some options"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getValueLookupMap = value => {
|
const getSelectedLookupMap = value => {
|
||||||
let map = {}
|
let map = {}
|
||||||
if (value?.length) {
|
if (value?.length) {
|
||||||
value.forEach(option => {
|
value.forEach(option => {
|
||||||
|
@ -39,14 +39,29 @@
|
||||||
return map
|
return map
|
||||||
}
|
}
|
||||||
|
|
||||||
const toggleOption = optionValue => {
|
const getOptionLookupMap = options => {
|
||||||
if (valueLookupMap[optionValue]) {
|
let map = {}
|
||||||
|
if (options) {
|
||||||
|
options.forEach(option => {
|
||||||
|
const optionValue = getOptionValue(option)
|
||||||
|
if (optionValue != null) {
|
||||||
|
map[optionValue] = getOptionLabel(option) || ""
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return map
|
||||||
|
}
|
||||||
|
|
||||||
|
const makeToggleOption = (map, value) => {
|
||||||
|
return optionValue => {
|
||||||
|
if (map[optionValue]) {
|
||||||
const filtered = value.filter(option => option !== optionValue)
|
const filtered = value.filter(option => option !== optionValue)
|
||||||
dispatch("change", filtered)
|
dispatch("change", filtered)
|
||||||
} else {
|
} else {
|
||||||
dispatch("change", [...value, optionValue])
|
dispatch("change", [...value, optionValue])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Picker
|
<Picker
|
||||||
|
|
|
@ -106,4 +106,10 @@
|
||||||
.spectrum-Picker {
|
.spectrum-Picker {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
.spectrum-Picker-label {
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
width: 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
export { default as TextField } from "./TextField.svelte"
|
export { default as TextField } from "./TextField.svelte"
|
||||||
export { default as Select } from "./Select.svelte"
|
export { default as Select } from "./Select.svelte"
|
||||||
|
export { default as Multiselect } from "./Multiselect.svelte"
|
||||||
|
|
Loading…
Reference in New Issue