Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/slots.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ Customize the rendered HTML if a selected option (inside the select control). Yo
</template>
```

## tag

**Type**: `slotProps: { option: Option, removeOption: () => void }`

Customize the rendered HTML for a tag representing a selected option in multi-select mode (when `isMulti` is true). You can use the slot props to retrieve the current selected option and a function to remove the option from the selection.

```vue
<template>
<VueSelect v-model="option" :options="options">
<template #tag="{ option }">
My value is: {{ option.value }}
<span @click="removeOption">X</span>
</template>
</VueSelect>
</template>
```

## menu-header

**Type**: `slotProps: {}`
Expand Down
24 changes: 23 additions & 1 deletion playground/Playground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ const userOptions: UserOption[] = [
:is-multi="true"
:is-loading="isLoading"
placeholder="Pick users"
/>
>
<template #tag="{ option }">
<span class="custom-tags">
{{ option.label }}
<span class="custom-x-mark" @click="removeOption">&times;</span>
</span>
</template>
</VueSelect>

<p class="selected-value">
Selected user value: {{ activeUsers || "none" }}
Expand Down Expand Up @@ -89,6 +96,21 @@ body {
font-weight: 500;
font-family: "IBM Plex Mono", monospace;
}

.custom-tags {
background-color: #e0f7fa;
padding: 5px 10px;
margin: 3px;
border-radius: 12px;
display: inline-flex;
align-items: center;
}

.custom-x-mark {
cursor: pointer;
margin-left: 8px;
color: #00796b;
}
}
}
</style>
28 changes: 21 additions & 7 deletions src/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -402,17 +402,25 @@ onBeforeUnmount(() => {
</slot>
</div>

<template v-if="props.isMulti && selectedOptions.length">
<button
<div
v-if="props.isMulti && selectedOptions.length"
class="multi-value-container"
>
<div
v-for="(option, i) in selectedOptions"
:key="i"
type="button"
class="multi-value"
@click="removeOption(option)"
>
{{ getMultiValueLabel(option) }}<XMarkIcon />
</button>
</template>
<slot
name="tag"
:option="option"
:remove-option="() => removeOption(option)"
>
<span class="multi-value">
{{ getMultiValueLabel(option) }}<XMarkIcon /></span>
</slot>
</div>
</div>

<input
:id="inputId"
Expand Down Expand Up @@ -636,6 +644,12 @@ onBeforeUnmount(() => {
z-index: 0;
}

.multi-value-container {
display: flex;
flex-wrap: wrap;
gap: var(--vs-multi-value-gap);
}

.multi-value {
appearance: none;
display: flex;
Expand Down