Merge pull request #54 from marcosmoura/components/mdSelect

Components/md select
This commit is contained in:
Marcos Moura 2016-11-13 01:18:57 -02:00 committed by GitHub
commit 599ffc4e4e
12 changed files with 312 additions and 219 deletions

View file

@ -6,47 +6,49 @@
<md-input-container>
<label for="movie">Movie</label>
<md-select name="movie" id="movie" v-model="movie">
<md-option value="Fight Club">Fight Club</md-option>
<md-option value="Godfather II">Godfather II</md-option>
<md-option value="Godfather III">Godfather III</md-option>
<md-option value="Godfather">Godfather</md-option>
<md-option value="Godfellas">Godfellas</md-option>
<md-option value="Pulp Fiction">Pulp Fiction</md-option>
<md-option value="Scarface">Scarface</md-option>
<md-option value="fight_club">Fight Club</md-option>
<md-option value="godfather">Godfather</md-option>
<md-option value="godfather_ii">Godfather II</md-option>
<md-option value="godfather_iii">Godfather III</md-option>
<md-option value="godfellas">Godfellas</md-option>
<md-option value="pulp_fiction">Pulp Fiction</md-option>
<md-option value="scarface">Scarface</md-option>
</md-select>
</md-input-container>
<md-input-container>
<label for="country">Country</label>
<md-select name="country" id="country" v-model="country">
<md-option value="Australia">Australia</md-option>
<md-option value="Brazil">Brazil</md-option>
<md-option value="Japan">Japan</md-option>
<md-option value="United States">United States</md-option>
<md-option value="australia">Australia</md-option>
<md-option value="brazil">Brazil</md-option>
<md-option value="japan">Japan</md-option>
<md-option value="united_states">United States</md-option>
</md-select>
</md-input-container>
<md-input-container>
<label for="font">Font</label>
<md-select name="font" id="font" v-model="font">
<md-option value="Arial">Arial</md-option>
<md-option value="Calibri">Calibri</md-option>
<md-option value="Cambria">Cambria</md-option>
<md-option value="Comic Sans">Comic Sans</md-option>
<md-option value="Consolas">Consolas</md-option>
<md-option value="Courier">Courier</md-option>
<md-option value="Droid Sans">Droid Sans</md-option>
<md-option value="Georgia">Georgia</md-option>
<md-option value="Helvetica">Helvetica</md-option>
<md-option value="Impact">Impact</md-option>
<md-option value="Roboto">Roboto</md-option>
<md-option value="Segoe UI">Segoe UI</md-option>
<md-option value="Times New Roman">Times New Roman</md-option>
<md-option value="Ubuntu">Ubuntu</md-option>
<md-option value="Verdana">Verdana</md-option>
<md-option value="arial">Arial</md-option>
<md-option value="calibri">Calibri</md-option>
<md-option value="cambria">Cambria</md-option>
<md-option value="comic_sans">Comic Sans</md-option>
<md-option value="consolas">Consolas</md-option>
<md-option value="courier">Courier</md-option>
<md-option value="droid_sans">Droid Sans</md-option>
<md-option value="georgia">Georgia</md-option>
<md-option value="helvetica">Helvetica</md-option>
<md-option value="impact">Impact</md-option>
<md-option value="roboto">Roboto</md-option>
<md-option value="segoe_ui">Segoe UI</md-option>
<md-option value="times_new_roman">Times New Roman</md-option>
<md-option value="ubuntu">Ubuntu</md-option>
<md-option value="verdana">Verdana</md-option>
</md-select>
</md-input-container>
</div>
<md-button class="md-raised md-primary" @click="setPulpFiction">Set Pulp Fiction</md-button>
</demo-example>
<demo-example label="Groups" height="350">
@ -55,22 +57,51 @@
<label for="food">Food</label>
<md-select name="food" id="food" v-model="food">
<md-subheader>Fruits</md-subheader>
<md-option value="Apples">Apples</md-option>
<md-option value="Bananas">Bananas</md-option>
<md-option value="Peaches">Peaches</md-option>
<md-option value="Oranges">Oranges</md-option>
<md-option value="apples">Apples</md-option>
<md-option value="bananas">Bananas</md-option>
<md-option value="peaches">Peaches</md-option>
<md-option value="oranges">Oranges</md-option>
<md-subheader>Vegetables</md-subheader>
<md-option value="Carrots">Carrots</md-option>
<md-option value="Cucumbers">Cucumbers</md-option>
<md-option value="carrots">Carrots</md-option>
<md-option value="cucumbers">Cucumbers</md-option>
<md-subheader>Baked Goods</md-subheader>
<md-option value="Apple Pie">Apple Pie</md-option>
<md-option value="Chocolate Cake">Chocolate Cake</md-option>
<md-option value="apple_pie">Apple Pie</md-option>
<md-option value="chocolate_cake">Chocolate Cake</md-option>
</md-select>
</md-input-container>
</div>
</demo-example>
<demo-example label="Multiple" height="350">
<div class="field-group">
<md-input-container>
<label for="users">Users</label>
<md-select name="users" id="users" multiple v-model="users">
<md-subheader>Managers</md-subheader>
<md-option value="jim_halpert">Jim Halpert</md-option>
<md-option value="dwight_schrute">Dwight Schrute</md-option>
<md-option value="michael_scott">Michael Scott</md-option>
<md-subheader>Employees</md-subheader>
<md-option value="pam_beesly">Pam Beesly</md-option>
<md-option value="angela_martin">Angela Martin</md-option>
<md-option value="kelly_kapoor">Kelly Kapoor</md-option>
<md-option value="ryan_howard">Ryan Howard</md-option>
<md-option value="kevin_malone">Kevin Malone</md-option>
<md-option value="creed_bratton">Creed Bratton</md-option>
<md-option value="oscar_nunez">Oscar Nunez</md-option>
<md-option value="toby_flenderson">Toby Flenderson</md-option>
<md-option value="stanley_hudson">Stanley Hudson</md-option>
<md-option value="meredith_palmer">Meredith Palmer</md-option>
<md-option value="phyllis_lapin_vance">Phyllis Lapin-Vance</md-option>
</md-select>
</md-input-container>
</div>
{{ users }}
</demo-example>
</div>
<div slot="code">
@ -168,13 +199,17 @@
<script>
export default {
data() {
return {
movie: 'Godfather',
country: null,
font: null,
food: null
};
data: () => ({
movie: 'godfather',
country: '',
font: '',
food: '',
users: []
}),
methods: {
setPulpFiction() {
this.movie = 'pulp_fiction';
}
}
};
</script>

View file

@ -32,6 +32,7 @@ $button-icon-size: 40px;
font-style: inherit;
font-variant: inherit;
font-weight: 500;
letter-spacing: inherit;
line-height: $button-height;
text-align: center;
text-transform: uppercase;

View file

@ -14,7 +14,7 @@
let options = {
staticClass: 'md-button',
attrs: {
type: hasLink || 'button',
type: this.type || 'button',
disabled: isDisabled
},
on: {

View file

@ -1,6 +1,6 @@
<template>
<div class="md-checkbox" :class="classes">
<div class="md-checkbox-container" @click="toggleCheck" v-md-ink-ripple="disabled">
<div class="md-checkbox-container" @click.stop="toggleCheck" v-md-ink-ripple="disabled">
<input type="checkbox" :name="name" :id="id" :disabled="disabled" :value="value">
</div>
@ -28,11 +28,16 @@
computed: {
classes() {
return {
'md-checked': Boolean(this.value),
'md-checked': Boolean(this.checked),
'md-disabled': this.disabled
};
}
},
watch: {
value() {
this.checked = this.value;
}
},
methods: {
toggleCheck() {
if (!this.disabled) {

View file

@ -19,19 +19,9 @@
mdDisabled: Boolean,
mdHasPassword: Boolean
},
computed: {
classes() {
return {
'md-input-inline': this.mdInline,
'md-has-password': this.mdHasPassword,
'md-has-select': this.mdHasSelect,
'md-has-value': Boolean(this.value)
};
}
},
data() {
return {
value: null,
value: '',
input: false,
inputType: false,
showPassword: false,
@ -41,12 +31,27 @@
inputLength: 0
};
},
computed: {
hasValue() {
if (this.value.constructor === Array) {
return this.value.length > 0;
}
return Boolean(this.value);
},
classes() {
return {
'md-input-inline': this.mdInline,
'md-has-password': this.mdHasPassword,
'md-has-select': this.mdHasSelect,
'md-has-value': this.hasValue
};
}
},
methods: {
togglePasswordType() {
if (this.input.tagName.toLowerCase() === 'input') {
let type = this.input.type;
if (type === 'password') {
if (this.inputType === 'password') {
this.input.type = 'text';
this.showPassword = true;
} else {

View file

@ -22,7 +22,7 @@ $menu-base-width: 56px;
opacity: 0;
transition: width $swift-ease-out-duration $swift-ease-out-timing-function,
opacity .25s $swift-ease-in-timing-function,
clip-path .1s .073s $swift-ease-in-timing-function;
clip-path .17s .08s $swift-ease-in-timing-function;
color: rgba(#212121, .87);
&.md-direction-bottom-right {
@ -64,8 +64,8 @@ $menu-base-width: 56px;
opacity: 1;
clip-path: inset(-8px -8px -8px -8px);
transition: width $swift-ease-out-duration $swift-ease-out-timing-function,
opacity .3s $swift-ease-out-timing-function,
clip-path .2s .073s $swift-ease-out-timing-function;
opacity .4s $swift-ease-out-timing-function,
clip-path .27s .08s $swift-ease-out-timing-function;
}
}

View file

@ -18,6 +18,10 @@
mdDirection: {
type: String,
default: 'bottom right'
},
mdCloseOnSelect: {
type: Boolean,
default: true
}
},
data() {
@ -176,11 +180,11 @@
window.requestAnimationFrame(this.calculateMenuContentPos);
},
open() {
if (document.body.contains(this.menuContent)) {
document.body.removeChild(this.menuContent);
if (this.$root.$el.contains(this.menuContent)) {
this.$root.$el.removeChild(this.menuContent);
}
document.body.appendChild(this.menuContent);
this.$root.$el.appendChild(this.menuContent);
document.addEventListener('click', this.closeOnOffClick);
window.addEventListener('resize', this.recalculateOnResize);
@ -205,7 +209,7 @@
activeRipple.classList.remove('md-active');
}
document.body.removeChild(menuContent);
this.$root.$el.removeChild(menuContent);
document.removeEventListener('click', this.closeOnOffClick);
window.removeEventListener('resize', this.recalculateOnResize);
}

View file

@ -9,54 +9,70 @@
</template>
<script>
import getClosestVueParent from '../../core/utils/getClosestVueParent';
import 'element.scrollintoviewifneeded-polyfill';
export default {
props: {
disabled: Boolean
},
data() {
return {
index: 0
};
},
data: () => ({
parentContent: {},
index: 0
}),
computed: {
classes() {
return {
'md-highlighted': this.checkHighlight()
'md-highlighted': this.highlighted
};
}
},
methods: {
close() {
if (!this.disabled) {
this.$emit('click');
this.$parent.$parent.close();
}
},
checkHighlight() {
if (this.index === this.$parent.$parent.highlighted) {
highlighted() {
if (this.index === this.parentContent.highlighted) {
if (this.disabled) {
if (this.$parent.$parent.oldHighlight > this.$parent.$parent.highlighted) {
this.$parent.$parent.highlighted--;
if (this.parentContent.oldHighlight > this.parentContent.highlighted) {
this.parentContent.highlighted--;
} else {
this.$parent.$parent.highlighted++;
this.parentContent.highlighted++;
}
}
if (this.index === 1) {
this.parentContent.$el.scrollTop = 0;
} else if (this.index === this.parentContent.itemsAmount) {
this.parentContent.$el.scrollTop = this.parentContent.$el.scrollHeight;
} else {
this.$el.scrollIntoViewIfNeeded(false);
}
return true;
}
return false;
}
},
methods: {
close() {
if (!this.disabled) {
if (this.parentMenu.mdCloseOnSelect) {
this.parentContent.close();
}
this.$emit('click');
}
}
},
mounted() {
if (!this.$parent.$el.classList.contains('md-list')) {
this.parentContent = getClosestVueParent(this.$parent, 'md-menu-content');
this.parentMenu = getClosestVueParent(this.$parent, 'md-menu');
if (!this.parentContent) {
this.$destroy();
throw new Error('You must wrap the md-menu-item in a md-menu-content');
}
this.$parent.$parent.itemsAmount++;
this.index = this.$parent.$parent.itemsAmount;
this.parentContent.itemsAmount++;
this.index = this.parentContent.itemsAmount;
}
};
</script>

View file

@ -1,73 +1,73 @@
<template>
<div
<md-menu-item
class="md-option"
:class="classes"
@click="selectOption"
@mouseenter="setHighlight"
@keydown.enter="selectOption"
v-md-ink-ripple
tabindex="-1">
<span>
<md-checkbox v-model="check" v-if="parentSelect.multiple">
<span ref="item">
<slot></slot>
</span>
</md-checkbox>
<span ref="item" v-else>
<slot></slot>
</span>
</div>
</md-menu-item>
</template>
<script>
import 'element.scrollintoviewifneeded-polyfill';
import getClosestVueParent from '../../core/utils/getClosestVueParent';
export default {
props: {
value: [String, Boolean, Number]
},
data() {
return {
index: 0
};
},
computed: {
classes() {
return {
'md-highlighted': this.hasHighlight()
};
}
},
watch: {
classes() {
if (this.hasHighlight()) {
this.$el.focus();
this.$el.scrollIntoViewIfNeeded(false);
data: () => ({
parentSelect: {},
check: false,
index: 0
}),
methods: {
selectOption() {
if (!this.parentSelect.multiple) {
this.parentSelect.selectOption(this.value, this.$refs.item.textContent);
} else {
this.check = !this.check;
}
},
selectIfValueMatches() {
if (this.value === this.parentSelect.value) {
this.selectOption();
}
}
},
methods: {
setHighlight() {
this.$parent.highlightOption(this.index);
},
hasHighlight() {
return this.index === this.$parent.highlighted;
},
selectOption() {
if (this.hasHighlight()) {
if (this.$parent.$el.classList.contains('md-select')) {
this.$parent.selectOption(this.value);
} else {
this.$parent.$parent.selectOption(this.value);
}
watch: {
check(check) {
if (check) {
this.parentSelect.selectMultiple(this.index, this.value, this.$refs.item.textContent);
} else {
this.parentSelect.selectMultiple(this.index);
}
}
},
mounted() {
let parentClasses = this.$parent.$el.classList;
this.parentSelect = getClosestVueParent(this.$parent, 'md-select');
this.parentContent = getClosestVueParent(this.$parent, 'md-menu-content');
if (!parentClasses.contains('md-select')) {
this.$destroy();
throw new Error('You should wrap the md-option in a md-select');
if (!this.parentSelect) {
throw new Error('You must wrap the md-option in a md-select');
}
this.$parent.optionsAmount++;
this.index = this.$parent.optionsAmount;
this.parentSelect.optionsAmount++;
this.index = this.parentSelect.optionsAmount;
this.parentSelect.options[this.index] = {};
this.$watch(() => {
return this.parentSelect.value;
}, this.selectIfValueMatches);
this.selectIfValueMatches();
}
};
</script>

View file

@ -46,6 +46,13 @@
left: -999em;
}
.md-menu {
width: 100%;
height: 100%;
display: block;
position: relative;
}
.md-select-value {
width: 100%;
height: 100%;
@ -65,7 +72,6 @@
min-width: 156px;
max-width: 100%;
min-height: 48px;
max-height: 256px;
display: flex;
flex-flow: column;
justify-content: stretch;
@ -113,30 +119,42 @@
}
}
.md-option {
height: 48px;
min-height: 48px;
padding: 0 4px 0 16px;
display: flex;
flex-flow: column;
justify-content: center;
overflow: hidden;
cursor: pointer;
position: relative;
transform: translate3D(0, 0, 0);
transition: $swift-ease-out;
font-size: 16px;
line-height: 1.2em;
text-overflow: ellipsis;
white-space: nowrap;
.md-select-content {
width: auto;
max-height: 256px;
&.md-highlighted {
background-color: rgba(#000, .12);
&.md-direction-bottom-right {
margin-top: -15px;
margin-left: -16px;
}
span {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
&.md-direction-bottom-left {
margin-top: -15px;
margin-left: 16px;
}
&.md-direction-top-right {
margin-top: 15px;
margin-left: -16px;
}
&.md-direction-top-left {
margin-top: 15px;
margin-left: 16px;
}
.md-menu-item .md-list-item-holder {
overflow: visible;
justify-content: flex-start;
}
&.md-multiple {
.md-checkbox {
margin: 0;
}
.md-checkbox-label {
padding-left: 16px;
}
}
}

View file

@ -1,26 +1,12 @@
<template>
<div
class="md-select"
:class="classes"
:tabindex="disabled ? null : '0'">
<span
class="md-select-value"
@click="open"
@keydown.enter.prevent="open"
ref="value">{{ value }}</span>
<div class="md-select" :class="classes">
<md-menu :md-close-on-select="!multiple">
<span class="md-select-value" md-menu-trigger ref="value">{{ selectedValue || multiplevalue }}</span>
<div
class="md-select-menu"
tabindex="-1"
ref="menu"
@keydown.esc.prevent="close"
@keydown.tab.prevent="close"
@keydown.up.prevent="highlightOption(highlighted - 1)"
@keydown.down.prevent="highlightOption(highlighted + 1)">
<div class="md-select-menu-container">
<md-menu-content class="md-select-content" :class="contentClasses">
<slot></slot>
</div>
</div>
</md-menu-content>
</md-menu>
<select :name="name" :id="id" :required="required" tabindex="-1">
<option :value="value">{{ value }}</option>
@ -31,77 +17,83 @@
<style lang="scss" src="./mdSelect.scss"></style>
<script>
import getClosestVueParent from '../../core/utils/getClosestVueParent';
export default {
props: {
name: String,
required: Boolean,
value: [String, Number, Boolean],
multiple: Boolean,
value: [String, Number, Array],
id: String,
disabled: Boolean
},
data() {
return {
active: false,
highlighted: false,
selectedValue: null,
multiplevalue: null,
options: {},
optionsAmount: 0
};
},
computed: {
classes() {
return {
'md-disabled': this.disabled,
'md-active': this.active
'md-disabled': this.disabled
};
},
contentClasses() {
return {
'md-multiple': this.multiple
};
}
},
methods: {
open() {
this.active = true;
document.addEventListener('click', this.closeOnOffClick);
this.$refs.menu.focus();
},
close() {
if (this.active) {
this.$refs.menu.blur();
this.active = false;
document.removeEventListener('click', this.closeOnOffClick);
this.$refs.value.focus();
}
},
closeOnOffClick(event) {
if (!this.$el.contains(event.target)) {
this.close();
}
},
highlightOption(factor) {
if (factor >= 1 && factor <= this.optionsAmount) {
this.highlighted = factor;
} else {
this.highlighted = 1;
}
},
selectOption(value) {
this.close();
this.$parent.setValue(value);
changeValue(value, parentValue) {
this.$emit('change', value);
this.$emit('input', value);
if (this.parentContainer) {
this.$parent.setValue(parentValue || value);
}
},
selectMultiple(index, value, text) {
let output = [];
let values = [];
this.options[index] = {
value,
text
};
for (var key in this.options) {
if (this.options.hasOwnProperty(key) && this.options[key].text) {
output.push(this.options[key].text);
values.push(this.options[key].value);
}
}
this.multiplevalue = output.join(', ');
this.changeValue(values, this.multiplevalue);
},
selectOption(value, text) {
this.selectedValue = text;
this.changeValue(value);
}
},
mounted() {
if (!this.$parent.$el.classList.contains('md-input-container')) {
this.$destroy();
this.parentContainer = this.parentContent = getClosestVueParent(this.$parent, 'md-input-container');
throw new Error('You should wrap the md-select in a md-input-container');
if (this.parentContainer) {
this.parentContainer.setValue(this.value);
this.parentContainer.hasSelect = true;
}
this.$parent.setValue(this.value);
this.$parent.hasSelect = true;
},
beforeDestroy() {
this.$parent.setValue(null);
this.$parent.hasSelect = false;
document.removeEventListener('click', this.closeOnOffClick);
if (this.parentContainer) {
this.parentContainer.setValue('');
this.parentContainer.hasSelect = false;
}
}
};
</script>

View file

@ -0,0 +1,17 @@
let getClosestVueParent = ($parent, cssClass) => {
if (!$parent.$el) {
return false;
}
if ($parent.$el.classList.contains(cssClass)) {
return $parent;
}
if ($parent._uid === 0) {
return false;
}
return getClosestVueParent($parent.$parent, cssClass);
};
export default getClosestVueParent;