Skip to content

Instantly share code, notes, and snippets.

@Erioifpud
Last active July 18, 2018 12:59
Show Gist options
  • Select an option

  • Save Erioifpud/0a019ff96dfa7b24014055976d2e7572 to your computer and use it in GitHub Desktop.

Select an option

Save Erioifpud/0a019ff96dfa7b24014055976d2e7572 to your computer and use it in GitHub Desktop.
<template>
<div class="container">
<label v-if="label" :for="rid" :class="{ clickable: clickable }">{{ label }}</label>
<!-- textarea -->
<textarea
v-if="isTextarea"
:id="rid"
:value="data"
@input="handleInput($event.target.value)"
:disabled="disabled"
:placeholder="placeholder"
:readonly="readonly"
:required="required"
:autofocus="autofocus"
:rows="rows"
:cols="cols"
:wrap="wrap"
>
</textarea>
<!-- select -->
<select
v-else-if="isSelect"
:id="rid"
@change="handleChange($event.target.value)"
:disabled="disabled"
:required="required"
:autofocus="autofocus"
:size="size"
:multiple="multiple"
>
<option value="" selected>none</option>
<slot></slot>
</select>
<!-- text, password, radio, checkbox, date -->
<input
v-else
:type="type"
:id="rid"
:value="data"
:checked="checkedValue"
:name="name"
@input="handleInput($event.target.value)"
@click="handleClick($event.target)"
:disabled="disabled"
:min="min"
:max="max"
:placeholder="placeholder"
:readonly="readonly"
:required="required"
:autofocus="autofocus"
>
</div>
</template>
<script>
export default {
name: 'InputNew',
data () {
return {
rid: ''
}
},
model: {
prop: 'data',
event: 'change'
},
props: {
label: String,
type: {
default: 'text',
type: String
},
name: String,
value: {
default: '',
type: String
},
data: [String, Boolean],
disabled: Boolean,
max: Number,
min: Number,
placeholder: String,
readonly: Boolean,
required: Boolean,
autofocus: Boolean,
rows: Number,
cols: Number,
wrap: {
default: 'soft',
type: String
},
size: Number,
multiple: Boolean,
maxlength: Number,
pattern: String
},
methods: {
randomId() {
var result = "";
for (var i = 0; i < 6; i++) {
result += Math.floor(Math.random() * 10);
}
return `inputNew-${result}`;
},
handleInput(value) {
if (!this.checkable) {
this.$emit('change', value)
}
},
handleClick(el) {
if (this.isCheckbox) {
this.$emit('change', el.checked)
} else if (this.isRadio) {
this.$emit('change', el.value)
}
},
handleChange(value) {
this.$emit('change', value)
}
},
computed: {
isTextarea() {
return this.type === 'textarea'
},
isCheckbox() {
return this.type === 'checkbox'
},
isRadio() {
return this.type === 'radio'
},
isSelect() {
return this.type === 'select'
},
checkable() {
return this.isRadio || this.isCheckbox
},
checkedValue() {
if (this.isRadio) {
return this.value
} else {
return this.data
}
},
clickable() {
return this.isSelect || this.checkable
}
},
mounted() {
this.rid = this.randomId()
}
}
</script>
<style lang="scss" scoped>
.container {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
vertical-align: middle;
text-align: center;
color: #2c3e50;
position: relative;
display: inline-block;
margin-top: 1rem;
}
label:not(.clickable) {
position: absolute;
font-size: 0.9rem;
height: 1rem;
line-height: 1rem;
left: 0;
}
.base {
font-family: inherit;
font-size: inherit;
box-shadow: 0px 3px 1px -2px rgba(0, 0, 0, 0.2),
0px 2px 2px 0px rgba(0, 0, 0, 0.14), 0px 1px 5px 0px rgba(0, 0, 0, 0.12);
margin: 1rem 0 0.5rem 0;
-webkit-appearance: none;
border-radius: 0.125rem;
border: none;
padding: 0 0.75rem;
outline: none;
caret-color: #1f69bd;
}
input:not([type=checkbox]):not([type=radio]) {
@extend .base;
align-items: center;
min-height: 3rem;
}
textarea {
@extend .base;
caret-color: inherit;
min-height: 1rem;
min-width: 2rem;
}
select {
@extend .base;
min-height: 3rem;
background-color: #fff;
cursor: pointer;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment