This commit is contained in:
syuilo 2020-09-05 15:46:02 +09:00
parent d5d29ac87c
commit 6ec0735350
2 changed files with 117 additions and 146 deletions

View File

@ -2,66 +2,51 @@
<div class="juejbjww" :class="{ focused, filled, inline, disabled }"> <div class="juejbjww" :class="{ focused, filled, inline, disabled }">
<div class="icon" ref="icon"><slot name="icon"></slot></div> <div class="icon" ref="icon"><slot name="icon"></slot></div>
<div class="input"> <div class="input">
<span class="label" ref="label"><slot></slot></span> <span class="label" ref="labelEl"><slot></slot></span>
<span class="title" ref="title"> <span class="title" ref="title">
<slot name="title"></slot> <slot name="title"></slot>
<span class="warning" v-if="invalid"><fa :icon="faExclamationCircle"/>{{ $refs.input.validationMessage }}</span> <span class="warning" v-if="invalid"><fa :icon="faExclamationCircle"/>{{ $refs.input.validationMessage }}</span>
</span> </span>
<div class="prefix" ref="prefix"><slot name="prefix"></slot></div> <div class="prefix" ref="prefixEl"><slot name="prefix"></slot></div>
<template v-if="type != 'file'"> <input v-if="debounce" ref="inputEl"
<input v-if="debounce" ref="input" v-debounce="500"
v-debounce="500" :type="type"
:type="type" v-model.lazy="v"
v-model.lazy="v" :disabled="disabled"
:disabled="disabled" :required="required"
:required="required" :readonly="readonly"
:readonly="readonly" :placeholder="placeholder"
:placeholder="placeholder" :pattern="pattern"
:pattern="pattern" :autocomplete="autocomplete"
:autocomplete="autocomplete" :spellcheck="spellcheck"
:spellcheck="spellcheck" :step="step"
:step="step" @focus="focused = true"
@focus="focused = true" @blur="focused = false"
@blur="focused = false" @keydown="$emit('keydown', $event)"
@keydown="$emit('keydown', $event)" @input="onInput"
@input="onInput" :list="id"
:list="id" >
> <input v-else ref="inputEl"
<input v-else ref="input" :type="type"
:type="type" v-model="v"
v-model="v" :disabled="disabled"
:disabled="disabled" :required="required"
:required="required" :readonly="readonly"
:readonly="readonly" :placeholder="placeholder"
:placeholder="placeholder" :pattern="pattern"
:pattern="pattern" :autocomplete="autocomplete"
:autocomplete="autocomplete" :spellcheck="spellcheck"
:spellcheck="spellcheck" :step="step"
:step="step" @focus="focused = true"
@focus="focused = true" @blur="focused = false"
@blur="focused = false" @keydown="$emit('keydown', $event)"
@keydown="$emit('keydown', $event)" @input="onInput"
@input="onInput" :list="id"
:list="id" >
> <datalist :id="id" v-if="datalist">
<datalist :id="id" v-if="datalist"> <option v-for="data in datalist" :value="data"/>
<option v-for="data in datalist" :value="data"/> </datalist>
</datalist> <div class="suffix" ref="suffixEl"><slot name="suffix"></slot></div>
</template>
<template v-else>
<input ref="input"
type="text"
:value="filePlaceholder"
readonly
@click="chooseFile"
>
<input ref="file"
type="file"
:value="value"
@change="onChangeFile"
>
</template>
<div class="suffix" ref="suffix"><slot name="suffix"></slot></div>
</div> </div>
<button class="save _textButton" v-if="save && changed" @click="() => { changed = false; save(); }">{{ $t('save') }}</button> <button class="save _textButton" v-if="save && changed" @click="() => { changed = false; save(); }">{{ $t('save') }}</button>
<div class="desc _caption"><slot name="desc"></slot></div> <div class="desc _caption"><slot name="desc"></slot></div>
@ -69,7 +54,7 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent } from 'vue'; import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs } from 'vue';
import debounce from 'v-debounce'; import debounce from 'v-debounce';
import { faExclamationCircle } from '@fortawesome/free-solid-svg-icons'; import { faExclamationCircle } from '@fortawesome/free-solid-svg-icons';
@ -136,107 +121,88 @@ export default defineComponent({
required: false, required: false,
}, },
}, },
data() { setup(props, context) {
return { const { value, type, autofocus } = toRefs(props);
v: this.value, const v = ref(value.value);
focused: false, const id = Math.random().toString(); // TODO: uuid?
invalid: false, const focused = ref(false);
changed: false, const changed = ref(false);
id: Math.random().toString(), const invalid = ref(false);
faExclamationCircle const filled = computed(() => v.value !== '' && v.value != null);
const inputEl = ref(null);
const prefixEl = ref(null);
const suffixEl = ref(null);
const labelEl = ref(null);
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
context.emit('change', ev);
}; };
},
computed: {
filled(): boolean {
return this.v !== '' && this.v != null;
},
filePlaceholder(): string | null {
if (this.type != 'file') return null;
if (this.v == null) return null;
if (typeof this.v == 'string') return this.v; watch(value, newValue => {
v.value = newValue;
});
if (Array.isArray(this.v)) { watch(v, newValue => {
return this.v.map(file => file.name).join(', '); if (type.value === 'number') {
context.emit('update:value', parseFloat(newValue));
} else { } else {
return this.v.name; context.emit('update:value', newValue);
}
}
},
watch: {
value(v) {
this.v = v;
},
v(v) {
if (this.type === 'number') {
this.$emit('update:value', parseFloat(v));
} else {
this.$emit('update:value', v);
} }
this.invalid = this.$refs.input.validity.badInput; invalid.value = inputEl.value.validity.badInput;
} });
},
mounted() {
if (this.autofocus) {
this.$nextTick(() => {
this.$refs.input.focus();
});
}
this.$nextTick(() => { onMounted(() => {
// // TODO: vue3
// 0 /*this.$on('keydown', (e: KeyboardEvent) => {
const clock = setInterval(() => { if (e.code == 'Enter') {
if (this.$refs.prefix) { this.$emit('enter');
this.$refs.label.style.left = (this.$refs.prefix.offsetLeft + this.$refs.prefix.offsetWidth) + 'px';
if (this.$refs.prefix.offsetWidth) {
this.$refs.input.style.paddingLeft = this.$refs.prefix.offsetWidth + 'px';
}
} }
if (this.$refs.suffix) { });*/
if (this.$refs.suffix.offsetWidth) {
this.$refs.input.style.paddingRight = this.$refs.suffix.offsetWidth + 'px';
}
}
}, 100);
this.$once('hook:beforeDestroy', () => { nextTick(() => {
clearInterval(clock); if (autofocus.value) {
focus();
}
//
// 0
const clock = setInterval(() => {
if (prefixEl.value) {
labelEl.value.style.left = (prefixEl.value.offsetLeft + prefixEl.value.offsetWidth) + 'px';
if (prefixEl.value.offsetWidth) {
inputEl.value.style.paddingLeft = prefixEl.value.offsetWidth + 'px';
}
}
if (suffixEl.value) {
if (suffixEl.value.offsetWidth) {
inputEl.value.style.paddingRight = suffixEl.value.offsetWidth + 'px';
}
}
}, 100);
onUnmounted(() => {
clearInterval(clock);
});
}); });
}); });
// TODO: vue3 return {
/*this.$on('keydown', (e: KeyboardEvent) => { id,
if (e.code == 'Enter') { focused,
this.$emit('enter'); changed,
} filled,
});*/ inputEl,
prefixEl,
suffixEl,
labelEl,
focus,
onInput,
faExclamationCircle,
};
}, },
methods: {
focus() {
this.$refs.input.focus();
},
togglePassword() {
if (this.type == 'password') {
this.type = 'text'
} else {
this.type = 'password'
}
},
chooseFile() {
this.$refs.file.click();
},
onChangeFile() {
this.v = Array.from((this.$refs.file as any).files);
this.$emit('update:value', this.v);
this.$emit('change', this.v);
},
onInput(ev) {
this.changed = true;
this.$emit('change', ev);
}
}
}); });
</script> </script>

View File

@ -14,6 +14,9 @@
<mk-switch v-model:value="dialogCancel"> <mk-switch v-model:value="dialogCancel">
<span>With cancel button</span> <span>With cancel button</span>
</mk-switch> </mk-switch>
<mk-switch v-model:value="dialogInput">
<span>With input field</span>
</mk-switch>
<mk-button @click="showDialog()">Show</mk-button> <mk-button @click="showDialog()">Show</mk-button>
</div> </div>
<div class="_content"> <div class="_content">
@ -47,8 +50,9 @@ export default defineComponent({
return { return {
dialogTitle: 'Hello', dialogTitle: 'Hello',
dialogBody: 'World!', dialogBody: 'World!',
dialogResult: null,
dialogCancel: false, dialogCancel: false,
dialogInput: false,
dialogResult: null,
faExclamationTriangle faExclamationTriangle
} }
}, },
@ -59,6 +63,7 @@ export default defineComponent({
title: this.dialogTitle, title: this.dialogTitle,
text: this.dialogBody, text: this.dialogBody,
showCancelButton: this.dialogCancel, showCancelButton: this.dialogCancel,
input: this.dialogInput ? {} : null
}); });
} }
} }