# Ejemplo de código de Selector. DESY. Sistema de diseño del Gobierno de Aragón.
## Componentes Índice de páginas
Componentes
# Selector
Formularios
Es un elemento de formulario que permite seleccionar una opción de entre un listado de varias. Cuando está plegado, muestra la opción actualmente seleccionada y cuando está expandido muestra una lista desplegable de opciones predefinidas.
# Listbox
Mostrar parámetros
## Parámetros Nunjucks del componente: "Listbox"
```yaml
params:
- name: id
type: string
required: true
description: The id of the listbox.
- name: isMultiselectable
type: boolean
required: false
description: Defaults to false. If true, allows multiple selections.
- name: doesChangeButtonText
type: boolean
required: false
description: Defaults to false. If true, the button text is updated with option selected text.
- name: text
type: string
required: true
description: If `html` is set, this is not required. Text for the button. If `html` is provided, the `text` argument will be ignored.
- name: html
type: string
required: true
description: If `text` is set, this is not required. HTML for the button. If `html` is provided, the `text` argument will be ignored.
- name: label
type: object
required: true
description: Label text or HTML by specifying value for either text or html keys.
- name: text
type: string
required: true
description: If `html` is set, this is not required. Text for the button. If `html` is provided, the `text` argument will be ignored.
- name: html
type: string
required: true
description: If `text` is set, this is not required. HTML for the button. If `html` is provided, the `text` argument will be ignored.
- name: classes
type: string
required: false
description: Classes to add to the element that acts as label.
- name: classesContainer
type: string
required: false
description: Classes to add to the container div of button and tooltip elements.
- name: classesTooltip
type: string
required: false
description: Classes to add to the tooltip content.
- name: idPrefix
type: string
required: false
description: String to prefix id for each item if no id is specified on each item. If not passed, fall back to using the name option instead.
- name: items
type: array
required: true
description: Array of items objects.
params:
- name: text
type: string
required: true
description: If `html` is set, this is not required. Text to use within each item. If `html` is provided, the `text` argument will be ignored.
- name: html
type: string
required: true
description: If `text` is set, this is not required. HTML to use within each item. If `html` is provided, the `text` argument will be ignored.
- name: id
type: string
required: false
description: Specific id attribute for the item. If omitted, then idPrefix option will be applied.
- name: active
type: boolean
required: false
description: If true, item will be active.
- name: classes
type: string
required: false
description: Classes to add to the item.
- name: attributes
type: object
required: false
description: HTML attributes (for example data attributes) to add to the item.
- name: classes
type: string
required: false
description: Classes to add to the button element.
- name: attributes
type: object
required: false
description: HTML attributes (for example data attributes) to add to the button element.
```
### Por defecto [#](#por-defecto)
Mostrar códigodel ejemplo: Por defecto
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "default",
"text": "Por defecto",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"html": "Option 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Option 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Con estado activo [#](#con-estado-activo)
Simula activar la pseudo-clase de CSS `:active`. En realidad sólo se usa para documentar estos ejemplos.
Mostrar códigodel ejemplo: Con estado activo
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "with-active-state",
"text": "Activo",
"label": {
"text": "Esto es un label"
},
"classes": "ds-active",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Con estado hover [#](#con-estado-hover)
Simula activar la pseudo-clase de CSS `:hover`. En realidad sólo se usa para documentar estos ejemplos.
Mostrar códigodel ejemplo: Con estado hover
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "with-hover-state",
"text": "Hover",
"label": {
"text": "Esto es un label"
},
"classes": "ds-hover",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Con estado focus [#](#con-estado-focus)
Simula activar la pseudo-clase de CSS `:focus`. En realidad sólo se usa para documentar estos ejemplos.
Mostrar códigodel ejemplo: Con estado focus
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "with-focus-state",
"text": "Focus",
"label": {
"text": "Esto es un label"
},
"classes": "ds-focus",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
```
### Con clases de css aplicadas al container [#](#con-clases-de-css-aplicadas-al-container)
Tanto el botón como los elementos del tooltip están rodeados de un div contenedor al que se le pueden aplicar estilos de CSS.
Mostrar códigodel ejemplo: Con clases de css aplicadas al container
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "classes-applied-to-container-element",
"text": "Clases en container",
"label": {
"text": "Esto es un label"
},
"classesContainer": "inline-block p-base bg-primary-light",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Clases aplicadas al contenido del tooltip [#](#clases-aplicadas-al-contenido-del-tooltip)
El contenido del tooltip puede tener clases adicionales aplicadas. En este ejemplo se le ha aplicado una altura máxima y scroll vertical si el contenido se desborda.
Mostrar códigodel ejemplo: Clases aplicadas al contenido del tooltip
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "classes-applied-to-tooltip-content",
"text": "Clases al contenido del tooltip",
"label": {
"text": "Esto es un label"
},
"classesTooltip": "max-h-24 overflow-y-auto",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Clases aplicadas a varios elementos [#](#clases-aplicadas-a-varios-elementos)
En este ejemplo, usando clases de CSS, la anchura del contenido establece la anchura del tooltip.
Mostrar códigodel ejemplo: Clases aplicadas a varios elementos
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "classes-applied-to-various-elements",
"text": "Listbox de anchura completa",
"label": {
"text": "Esto es un label",
"classes": "font-semibold text-sm"
},
"classes": "w-full justify-between",
"classesTooltip": "w-max max-h-64 overflow-y-auto",
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Con item activo [#](#con-item-activo)
Podemos seleccionar inicialmente un item si le añadimos el parámetro `'active': true`. También puedes usar con javascript la función global `activateItemListBox(elementMenu, activeItemId)` para seleccionar un item, usando sus ids. Ej: Abre la consola del navegador y escribe `activateItemListBox('listbox', 'with-active-item-listbox-item-2')` para seleccionar el segundo item de este ejemplo.
Mostrar códigodel ejemplo: Con item activo
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "with-active-item",
"text": "con item activo",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4 activa",
"active": true
},
{
"href": "#",
"text": "Opción 5"
}
],
"attributes": {
"id": "listbox"
}
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4 activa
Opción 5
```
### Permite selecciones múltiples [#](#permite-selecciones-mltiples)
Usa el parámetro `'isMultiselectable': true`. En este caso al seleccionar un item el elemento no se cerrará. También puedes usar con javascript la función global `activateItemListBox(elementMenu, activeItemId)` para seleccionar varios items, usando sus ids. Ej: Abre la consola del navegador y escribe `activateItemListBox('listbox-multiple', ['is-multiselectable-listbox-item-2', 'is-multiselectable-listbox-item-3'])` para seleccionar el segundo y el tercer item de este ejemplo.
Mostrar códigodel ejemplo: Permite selecciones múltiples
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "is-multiselectable",
"isMultiselectable": true,
"text": "Selecciones múltiples",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
],
"attributes": {
"id": "listbox-multiple"
}
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Cambia el texto del botón [#](#cambia-el-texto-del-botn)
Usa el parámetro `'doesChangeButtonText': true` para hacer que el texto del botón se actualice al seleccionar un item. Si `'doesChangeButtonText': true`, entonces no tiene efecto
Mostrar códigodel ejemplo: Cambia el texto del botón
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "does-change-button-text",
"text": "Opción 1",
"label": {
"text": "Esto es un label"
},
"doesChangeButtonText": true,
"items": [
{
"href": "#",
"html": "Option 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5"
}
]
}) }}
```
##### HTML
```html
Esto es un label
Option 1
Opción 2
Opción 3
Opción 4
Opción 5
```
### Con iconos en items [#](#con-iconos-en-items)
Mostrar códigodel ejemplo: Con iconos en items
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "icons",
"text": "Iconos en items",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"html": "Opción 1 "
},
{
"href": "#",
"html": "Opción 2 "
},
{
"href": "#",
"html": "Opción 3 "
}
]
}) }}
```
##### HTML
```html
Esto es un label
Opción 1
Opción 2
Opción 3
```
### Con párrafos en items [#](#con-prrafos-en-items)
Mostrar códigodel ejemplo: Con párrafos en items
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "paragraphs",
"text": "Párrafos en items",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"html": "Actuaciones previas/preparatorias
Consulta previa, actos preparatorios de expediente, bases reguladoras...
",
"classes": "flex-wrap"
},
{
"href": "#",
"html": "Inicio de la tramitación
Documentos que no se puedan asociar a ningún otro tramite en fase de inicio
Participación pública
Información pública, alegaciones, audiencia...
Informes sectoriales
Informes sectoriales que se emiten en la tramitación por órganos distintos del instructor...
Valoración/Prueba/Licitación
Informe de valoración, documentos técnicos de evaluación, peticiones de prueba, documentos fase de licitación, informes propuesta...
```
### Menú abierto o cerrado con javascript [#](#men-abierto-o-cerrado-con-javascript)
Podemos abrir o cerrar un menú usando con javascript la función global `toggleMenuListbox(elementMenu, open)` para abrir o cerrar un menú, usando su id. El parámetro `open` admite `true` o `false`, si le pasamos `true` abrirá el menú, y si le pasamos `false` cerrará el menú. Ej: Abre la consola del navegador y escribe `toggleMenuListbox('listbox-javascript', true)` para abrir el menú de este ejemplo.
Mostrar códigodel ejemplo: Menú abierto o cerrado con javascript
#### Contenido
Nunjucks macro HTML
##### Nunjucks macro
```js
{% from "components/listbox/_macro.listbox.njk" import componentListbox %}
{{ componentListbox({
"id": "with-active-unactive-item",
"text": "con item activo",
"label": {
"text": "Esto es un label"
},
"items": [
{
"href": "#",
"text": "Opción 1"
},
{
"href": "#",
"text": "Opción 2"
},
{
"href": "#",
"text": "Opción 3"
},
{
"href": "#",
"text": "Opción 4"
},
{
"href": "#",
"text": "Opción 5",
"active": true
}
],
"attributes": {
"id": "listbox-javascript"
}
}) }}
```
##### HTML
```html