62 lines
3.0 KiB
Plaintext
62 lines
3.0 KiB
Plaintext
component Select {
|
|
props {
|
|
@event input = function
|
|
@event change = function
|
|
@event focus = function
|
|
@event blur = function
|
|
@event open = function
|
|
@event close = function
|
|
@event invalid = function
|
|
size = "default"
|
|
color = "primary"
|
|
id = ""
|
|
name = ""
|
|
label = "Select"
|
|
hiddenLabel = false
|
|
value = ""
|
|
values = []
|
|
options = []
|
|
placeholder = "Select an option"
|
|
variant = "normal"
|
|
icon = ""
|
|
iconPosition = "start"
|
|
helperText = ""
|
|
cornerHint = ""
|
|
error = ""
|
|
inline = false
|
|
multiple = false
|
|
readonly = false
|
|
disabled = false
|
|
required = false
|
|
class = ""
|
|
}
|
|
functions {
|
|
function selected(option) {
|
|
if (multiple) return values.includes(option.value)
|
|
return option.value === value
|
|
}
|
|
function emitField(nameEvent, sourceEvent) {
|
|
sourceEvent.stopPropagation()
|
|
$emit(nameEvent, { value: sourceEvent.currentTarget.value, name: name, sourceEvent: sourceEvent })
|
|
}
|
|
function handleFocus(sourceEvent) { emitField("focus", sourceEvent); $emit("open", { name: name, sourceEvent: sourceEvent }) }
|
|
function handleBlur(sourceEvent) { emitField("blur", sourceEvent); $emit("close", { name: name, sourceEvent: sourceEvent }) }
|
|
function handleInvalid(sourceEvent) { $emit("invalid", { name: name, message: sourceEvent.currentTarget.validationMessage, sourceEvent: sourceEvent }) }
|
|
}
|
|
view {
|
|
<div {...attrs} class="wire-next wire-next--color-{color} wire-next--size-{size} wire-next--field wire-next--select {class}" data-variant="{variant}" data-inline="{inline}" data-floating="{variant === 'floating'}" data-invalid="{error ? 'true' : 'false'}" data-readonly="{readonly}">
|
|
<div class="wire-next__field-heading"><label class="{hiddenLabel ? 'wire-next__sr-only' : ''}" for="{id || name}">{label}</label>{#if cornerHint}<span class="wire-next__field-hint">{cornerHint}</span>{/if}</div>
|
|
<div class="wire-next__field-control" data-icon-position="{iconPosition}">
|
|
{#if icon}<span class="{icon} wire-next__field-icon" aria-hidden="true"></span>{/if}
|
|
<select id="{id || name}" name="{name}" multiple="{multiple}" disabled="{disabled || readonly}" required="{required}" aria-readonly="{readonly}" aria-invalid="{error ? 'true' : 'false'}" @input="emitField('input', event)" @change="emitField('change', event)" @focus="handleFocus(event)" @blur="handleBlur(event)" @invalid="handleInvalid(event)">
|
|
{#if !multiple}<option value="" selected="{value === ''}" disabled="{required}">{placeholder}</option>{/if}
|
|
{#each options as option}<option value="{option.value}" selected="{selected(option)}" disabled="{option.disabled}">{option.label}</option>{/each}
|
|
</select>
|
|
{#if variant === "floating"}<span class="wire-next__field-floating-label">{label}</span>{/if}
|
|
</div>
|
|
{#if helperText}<small class="wire-next__field-help">{helperText}</small>{/if}
|
|
<small class="wire-next__field-error" data-error="{name}">{error}</small>
|
|
</div>
|
|
}
|
|
}
|