-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(select): add
multiple
option to limel-select, replacing limel-…
…multi-select fix #203
- Loading branch information
1 parent
13f7f49
commit 1f7fd41
Showing
12 changed files
with
443 additions
and
186 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@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); | ||
} | ||
|
||
limel-collapsible-section { | ||
--body-max-height: 70vh; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
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<Option[]>; | ||
|
||
@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 ( | ||
<div class="multi-select"> | ||
<limel-collapsible-section | ||
header={ | ||
this.label + | ||
': ' + | ||
this.value | ||
.map(option => { | ||
return option.text; | ||
}) | ||
.join(', ') | ||
} | ||
> | ||
<div> | ||
{this.options.map((option: Option, index: number) => { | ||
return this.renderCheckbox(index, option); | ||
})} | ||
</div> | ||
</limel-collapsible-section> | ||
</div> | ||
); | ||
} | ||
|
||
private renderCheckbox(index: number, option: Option) { | ||
return ( | ||
<div class="mdc-form-field "> | ||
<div | ||
class={` | ||
mdc-checkbox | ||
${this.disabled ? 'mdc-checkbox--disabled' : ''} | ||
`} | ||
> | ||
<input | ||
type="checkbox" | ||
class="mdc-checkbox__native-control" | ||
id={this.fieldId + '_' + index.toString()} | ||
value={option.value} | ||
checked={!!this.isOptionChecked(option)} | ||
disabled={this.disabled} | ||
onChange={this.onChange} | ||
/> | ||
<div class="mdc-checkbox__background"> | ||
<svg | ||
class="mdc-checkbox__checkmark" | ||
viewBox="0 0 24 24" | ||
> | ||
<path | ||
class="mdc-checkbox__checkmark-path" | ||
fill="none" | ||
d="M1.73,12.91 8.1,19.28 22.79,4.59" | ||
/> | ||
</svg> | ||
<div class="mdc-checkbox__mixedmark" /> | ||
</div> | ||
</div> | ||
<label htmlFor={this.fieldId + '_' + index.toString()}> | ||
{option.text} | ||
</label> | ||
</div> | ||
); | ||
} | ||
|
||
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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.