From 018e2c4c1b8b9dcdc83d7d346b811f9d6ccf175b Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Tue, 10 May 2022 13:32:09 +0100 Subject: [PATCH] Allow status lights to be added to pickers --- packages/bbui/src/Form/Core/Picker.svelte | 32 +++++++++++---- packages/bbui/src/Form/Core/Select.svelte | 41 ++++++++----------- packages/bbui/src/Form/Select.svelte | 2 + .../bbui/src/StatusLight/StatusLight.svelte | 3 ++ 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/packages/bbui/src/Form/Core/Picker.svelte b/packages/bbui/src/Form/Core/Picker.svelte index 513bb35132..a3b5c147e5 100644 --- a/packages/bbui/src/Form/Core/Picker.svelte +++ b/packages/bbui/src/Form/Core/Picker.svelte @@ -7,12 +7,14 @@ import clickOutside from "../../Actions/click_outside" import Search from "./Search.svelte" import Icon from "../../Icon/Icon.svelte" + import StatusLight from "../../StatusLight/StatusLight.svelte" export let id = null export let disabled = false export let error = null export let fieldText = "" export let fieldIcon = "" + export let fieldColour = "" export let isPlaceholder = false export let placeholderOption = null export let options = [] @@ -21,6 +23,7 @@ export let getOptionLabel = option => option export let getOptionValue = option => option export let getOptionIcon = () => null + export let getOptionColour = () => null export let open = false export let readonly = false export let quiet = false @@ -83,11 +86,10 @@ on:mousedown={onClick} > {#if fieldIcon} - + {/if} - {/if} + {#if fieldColour} + + + + {/if} onSelectOption(getOptionValue(option, idx))} > {#if getOptionIcon(option, idx)} - + {/if} @@ -173,6 +180,11 @@ > + {#if getOptionColour(option, idx)} + + + + {/if} {/each} {/if} @@ -208,12 +220,18 @@ padding-right: 2px; } - .icon-Padding { - padding-right: 10px; + /* Icon and colour alignment */ + .spectrum-Menu-checkmark { + align-self: center; + margin-top: 0; } - .icon-Placeholder-Padding { - padding-right: 10px; + .option-colour { + padding-left: 8px; } + .option-icon { + padding-right: 8px; + } + .spectrum-Popover :global(.spectrum-Search) { margin-top: -1px; margin-left: -1px; diff --git a/packages/bbui/src/Form/Core/Select.svelte b/packages/bbui/src/Form/Core/Select.svelte index 413b11dd34..81d7ec8e6c 100644 --- a/packages/bbui/src/Form/Core/Select.svelte +++ b/packages/bbui/src/Form/Core/Select.svelte @@ -11,6 +11,7 @@ export let getOptionLabel = option => option export let getOptionValue = option => option export let getOptionIcon = () => null + export let getOptionColour = () => null export let readonly = false export let quiet = false export let autoWidth = false @@ -20,7 +21,19 @@ const dispatch = createEventDispatcher() let open = false $: fieldText = getFieldText(value, options, placeholder) - $: fieldIcon = getFieldIcon(value, options, placeholder) + $: fieldIcon = getFieldAttribute(getOptionIcon, value, options) + $: fieldColour = getFieldAttribute(getOptionColour, value, options) + + const getFieldAttribute = (getAttribute, value, options) => { + // Wait for options to load if there is a value but no options + if (!options?.length) { + return "" + } + const index = options.findIndex( + (option, idx) => getOptionValue(option, idx) === value + ) + return index !== -1 ? getAttribute(options[index], index) : null + } const getFieldText = (value, options, placeholder) => { // Always use placeholder if no value @@ -28,27 +41,7 @@ return placeholder || "Choose an option" } - // Wait for options to load if there is a value but no options - if (!options?.length) { - return "" - } - - // Render the label if the selected option is found, otherwise raw value - const index = options.findIndex( - (option, idx) => getOptionValue(option, idx) === value - ) - return index !== -1 ? getOptionLabel(options[index], index) : value - } - - const getFieldIcon = (value, options) => { - // Wait for options to load if there is a value but no options - if (!options?.length) { - return "" - } - const index = options.findIndex( - (option, idx) => getOptionValue(option, idx) === value - ) - return index !== -1 ? getOptionIcon(options[index], index) : null + return getFieldAttribute(getOptionLabel, value, options) } const selectOption = value => { @@ -66,12 +59,14 @@ {disabled} {readonly} {fieldText} + {fieldIcon} + {fieldColour} {options} {autoWidth} {getOptionLabel} {getOptionValue} {getOptionIcon} - {fieldIcon} + {getOptionColour} {autocomplete} {sort} isPlaceholder={value == null || value === ""} diff --git a/packages/bbui/src/Form/Select.svelte b/packages/bbui/src/Form/Select.svelte index 0df27e2ff0..1b68746c5e 100644 --- a/packages/bbui/src/Form/Select.svelte +++ b/packages/bbui/src/Form/Select.svelte @@ -14,6 +14,7 @@ export let getOptionLabel = option => extractProperty(option, "label") export let getOptionValue = option => extractProperty(option, "value") export let getOptionIcon = option => option?.icon + export let getOptionColour = option => option?.colour export let quiet = false export let autoWidth = false export let sort = false @@ -47,6 +48,7 @@ {getOptionLabel} {getOptionValue} {getOptionIcon} + {getOptionColour} on:change={onChange} on:click /> diff --git a/packages/bbui/src/StatusLight/StatusLight.svelte b/packages/bbui/src/StatusLight/StatusLight.svelte index e02cfb222e..a0c72443a6 100644 --- a/packages/bbui/src/StatusLight/StatusLight.svelte +++ b/packages/bbui/src/StatusLight/StatusLight.svelte @@ -51,6 +51,9 @@ justify-content: center; align-items: center; --spectrum-statuslight-info-text-gap: 4px; + min-height: 0; + padding-top: 0; + padding-bottom: 0; } .spectrum-StatusLight.withText::before { margin-right: 10px;