nb-select
This is a select component for Vue.js 3+.
Loading component...
Installation
yarn add @vlalg-nimbus/nb-selects
Usage
import { createApp } from 'vue'
import App from './App.vue'
import NbSelectComponents from '@vlalg-nimbus/nb-selects'
import "@vlalg-nimbus/nb-selects/dist/style.css";
const app = createApp(App)
app.use(NbSelectComponents)
app.mount('#app')
To use, simply call the component, in this case it will be NbSelect or nb-select.
<template>
<NbSelect />
</template>
Preview & Playground
Select the component you want to edit/test
Props
Items with an (*) mean they are required
| name | Value type | Default | Description |
|---|---|---|---|
| nbId (*) | String | Sets the id attribute to differentiate from other components | |
| selectName | String | 'selectX' | Sets the name attribute for the select element |
| display | String | 'b' | Defines the display type. Accepts ib and b. |
| theme | String | 'light' | Defines the theme. Accepts dark and light. When theme is set, uses theme-specific color props. |
| hasBorderRadius | Boolean | false | Defines if the select should have border-radius. |
| borderRadius | Number | 0.5 | Defines border-radius. |
| disabled | Boolean | false | Defines if the select is disabled |
| required | Boolean | false | Defines if the select is required |
| fontFamily | String | "'Lato', sans-serif" | Defines the font-family |
| fontSize | String | '1.6em' | Defines the font-size |
| fontWeight | Number | 400 | Defines the font-weight |
| textAlign | String | 'left' | Defines the text alignment. Accepts left, center, right. |
| selectWidth | Number | 200 | Defines the select width in pixels |
| inputStyle | String | 'background' | Defines the input style. Accepts background, line, border. |
| options | Array | Defines the list of options. It is an array of objects with value and text properties (or custom keys via valueKey and textKey props) | |
| selectedOptionOnly | String, Number, Boolean | '' | Defines the selected value for single select. The value must exist in the list options |
| selectedOptionMultiple | Array | Defines the selected values for multiple select. The values must exist in the list options | |
| textKey | String | 'text' | Defines the key name for the text property in options objects |
| valueKey | String | 'value' | Defines the key name for the value property in options objects |
| multiple | Boolean | false | Defines if the select allows multiple selections |
| hasEmptyOption | Boolean | true | Defines if the select should have an empty option (placeholder) |
| emptyOptionText | String | 'Selecione uma opção' | Defines the text for the empty option |
| emptyOptionValue | String, Number | null | Defines the value for the empty option |
| dropdownScrollClass | String | '' | Defines a custom CSS class for the dropdown scroll container |
| hasLabel | Boolean | false | Defines if the select should have a label |
| labelText | String | 'Select: ' | Defines the label text |
| tabindex | Number, String | 0 | Defines the tab index. Set this property to make the select focusable by the keyboard. |
| lightBgColor | String | '#f8f8f2' | Defines the background color for light theme. Accepts Color name and Hex |
| lightBgColorFocus | String | '#eaeaea' | Defines the background color on focus for light theme. Accepts Color name and Hex |
| lightBorderColor | String | '#353734' | Defines the border color for light theme. Accepts Color name and Hex |
| lightBorderColorFocus | String | '#272936' | Defines the border color on focus for light theme. Accepts Color name and Hex |
| lightDisabledBgColor | String | '#dfdfd9' | Defines the disabled background color for light theme. Accepts Color name and Hex |
| lightTextColor | String | '#000000' | Defines the text color for light theme. Accepts Color name and Hex |
| lightDisabledBorderColor | String | 'rgba(53, 55, 52, 0.3)' | Defines the disabled border color for light theme. Accepts Color name and Hex |
| lightOptionTextColor | String | '#000000' | Defines the option text color for light theme. Accepts Color name and Hex |
| lightOptionTextColorSelected | String | '#000000' | Defines the selected option text color for light theme. Accepts Color name and Hex |
| lightOptionBgColorSelected | String | '#e0e0e0' | Defines the selected option background color for light theme. Accepts Color name and Hex |
| lightPlaceholderColor | String | '#999999' | Defines the placeholder color for light theme. Accepts Color name and Hex |
| darkBgColor | String | '#353734' | Defines the background color for dark theme. Accepts Color name and Hex |
| darkBgColorFocus | String | '#272936' | Defines the background color on focus for dark theme. Accepts Color name and Hex |
| darkBorderColor | String | '#44475a' | Defines the border color for dark theme. Accepts Color name and Hex |
| darkBorderColorFocus | String | 'rgba(68, 71, 90, 0.4)' | Defines the border color on focus for dark theme. Accepts Color name and Hex |
| darkDisabledBgColor | String | 'rgba(40, 42, 54, 1)' | Defines the disabled background color for dark theme. Accepts Color name and Hex |
| darkTextColor | String | '#ffffff' | Defines the text color for dark theme. Accepts Color name and Hex |
| darkDisabledBorderColor | String | 'rgba(68, 71, 90, 0.3)' | Defines the disabled border color for dark theme. Accepts Color name and Hex |
| darkOptionTextColor | String | '#ffffff' | Defines the option text color for dark theme. Accepts Color name and Hex |
| darkOptionTextColorSelected | String | '#ffffff' | Defines the selected option text color for dark theme. Accepts Color name and Hex |
| darkOptionBgColorSelected | String | '#3d3d3d' | Defines the selected option background color for dark theme. Accepts Color name and Hex |
| darkPlaceholderColor | String | '#999999' | Defines the placeholder color for dark theme. Accepts Color name and Hex |
Events
| name | Return type | Description |
|---|---|---|
| changed | value | Fired when the selected value changes, returns the selected value(s). |
| user-changed | value | Fired when the user manually changes the selected value, returns the selected value(s). |
| clicked | nothing | Fired when the select is clicked, returns nothing. |
Slot
The component has a slot called slot-select-item where you can customize the display of each option.
<template>
<NbSelect
nb-id="select-custom"
select-name="select-custom"
:options="selectOptions"
:selected-option-only="selectedValue"
>
<template #slot-select-item="{ propRow, propIndex }">
<div>
<strong>{{ propRow.text }}</strong>
<span style="color: #999;"> - {{ propRow.description }}</span>
</div>
</template>
</NbSelect>
</template>
Example
Light Theme (default)
<template>
<NbSelect
nb-id="select-light"
select-name="select-light"
theme="light"
:options="selectOptions"
:selected-option-only="selectedValue"
light-bg-color="#dbeafe"
light-bg-color-focus="#bfdbfe"
light-border-color="#93c5fd"
light-border-color-focus="#60a5fa"
light-text-color="#1e40af"
light-placeholder-color="#64748b"
light-option-text-color="#1e40af"
light-option-text-color-selected="#ffffff"
light-option-bg-color-selected="#3b82f6"
:border-radius="0.375"
@changed="handleChange($event)"
@user-changed="handleUserChange($event)"
/>
</template>
<script setup>
import { ref } from 'vue';
defineOptions({
name: 'CompSelect',
});
const selectOptions = [
{ value: 'option1', text: 'Option 1' },
{ value: 'option2', text: 'Option 2' },
{ value: 'option3', text: 'Option 3' }
];
const selectedValue = ref('option1');
const handleChange = (value) => {
selectedValue.value = value;
console.log('Changed:', value);
};
const handleUserChange = (value) => {
console.log('User changed:', value);
};
</script>
Dark Theme
<template>
<NbSelect
nb-id="select-dark"
select-name="select-dark"
theme="dark"
:options="selectOptions"
:selected-option-only="selectedValue"
dark-bg-color="#8b5cf6"
dark-bg-color-focus="#7c3aed"
dark-border-color="#a78bfa"
dark-border-color-focus="#8b5cf6"
dark-text-color="#f3f4f6"
dark-placeholder-color="#c4b5fd"
dark-option-text-color="#f3f4f6"
dark-option-text-color-selected="#ffffff"
dark-option-bg-color-selected="#6d28d9"
:border-radius="0.5"
@changed="handleChange($event)"
@user-changed="handleUserChange($event)"
/>
</template>
<script setup>
import { ref } from 'vue';
defineOptions({
name: 'CompSelectDark',
});
const selectOptions = [
{ value: 'option1', text: 'Option 1' },
{ value: 'option2', text: 'Option 2' },
{ value: 'option3', text: 'Option 3' }
];
const selectedValue = ref('option2');
const handleChange = (value) => {
selectedValue.value = value;
console.log('Changed:', value);
};
const handleUserChange = (value) => {
console.log('User changed:', value);
};
</script>
Multiple Selection
<template>
<NbSelect
nb-id="select-multiple"
select-name="select-multiple"
theme="light"
:multiple="true"
:options="selectOptions"
:selected-option-multiple="selectedValues"
@changed="handleChange($event)"
@user-changed="handleUserChange($event)"
/>
</template>
<script setup>
import { ref } from 'vue';
defineOptions({
name: 'CompSelectMultiple',
});
const selectOptions = [
{ value: 'option1', text: 'Option 1' },
{ value: 'option2', text: 'Option 2' },
{ value: 'option3', text: 'Option 3' }
];
const selectedValues = ref(['option1', 'option2']);
const handleChange = (value) => {
selectedValues.value = value;
console.log('Changed:', value);
};
const handleUserChange = (value) => {
console.log('User changed:', value);
};
</script>
Comportamento
Fechamento Automático
O dropdown do componente fecha automaticamente em duas situações:
- Quando o foco sai do componente: Ao usar Tab para navegar para fora do componente ou clicar em outro elemento da página, o dropdown fecha automaticamente.
- Quando outro select é aberto: Ao abrir um novo select (single ou múltiplo), todos os outros selects abertos fecham automaticamente. Isso garante que apenas um select esteja aberto por vez.
Modo Múltiplo
No modo múltiplo (multiple="true"), o dropdown permanece aberto após selecionar uma opção, permitindo que você selecione múltiplas opções sem precisar reabrir o dropdown. O dropdown só fecha quando:
- O foco sai do componente
- Outro select é aberto
- Você clica fora do componente
Limitações
Overflow Hidden
O dropdown do componente usa position: absolute para posicionamento. Se o componente estiver dentro de um container pai com overflow: hidden, o dropdown pode ser cortado ou ocultado.
Solução: Remova ou ajuste a propriedade overflow: hidden no container pai do componente para permitir que o dropdown seja exibido corretamente.
<!-- ❌ Problema: dropdown será cortado -->
<div style="overflow: hidden;">
<NbSelect nb-id="select-1" :options="options" />
</div>
<!-- ✅ Solução: remova overflow: hidden ou use overflow: visible -->
<div style="overflow: visible;">
<NbSelect nb-id="select-1" :options="options" />
</div>
