feat(components): add Select, SelectItem, SelectItemGroup

Closes #31
This commit is contained in:
Eric Liu 2019-12-21 17:18:35 -08:00
commit 6c75c8a973
9 changed files with 243 additions and 0 deletions

View file

@ -47,6 +47,10 @@ Currently, the following components are supported:
- RadioButtonGroup - RadioButtonGroup
- Search - Search
- SearchSkeleton - SearchSkeleton
- Select
- SelectSkeleton
- SelectItem
- SelectItemGroup
- SkeletonPlaceholder - SkeletonPlaceholder
- SkeletonText - SkeletonText
- Tag - Tag

View file

@ -0,0 +1,19 @@
<script>
let className = undefined;
export { className as class };
export let hideLabel = false;
export let style = undefined;
import { cx } from '../../lib';
const _class = cx('--form-item', className);
</script>
<div on:click on:mouseover on:mouseenter on:mouseleave class={_class} {style}>
{#if !hideLabel}
<span class={cx('--label', '--skeleton')} />
{/if}
<div class={cx('--select', '--skeleton')}>
<div class={cx('--select-input')} />
</div>
</div>

View file

@ -0,0 +1,29 @@
<script>
export let story = undefined;
import Layout from '../../internal/ui/Layout.svelte';
import Select from './Select.svelte';
import SelectItem from './SelectItem.svelte';
import SelectSkeleton from './Select.Skeleton.svelte';
import SelectItemGroup from './SelectItemGroup.svelte';
</script>
<Layout>
<div>
{#if story === 'skeleton'}
<SelectSkeleton {...$$props} />
{:else}
<Select {...$$props.select} id="select-1" defaultValue="placeholder-item">
<SelectItem value="placeholder-item" text="Choose an option" disabled hidden />
<SelectItemGroup {...$$props.group} label="Category 1">
<SelectItem value="option-1" text="Option 1" />
<SelectItem value="option-2" text="Option 2" />
</SelectItemGroup>
<SelectItemGroup {...$$props.group} label="Category 2">
<SelectItem value="option-3" text="Option 3" />
<SelectItem value="option-4" text="Option 4" />
</SelectItemGroup>
</Select>
{/if}
</div>
</Layout>

View file

@ -0,0 +1,36 @@
import { withKnobs, text, boolean } from '@storybook/addon-knobs';
import Component from './Select.Story.svelte';
export default { title: 'Select', decorators: [withKnobs] };
const labelPositions = {
'Left (left)': 'left',
'Right (right)': 'right'
};
export const Default = () => ({
Component,
props: {
select: {
light: boolean('Light variant (light in <Select>)', false),
inline: boolean('Put control in-line with label (inline in <Select>)', false),
disabled: boolean('Disabled (disabled in <Select>)', false),
hideLabel: boolean('No label (hideLabel in <Select>)', false),
invalid: boolean('Show form validation UI (invalid in <Select>)', false),
invalidText: text(
'Form validation UI content (invalidText in <Select>)',
'A valid value is required'
),
labelText: text('Label text (helperText)', 'Select'),
helperText: text('Helper text (helperText)', 'Optional helper text.')
},
group: {
disabled: boolean('Disabled (disabled in <SelectItemGroup>)', false)
}
}
});
export const Skeleton = () => ({
Component,
props: { story: 'skeleton', hideLabel: boolean('No label (hideLabel in <Select>)', false) }
});

View file

@ -0,0 +1,110 @@
<script>
let className = undefined;
export { className as class };
export let id = Math.random();
export let inline = false;
export let labelText = '';
export let disabled = false;
export let defaultValue = undefined;
export let hideLabel = false;
export let invalid = false;
export let invalidText = '';
export let helperText = '';
export let light = false;
export let noLabel = false;
export let style = undefined;
import { createEventDispatcher, setContext } from 'svelte';
import { writable } from 'svelte/store';
import ChevronDown16 from 'carbon-icons-svelte/lib/ChevronDown16';
import WarningFilled16 from 'carbon-icons-svelte/lib/WarningFilled16';
import { cx } from '../../lib';
const dispatch = createEventDispatcher();
const errorId = `error-${id}`;
const _class = cx(
'--select',
inline && '--select--inline',
light && '--select--light',
invalid && '--select--invalid',
disabled && '--select--disabled',
className
);
const _labelClass = cx(
'--label',
hideLabel && '--visually-hidden',
disabled && '--label--disabled'
);
const _helperTextClass = cx('--form__helper-text', disabled && '--form__helper-text--disabled');
let selected = writable(defaultValue);
setContext('Select', { selected });
$: {
defaultValue = $selected;
dispatch('change', $selected);
}
</script>
<div class={cx('--form-item')} {style}>
<div class={_class}>
{#if !noLabel}
<label for={id} class={_labelClass}>{labelText}</label>
{/if}
{#if !inline && helperText}
<div class={_helperTextClass}>{helperText}</div>
{/if}
{#if inline}
<div class={cx('--select-input--inline__wrapper')}>
<div class={cx('--select-input__wrapper')} data-invalid={invalid || undefined}>
<select
class={cx('--select-input')}
aria-describedby={invalid ? errorId : undefined}
disabled={disabled || undefined}
aria-invalid={invalid || undefined}
on:change={event => {
selected.set(event.target.value);
}}
{id}>
<slot />
</select>
<ChevronDown16 class={cx('--select__arrow')} />
{#if invalid}
<WarningFilled16 class={cx('--select__invalid-icon')} />
{/if}
</div>
{#if invalid}
<div class={cx('--form-requirement')} id={errorId}>{invalidText}</div>
{/if}
</div>
{#if helperText}
<div class={_helperTextClass}>{helperText}</div>
{/if}
{/if}
{#if !inline}
<div class={cx('--select-input__wrapper')} data-invalid={invalid || undefined}>
<select
class={cx('--select-input')}
aria-describedby={invalid ? errorId : undefined}
disabled={disabled || undefined}
aria-invalid={invalid || undefined}
on:change
on:change={event => {
selected.set(event.target.value);
}}
{id}>
<slot />
</select>
<ChevronDown16 class={cx('--select__arrow')} />
{#if invalid}
<WarningFilled16 class={cx('--select__invalid-icon')} />
{/if}
</div>
{#if invalid}
<div class={cx('--form-requirement')} id={errorId}>{invalidText}</div>
{/if}
{/if}
</div>
</div>

View file

@ -0,0 +1,19 @@
<script>
let className = undefined;
export { className as class };
export let value = '';
export let text = '';
export let disabled = false;
export let hidden = false;
export let style = undefined;
import { getContext } from 'svelte';
import { cx } from '../../lib';
const { selected } = getContext('Select');
const _class = cx('--select-option', className);
</script>
<option class={_class} {value} {disabled} {hidden} {style} selected={$selected === value}>
{text}
</option>

View file

@ -0,0 +1,15 @@
<script>
let className = undefined;
export { className as class };
export let disabled = false;
export let label = 'Provide label';
export let style = undefined;
import { cx } from '../../lib';
const _class = cx('--select-optgroup', className);
</script>
<optgroup class={_class} {label} {disabled} {style}>
<slot />
</optgroup>

View file

@ -0,0 +1,6 @@
import Select from './Select.svelte';
export default Select;
export { default as SelectSkeleton } from './Select.Skeleton.svelte';
export { default as SelectItem } from './SelectItem.svelte';
export { default as SelectItemGroup } from './SelectItemGroup.svelte';

View file

@ -27,6 +27,7 @@ import ProgressIndicator, {
import RadioButton, { RadioButtonSkeleton } from './components/RadioButton'; import RadioButton, { RadioButtonSkeleton } from './components/RadioButton';
import RadioButtonGroup from './components/RadioButtonGroup'; import RadioButtonGroup from './components/RadioButtonGroup';
import Search, { SearchSkeleton } from './components/Search'; import Search, { SearchSkeleton } from './components/Search';
import Select, { SelectSkeleton, SelectItem, SelectItemGroup } from './components/Select';
import SkeletonPlaceholder from './components/SkeletonPlaceholder'; import SkeletonPlaceholder from './components/SkeletonPlaceholder';
import SkeletonText from './components/SkeletonText'; import SkeletonText from './components/SkeletonText';
import Tag, { TagSkeleton } from './components/Tag'; import Tag, { TagSkeleton } from './components/Tag';
@ -85,6 +86,10 @@ export {
Search, Search,
SearchSkeleton, SearchSkeleton,
SelectableTile, SelectableTile,
Select,
SelectSkeleton,
SelectItem,
SelectItemGroup,
SkeletonPlaceholder, SkeletonPlaceholder,
SkeletonText, SkeletonText,
Switch, Switch,