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> <md-input-container>
<label for="movie">Movie</label> <label for="movie">Movie</label>
<md-select name="movie" id="movie" v-model="movie"> <md-select name="movie" id="movie" v-model="movie">
<md-option value="Fight Club">Fight Club</md-option> <md-option value="fight_club">Fight Club</md-option>
<md-option value="Godfather II">Godfather II</md-option> <md-option value="godfather">Godfather</md-option>
<md-option value="Godfather III">Godfather III</md-option> <md-option value="godfather_ii">Godfather II</md-option>
<md-option value="Godfather">Godfather</md-option> <md-option value="godfather_iii">Godfather III</md-option>
<md-option value="Godfellas">Godfellas</md-option> <md-option value="godfellas">Godfellas</md-option>
<md-option value="Pulp Fiction">Pulp Fiction</md-option> <md-option value="pulp_fiction">Pulp Fiction</md-option>
<md-option value="Scarface">Scarface</md-option> <md-option value="scarface">Scarface</md-option>
</md-select> </md-select>
</md-input-container> </md-input-container>
<md-input-container> <md-input-container>
<label for="country">Country</label> <label for="country">Country</label>
<md-select name="country" id="country" v-model="country"> <md-select name="country" id="country" v-model="country">
<md-option value="Australia">Australia</md-option> <md-option value="australia">Australia</md-option>
<md-option value="Brazil">Brazil</md-option> <md-option value="brazil">Brazil</md-option>
<md-option value="Japan">Japan</md-option> <md-option value="japan">Japan</md-option>
<md-option value="United States">United States</md-option> <md-option value="united_states">United States</md-option>
</md-select> </md-select>
</md-input-container> </md-input-container>
<md-input-container> <md-input-container>
<label for="font">Font</label> <label for="font">Font</label>
<md-select name="font" id="font" v-model="font"> <md-select name="font" id="font" v-model="font">
<md-option value="Arial">Arial</md-option> <md-option value="arial">Arial</md-option>
<md-option value="Calibri">Calibri</md-option> <md-option value="calibri">Calibri</md-option>
<md-option value="Cambria">Cambria</md-option> <md-option value="cambria">Cambria</md-option>
<md-option value="Comic Sans">Comic Sans</md-option> <md-option value="comic_sans">Comic Sans</md-option>
<md-option value="Consolas">Consolas</md-option> <md-option value="consolas">Consolas</md-option>
<md-option value="Courier">Courier</md-option> <md-option value="courier">Courier</md-option>
<md-option value="Droid Sans">Droid Sans</md-option> <md-option value="droid_sans">Droid Sans</md-option>
<md-option value="Georgia">Georgia</md-option> <md-option value="georgia">Georgia</md-option>
<md-option value="Helvetica">Helvetica</md-option> <md-option value="helvetica">Helvetica</md-option>
<md-option value="Impact">Impact</md-option> <md-option value="impact">Impact</md-option>
<md-option value="Roboto">Roboto</md-option> <md-option value="roboto">Roboto</md-option>
<md-option value="Segoe UI">Segoe UI</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="times_new_roman">Times New Roman</md-option>
<md-option value="Ubuntu">Ubuntu</md-option> <md-option value="ubuntu">Ubuntu</md-option>
<md-option value="Verdana">Verdana</md-option> <md-option value="verdana">Verdana</md-option>
</md-select> </md-select>
</md-input-container> </md-input-container>
</div> </div>
<md-button class="md-raised md-primary" @click="setPulpFiction">Set Pulp Fiction</md-button>
</demo-example> </demo-example>
<demo-example label="Groups" height="350"> <demo-example label="Groups" height="350">
@ -55,22 +57,51 @@
<label for="food">Food</label> <label for="food">Food</label>
<md-select name="food" id="food" v-model="food"> <md-select name="food" id="food" v-model="food">
<md-subheader>Fruits</md-subheader> <md-subheader>Fruits</md-subheader>
<md-option value="Apples">Apples</md-option> <md-option value="apples">Apples</md-option>
<md-option value="Bananas">Bananas</md-option> <md-option value="bananas">Bananas</md-option>
<md-option value="Peaches">Peaches</md-option> <md-option value="peaches">Peaches</md-option>
<md-option value="Oranges">Oranges</md-option> <md-option value="oranges">Oranges</md-option>
<md-subheader>Vegetables</md-subheader> <md-subheader>Vegetables</md-subheader>
<md-option value="Carrots">Carrots</md-option> <md-option value="carrots">Carrots</md-option>
<md-option value="Cucumbers">Cucumbers</md-option> <md-option value="cucumbers">Cucumbers</md-option>
<md-subheader>Baked Goods</md-subheader> <md-subheader>Baked Goods</md-subheader>
<md-option value="Apple Pie">Apple Pie</md-option> <md-option value="apple_pie">Apple Pie</md-option>
<md-option value="Chocolate Cake">Chocolate Cake</md-option> <md-option value="chocolate_cake">Chocolate Cake</md-option>
</md-select> </md-select>
</md-input-container> </md-input-container>
</div> </div>
</demo-example> </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>
<div slot="code"> <div slot="code">
@ -168,13 +199,17 @@
<script> <script>
export default { export default {
data() { data: () => ({
return { movie: 'godfather',
movie: 'Godfather', country: '',
country: null, font: '',
font: null, food: '',
food: null users: []
}; }),
methods: {
setPulpFiction() {
this.movie = 'pulp_fiction';
}
} }
}; };
</script> </script>

