From 75a5487dce6ca0b9fa02022a7832f61f10d095f6 Mon Sep 17 00:00:00 2001 From: Adrian Schmidt Date: Mon, 1 Apr 2019 09:23:27 +0200 Subject: [PATCH] feat(select): add `multiple` option to limel-select, replacing limel-multi-select fix #203 --- src/components/multi-select/multi-select.mdx | 13 -- .../select-multiple/select-multiple.scss | 22 +++ .../select-multiple/select-multiple.tsx | 137 +++++++++++++++++ .../select-single.e2e.ts} | 0 .../select-single.scss} | 0 .../select-single/select-single.tsx | 135 +++++++++++++++++ .../test/select.test-wrapper.tsx | 0 src/components/select/select.mdx | 4 + src/components/select/select.tsx | 138 ++++++------------ src/examples/multi-select/multi-select.scss | 3 - src/examples/multi-select/multi-select.tsx | 55 ------- src/examples/select/select-multiple.tsx | 65 +++++++++ 12 files changed, 409 insertions(+), 163 deletions(-) delete mode 100644 src/components/multi-select/multi-select.mdx create mode 100644 src/components/select-multiple/select-multiple.scss create mode 100644 src/components/select-multiple/select-multiple.tsx rename src/components/{select/select.e2e.ts => select-single/select-single.e2e.ts} (100%) rename src/components/{select/select.scss => select-single/select-single.scss} (100%) create mode 100644 src/components/select-single/select-single.tsx rename src/components/{select => select-single}/test/select.test-wrapper.tsx (100%) delete mode 100644 src/examples/multi-select/multi-select.scss delete mode 100644 src/examples/multi-select/multi-select.tsx create mode 100644 src/examples/select/select-multiple.tsx diff --git a/src/components/multi-select/multi-select.mdx b/src/components/multi-select/multi-select.mdx deleted file mode 100644 index 43a9c94c8a..0000000000 --- a/src/components/multi-select/multi-select.mdx +++ /dev/null @@ -1,13 +0,0 @@ ---- -name: Multi Select -route: /multi-select -menu: Components ---- - -# Multi Select - - - -## Example - - diff --git a/src/components/select-multiple/select-multiple.scss b/src/components/select-multiple/select-multiple.scss new file mode 100644 index 0000000000..a4cdd31001 --- /dev/null +++ b/src/components/select-multiple/select-multiple.scss @@ -0,0 +1,22 @@ +@import '../../style/variables'; + +@import '@lime-material-16px/form-field/mdc-form-field'; +@import '@lime-material-16px/checkbox/mdc-checkbox'; +@import "@lime-material-16px/floating-label/mdc-floating-label"; + +.multi-select { + position: relative; + + .multi-select-label { + padding-left: pxToRem(15); + } + + .mdc-form-field { + display: flex; + } + + .mdc-checkbox { + @include mdc-checkbox-ink-color(primary); + @include mdc-checkbox-container-colors(secondary, on-primary, secondary, on-primary); + } +} diff --git a/src/components/select-multiple/select-multiple.tsx b/src/components/select-multiple/select-multiple.tsx new file mode 100644 index 0000000000..dcb44806ba --- /dev/null +++ b/src/components/select-multiple/select-multiple.tsx @@ -0,0 +1,137 @@ +import { MDCCheckbox } from '@lime-material-16px/checkbox'; +import { MDCFormField } from '@lime-material-16px/form-field'; +import { + Component, + Element, + Event, + EventEmitter, + Prop, + State, +} from '@stencil/core'; +import { Option } from '../../interface'; +import { createRandomString } from '../../util/random-string'; + +@Component({ + tag: 'limel-select-multiple', + shadow: true, + styleUrl: 'select-multiple.scss', +}) +export class SelectMultiple { + @Prop({ reflectToAttr: true }) + public disabled = false; + + @Prop({ reflectToAttr: true }) + public label: string; + + @Prop() + public value: Option[] = []; + + @Prop() + public options: Option[] = []; + + @Event() + private change: EventEmitter; + + @Element() + private limelMultiSelect: HTMLElement; + + @State() + private fieldId = createRandomString(); + + @State() + private mdcCheckboxes = []; + + public componentDidLoad() { + const elements = Array.from( + this.limelMultiSelect.shadowRoot.querySelectorAll( + '.multi-select .mdc-form-field' + ) + ); + + elements.forEach(element => { + const formField = new MDCFormField(element); + const checkbox = new MDCCheckbox(element.firstChild); + formField.input = checkbox; + this.mdcCheckboxes.push(checkbox); + }); + + this.onChange(); + } + + public render() { + return ( +
+ { return option.text }).join(', ')}> +
+ {this.options.map((option: Option, index: number) => { + return this.renderCheckbox(index, option); + })} +
+
+
+ ); + } + + private renderCheckbox(index: number, option: Option) { + return ( +
+
+ +
+ + + +
+
+
+ +
+ ); + } + + private isOptionChecked(option: Option) { + return this.value.find(checkedOption => { + return checkedOption.value === option.value; + }); + } + + private onChange = (event?) => { + if (event) { + event.stopPropagation(); + } + + const checked = this.options.filter(option => { + const optionChecked = this.mdcCheckboxes.some(mdcCheckbox => { + return ( + mdcCheckbox.checked && mdcCheckbox.value === option.value + ); + }); + if (optionChecked) { + return option; + } + }); + this.change.emit(checked); + }; +} diff --git a/src/components/select/select.e2e.ts b/src/components/select-single/select-single.e2e.ts similarity index 100% rename from src/components/select/select.e2e.ts rename to src/components/select-single/select-single.e2e.ts diff --git a/src/components/select/select.scss b/src/components/select-single/select-single.scss similarity index 100% rename from src/components/select/select.scss rename to src/components/select-single/select-single.scss diff --git a/src/components/select-single/select-single.tsx b/src/components/select-single/select-single.tsx new file mode 100644 index 0000000000..f0e03ded80 --- /dev/null +++ b/src/components/select-single/select-single.tsx @@ -0,0 +1,135 @@ +import { MDCSelect } from '@lime-material-16px/select'; +import { + Component, + Element, + Event, + EventEmitter, + Prop, + State, + Watch, +} from '@stencil/core'; +import { Option } from '../../interface'; + +@Component({ + tag: 'limel-select-single', + shadow: true, + styleUrl: 'select-single.scss', +}) +export class SelectSingle { + /** + * Set to `true` to disable the input. + */ + @Prop({ reflectToAttr: true }) + public disabled = false; + + /** + * The input label. + */ + @Prop({ reflectToAttr: true }) + public label: string; + + /** + * The currently selected item. + */ + @Prop() + public value: Option; + + @Prop() + public options: Option[] = []; + + @Event() + private change: EventEmitter