From 947f6ca92394bcc77adcd273860cc90c81fd177f Mon Sep 17 00:00:00 2001 From: Gerard Date: Mon, 14 Oct 2024 12:20:38 +0200 Subject: [PATCH] Update select to support multiple --- .../src/components/BccSelect/BccSelect.css | 8 +++ .../components/BccSelect/BccSelect.stories.ts | 52 ++++++++++++++++++- .../src/components/BccSelect/BccSelect.vue | 10 ++-- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/design-library/src/components/BccSelect/BccSelect.css b/design-library/src/components/BccSelect/BccSelect.css index 2ade6f44..d256758d 100644 --- a/design-library/src/components/BccSelect/BccSelect.css +++ b/design-library/src/components/BccSelect/BccSelect.css @@ -19,4 +19,12 @@ .bcc-select-success { @apply border-success focus:outline-success; } + + .bcc-select[multiple] { + background-image: none; + @apply h-auto max-h-full relative p-0 overflow-y-auto; + } + .bcc-select[multiple]>option { + @apply py-2 px-4; + } } diff --git a/design-library/src/components/BccSelect/BccSelect.stories.ts b/design-library/src/components/BccSelect/BccSelect.stories.ts index 30655ffd..29d15244 100644 --- a/design-library/src/components/BccSelect/BccSelect.stories.ts +++ b/design-library/src/components/BccSelect/BccSelect.stories.ts @@ -1,3 +1,4 @@ +import { ref } from "vue"; import BccSelect from "./BccSelect.vue"; import type { Meta, StoryFn } from "@storybook/vue3"; @@ -21,19 +22,24 @@ export default { showOptionalLabel: { description: "Will only take effect when `required` is `false`", }, + multiple: { + control: { type: "boolean", default: false }, + }, }, } as Meta; const Template: StoryFn = (args) => ({ components: { BccSelect }, setup() { - return { args }; + const selected = ref([]); + return { args, selected }; }, template: ` - + + `, }); @@ -64,12 +70,38 @@ Example.parameters = { + `, }, }, }; +/** + * Set the `multiple` prop to allow multiple options to be selected. + */ +export const Multiple: StoryFn = () => ({ + components: { BccSelect }, + setup() { + const selected = ref([]); + return { selected }; + }, + template: ` +
+ + + + + + + +
+

Selected:

+
{{ selected }}
+
+ `, +}); + /** * Set the `state` prop to control how the select is rendered. Set the `disabled` prop to disable the select */ @@ -80,26 +112,32 @@ export const State: StoryFn = () => ({ + + + + + + `, @@ -116,6 +154,7 @@ export const Placeholder: StoryFn = () => ({ + `, @@ -131,14 +170,17 @@ export const Size: StoryFn = () => ({ + + + `, @@ -154,10 +196,12 @@ export const WithLabel: StoryFn = () => ({ + + `, @@ -173,21 +217,25 @@ export const Optional: StoryFn = () => ({ + + + + `, diff --git a/design-library/src/components/BccSelect/BccSelect.vue b/design-library/src/components/BccSelect/BccSelect.vue index 2217743a..0ebc9767 100644 --- a/design-library/src/components/BccSelect/BccSelect.vue +++ b/design-library/src/components/BccSelect/BccSelect.vue @@ -12,7 +12,6 @@ import BccFormLabel from "@/components/BccFormLabel/BccFormLabel.vue"; import BccFormMessage from "@/components/BccFormMessage/BccFormMessage.vue"; type Props = { - modelValue?: string; state?: "default" | "error" | "success"; size?: "sm" | "base" | "lg"; disabled?: boolean; @@ -20,6 +19,7 @@ type Props = { showOptionalLabel?: boolean; optionalLabel?: string; required?: boolean; + multiple?: boolean; }; const props = withDefaults(defineProps(), { @@ -28,12 +28,12 @@ const props = withDefaults(defineProps(), { disabled: false, required: false, showOptionalLabel: false, + multiple: false, optionalLabel: "Optional", }); const id = `bcc-input-${useId()}`; - -const emit = defineEmits(["update:modelValue"]); +const modelValue = defineModel({ default: null }); const showOptionalLabel = computed(() => props.showOptionalLabel && !props.required); @@ -60,10 +60,10 @@ const { attrsWithoutStyles } = useAttrsWithoutStyles(); 'bcc-select-lg': size === 'lg', }" :id="id" - :value="modelValue" :disabled="disabled" :required="required" - @input="emit('update:modelValue', ($event.target as HTMLSelectElement).value)" + :multiple="multiple" + v-model="modelValue" v-bind="attrsWithoutStyles" >