View file

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

View file

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

View file

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

View file

@ -19,19 +19,9 @@
mdDisabled: Boolean, mdDisabled: Boolean,
mdHasPassword: 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() { data() {
return { return {
value: null, value: '',
input: false, input: false,
inputType: false, inputType: false,
showPassword: false, showPassword: false,
@ -41,12 +31,27 @@
inputLength: 0 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: { methods: {
togglePasswordType() { togglePasswordType() {
if (this.input.tagName.toLowerCase() === 'input') { if (this.input.tagName.toLowerCase() === 'input') {
let type = this.input.type; if (this.inputType === 'password') {
if (type === 'password') {
this.input.type = 'text'; this.input.type = 'text';
this.showPassword = true; this.showPassword = true;
} else { } else {

View file

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

View file

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

View file

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

View file

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

View file

@ -46,6 +46,13 @@
left: -999em; left: -999em;
} }
.md-menu {
width: 100%;
height: 100%;
display: block;
position: relative;
}
.md-select-value { .md-select-value {
width: 100%; width: 100%;
height: 100%; height: 100%;
@ -65,7 +72,6 @@
min-width: 156px; min-width: 156px;
max-width: 100%; max-width: 100%;
min-height: 48px; min-height: 48px;
max-height: 256px;
display: flex; display: flex;
flex-flow: column; flex-flow: column;
justify-content: stretch; justify-content: stretch;
@ -113,30 +119,42 @@
} }
} }
.md-option { .md-select-content {
height: 48px; width: auto;
min-height: 48px; max-height: 256px;
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-highlighted { &.md-direction-bottom-right {
background-color: rgba(#000, .12); margin-top: -15px;
margin-left: -16px;
} }
span { &.md-direction-bottom-left {
overflow: hidden; margin-top: -15px;
text-overflow: ellipsis; margin-left: 16px;
white-space: nowrap; }
&.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> <template>
<div <div class="md-select" :class="classes">
class="md-select" <md-menu :md-close-on-select="!multiple">
:class="classes" <span class="md-select-value" md-menu-trigger ref="value">{{ selectedValue || multiplevalue }}</span>
:tabindex="disabled ? null : '0'">
<span
class="md-select-value"
@click="open"
@keydown.enter.prevent="open"
ref="value">{{ value }}</span>
<div <md-menu-content class="md-select-content" :class="contentClasses">
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">
<slot></slot> <slot></slot>
</div> </md-menu-content>
</div> </md-menu>
<select :name="name" :id="id" :required="required" tabindex="-1"> <select :name="name" :id="id" :required="required" tabindex="-1">
<option :value="value">{{ value }}</option> <option :value="value">{{ value }}</option>
@ -31,77 +17,83 @@
<style lang="scss" src="./mdSelect.scss"></style> <style lang="scss" src="./mdSelect.scss"></style>
<script> <script>
import getClosestVueParent from '../../core/utils/getClosestVueParent';
export default { export default {
props: { props: {
name: String, name: String,
required: Boolean, required: Boolean,
value: [String, Number, Boolean], multiple: Boolean,
value: [String, Number, Array],
id: String, id: String,
disabled: Boolean disabled: Boolean
}, },
data() { data() {
return { return {
active: false, selectedValue: null,
highlighted: false, multiplevalue: null,
options: {},
optionsAmount: 0 optionsAmount: 0
}; };
}, },
computed: { computed: {
classes() { classes() {
return { return {
'md-disabled': this.disabled, 'md-disabled': this.disabled
'md-active': this.active };
},
contentClasses() {
return {
'md-multiple': this.multiple
}; };
} }
}, },
methods: { methods: {
open() { changeValue(value, parentValue) {
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);
this.$emit('change', value); this.$emit('change', value);
this.$emit('input', 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() { mounted() {
if (!this.$parent.$el.classList.contains('md-input-container')) { this.parentContainer = this.parentContent = getClosestVueParent(this.$parent, 'md-input-container');
this.$destroy();
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() { beforeDestroy() {
this.$parent.setValue(null); if (this.parentContainer) {
this.$parent.hasSelect = false; this.parentContainer.setValue('');
this.parentContainer.hasSelect = false;
document.removeEventListener('click', this.closeOnOffClick); }
} }
}; };
</script> </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;