mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
* fix(data-table): type DataTableRowId as "any" #530 Fixes #530 * fix(tree-view): make first node focusable if activeId does not match selected * fix(overflow-menu): set type="button" to prevent default submit behavior #554 Fixes #554 * fix(overflow-menu): update semantic attributes in OverflowMenuItem * fix(number-input): do not dispatch change event on initialization #561 Fixes #561 * fix(tree-view): make first focusable node tabbable regardless of active/selected states * fix(date-picker): load rangePlugin dynamically * build(rollup): enable inlineDynamicImports * build(rollup): remove "clipboard-copy" global * fix(overflow-menu): do not render title if using a slot #537 Fixes #537 * fix(select): forward missing focus, input events #501 Fixes #501
201 lines
5.4 KiB
Svelte
201 lines
5.4 KiB
Svelte
<script>
|
|
/**
|
|
* @event {string} change
|
|
*/
|
|
|
|
/**
|
|
* Specify the selected item value
|
|
* @type {string}
|
|
*/
|
|
export let selected = undefined;
|
|
|
|
/**
|
|
* Set the size of the select input
|
|
* @type {"sm" | "xl"}
|
|
*/
|
|
export let size = undefined;
|
|
|
|
/** Set to `true` to use the inline variant */
|
|
export let inline = false;
|
|
|
|
/** Set to `true` to enable the light variant */
|
|
export let light = false;
|
|
|
|
/** Set to `true` to disable the select element */
|
|
export let disabled = false;
|
|
|
|
/** Set an id for the select element */
|
|
export let id = "ccs-" + Math.random().toString(36);
|
|
|
|
/**
|
|
* Specify a name attribute for the select element
|
|
* @type {string}
|
|
*/
|
|
export let name = undefined;
|
|
|
|
/** Set to `true` to indicate an invalid state */
|
|
export let invalid = false;
|
|
|
|
/** Specify the invalid state text */
|
|
export let invalidText = "";
|
|
|
|
/** Set to `true` to indicate an warning state */
|
|
export let warn = false;
|
|
|
|
/** Specify the warning state text */
|
|
export let warnText = "";
|
|
|
|
/** Specify the helper text */
|
|
export let helperText = "";
|
|
|
|
/** Set to `true` to not render a label */
|
|
export let noLabel = false;
|
|
|
|
/** Specify the label text */
|
|
export let labelText = "";
|
|
|
|
/** Set to `true` to visually hide the label text */
|
|
export let hideLabel = false;
|
|
|
|
/** Obtain a reference to the select HTML element */
|
|
export let ref = null;
|
|
|
|
import { createEventDispatcher, setContext, afterUpdate } from "svelte";
|
|
import { writable } from "svelte/store";
|
|
import ChevronDown16 from "carbon-icons-svelte/lib/ChevronDown16/ChevronDown16.svelte";
|
|
import WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16/WarningFilled16.svelte";
|
|
import WarningAltFilled16 from "carbon-icons-svelte/lib/WarningAltFilled16/WarningAltFilled16.svelte";
|
|
|
|
const dispatch = createEventDispatcher();
|
|
const selectedValue = writable(selected);
|
|
|
|
setContext("Select", { selectedValue });
|
|
|
|
afterUpdate(() => {
|
|
selected = $selectedValue;
|
|
dispatch("change", $selectedValue);
|
|
});
|
|
|
|
$: errorId = `error-${id}`;
|
|
$: selectedValue.set(selected);
|
|
</script>
|
|
|
|
<div class:bx--form-item="{true}" {...$$restProps}>
|
|
<div
|
|
class:bx--select="{true}"
|
|
class:bx--select--inline="{inline}"
|
|
class:bx--select--light="{light}"
|
|
class:bx--select--invalid="{invalid}"
|
|
class:bx--select--disabled="{disabled}"
|
|
class:bx--select--warning="{warn}"
|
|
>
|
|
{#if !noLabel}
|
|
<label
|
|
for="{id}"
|
|
class:bx--label="{true}"
|
|
class:bx--visually-hidden="{hideLabel}"
|
|
class:bx--label--disabled="{disabled}"
|
|
>
|
|
<slot name="labelText">
|
|
{labelText}
|
|
</slot>
|
|
</label>
|
|
{/if}
|
|
{#if inline}
|
|
<div class:bx--select-input--inline__wrapper="{true}">
|
|
<div
|
|
class:bx--select-input__wrapper="{true}"
|
|
data-invalid="{invalid || undefined}"
|
|
>
|
|
<select
|
|
bind:this="{ref}"
|
|
aria-describedby="{invalid ? errorId : undefined}"
|
|
aria-invalid="{invalid || undefined}"
|
|
disabled="{disabled || undefined}"
|
|
id="{id}"
|
|
name="{name}"
|
|
class:bx--select-input="{true}"
|
|
class="{size && `bx--select-input--${size}`}"
|
|
on:change="{({ target }) => {
|
|
selectedValue.set(target.value);
|
|
}}"
|
|
on:input
|
|
on:focus
|
|
on:blur
|
|
>
|
|
<slot />
|
|
</select>
|
|
<ChevronDown16 class="bx--select__arrow" />
|
|
{#if invalid}
|
|
<WarningFilled16 class="bx--select__invalid-icon" />
|
|
{/if}
|
|
</div>
|
|
{#if invalid}
|
|
<div class:bx--form-requirement="{true}" id="{errorId}">
|
|
{invalidText}
|
|
</div>
|
|
{/if}
|
|
</div>
|
|
{#if helperText}
|
|
<div
|
|
class:bx--form__helper-text="{true}"
|
|
class:bx--form__helper-text--disabled="{disabled}"
|
|
>
|
|
{helperText}
|
|
</div>
|
|
{/if}
|
|
{/if}
|
|
{#if !inline}
|
|
<div
|
|
class:bx--select-input__wrapper="{true}"
|
|
data-invalid="{invalid || undefined}"
|
|
>
|
|
<select
|
|
bind:this="{ref}"
|
|
id="{id}"
|
|
name="{name}"
|
|
aria-describedby="{invalid ? errorId : undefined}"
|
|
disabled="{disabled || undefined}"
|
|
aria-invalid="{invalid || undefined}"
|
|
class:bx--select-input="{true}"
|
|
class="{size && `bx--select-input--${size}`}"
|
|
on:change="{({ target }) => {
|
|
selectedValue.set(target.value);
|
|
}}"
|
|
on:input
|
|
on:focus
|
|
on:blur
|
|
>
|
|
<slot />
|
|
</select>
|
|
<ChevronDown16 class="bx--select__arrow" />
|
|
{#if invalid}
|
|
<WarningFilled16 class="bx--select__invalid-icon" />
|
|
{/if}
|
|
{#if !invalid && warn}
|
|
<WarningAltFilled16
|
|
class="bx--select__invalid-icon bx--select__invalid-icon--warning"
|
|
/>
|
|
{/if}
|
|
</div>
|
|
{#if !invalid && helperText}
|
|
<div
|
|
class:bx--form__helper-text="{true}"
|
|
class:bx--form__helper-text--disabled="{disabled}"
|
|
>
|
|
{helperText}
|
|
</div>
|
|
{/if}
|
|
{#if invalid}
|
|
<div id="{errorId}" class:bx--form-requirement="{true}">
|
|
{invalidText}
|
|
</div>
|
|
{/if}
|
|
{#if !invalid && warn}
|
|
<div id="{errorId}" class:bx--form-requirement="{true}">
|
|
{warnText}
|
|
</div>
|
|
{/if}
|
|
{/if}
|
|
</div>
|
|
</div>
|