2020-11-25 12:31:34 +00:00
|
|
|
<template>
|
2021-09-29 15:50:45 +00:00
|
|
|
<div class="matxzzsk">
|
|
|
|
<div class="label" @click="focus"><slot name="label"></slot></div>
|
|
|
|
<div class="input" :class="{ inline, disabled, focused }">
|
|
|
|
<div class="prefix" ref="prefixEl"><slot name="prefix"></slot></div>
|
|
|
|
<input ref="inputEl"
|
|
|
|
:type="type"
|
|
|
|
v-model="v"
|
|
|
|
:disabled="disabled"
|
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:pattern="pattern"
|
|
|
|
:autocomplete="autocomplete"
|
|
|
|
:spellcheck="spellcheck"
|
|
|
|
:step="step"
|
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
|
|
|
@keydown="onKeydown($event)"
|
|
|
|
@input="onInput"
|
|
|
|
:list="id"
|
|
|
|
>
|
|
|
|
<datalist :id="id" v-if="datalist">
|
|
|
|
<option v-for="data in datalist" :value="data"/>
|
|
|
|
</datalist>
|
|
|
|
<div class="suffix" ref="suffixEl"><slot name="suffix"></slot></div>
|
2020-11-25 12:31:34 +00:00
|
|
|
</div>
|
2021-09-29 15:50:45 +00:00
|
|
|
<div class="caption"><slot name="caption"></slot></div>
|
2021-02-13 03:28:26 +00:00
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
<MkButton v-if="manualSave && changed" @click="updated" primary><i class="fas fa-save"></i> {{ $ts.save }}</MkButton>
|
|
|
|
</div>
|
2020-11-25 12:31:34 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs } from 'vue';
|
2021-10-21 21:23:23 +00:00
|
|
|
import MkButton from '@client/components/ui/button.vue';
|
2021-09-29 15:50:45 +00:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2020-11-25 12:31:34 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2021-02-13 03:28:26 +00:00
|
|
|
components: {
|
2021-09-29 15:50:45 +00:00
|
|
|
MkButton,
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
|
2020-11-25 12:31:34 +00:00
|
|
|
props: {
|
2021-09-29 15:50:45 +00:00
|
|
|
modelValue: {
|
|
|
|
required: true
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
|
|
|
type: {
|
|
|
|
type: String,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
required: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
readonly: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
pattern: {
|
|
|
|
type: String,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
autofocus: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
autocomplete: {
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
spellcheck: {
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
step: {
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
datalist: {
|
|
|
|
type: Array,
|
|
|
|
required: false,
|
|
|
|
},
|
|
|
|
inline: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
debounce: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2021-02-13 03:28:26 +00:00
|
|
|
manualSave: {
|
|
|
|
type: Boolean,
|
2020-11-25 12:31:34 +00:00
|
|
|
required: false,
|
2021-02-13 03:28:26 +00:00
|
|
|
default: false
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
emits: ['change', 'keydown', 'enter', 'update:modelValue'],
|
|
|
|
|
2020-11-25 12:31:34 +00:00
|
|
|
setup(props, context) {
|
2021-09-29 15:50:45 +00:00
|
|
|
const { modelValue, type, autofocus } = toRefs(props);
|
|
|
|
const v = ref(modelValue.value);
|
2020-11-25 12:31:34 +00:00
|
|
|
const id = Math.random().toString(); // TODO: uuid?
|
|
|
|
const focused = ref(false);
|
|
|
|
const changed = ref(false);
|
|
|
|
const invalid = ref(false);
|
|
|
|
const filled = computed(() => v.value !== '' && v.value != null);
|
|
|
|
const inputEl = ref(null);
|
|
|
|
const prefixEl = ref(null);
|
|
|
|
const suffixEl = ref(null);
|
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
context.emit('change', ev);
|
|
|
|
};
|
|
|
|
const onKeydown = (ev: KeyboardEvent) => {
|
|
|
|
context.emit('keydown', ev);
|
|
|
|
|
|
|
|
if (ev.code === 'Enter') {
|
|
|
|
context.emit('enter');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-13 03:28:26 +00:00
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
if (type?.value === 'number') {
|
2021-09-29 15:50:45 +00:00
|
|
|
context.emit('update:modelValue', parseFloat(v.value));
|
2021-02-13 03:28:26 +00:00
|
|
|
} else {
|
2021-09-29 15:50:45 +00:00
|
|
|
context.emit('update:modelValue', v.value);
|
2021-02-13 03:28:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
const debouncedUpdated = debounce(1000, updated);
|
|
|
|
|
|
|
|
watch(modelValue, newValue => {
|
2020-11-25 12:31:34 +00:00
|
|
|
v.value = newValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(v, newValue => {
|
2021-02-13 03:28:26 +00:00
|
|
|
if (!props.manualSave) {
|
2021-09-29 15:50:45 +00:00
|
|
|
if (props.debounce) {
|
|
|
|
debouncedUpdated();
|
|
|
|
} else {
|
|
|
|
updated();
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
invalid.value = inputEl.value.validity.badInput;
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// このコンポーネントが作成された時、非表示状態である場合がある
|
|
|
|
// 非表示状態だと要素の幅などは0になってしまうので、定期的に計算する
|
|
|
|
const clock = setInterval(() => {
|
|
|
|
if (prefixEl.value) {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
v,
|
|
|
|
focused,
|
|
|
|
invalid,
|
|
|
|
changed,
|
|
|
|
filled,
|
|
|
|
inputEl,
|
|
|
|
prefixEl,
|
|
|
|
suffixEl,
|
|
|
|
focus,
|
|
|
|
onInput,
|
|
|
|
onKeydown,
|
2021-02-13 03:28:26 +00:00
|
|
|
updated,
|
2020-11-25 12:31:34 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-09-29 15:50:45 +00:00
|
|
|
.matxzzsk {
|
|
|
|
> .label {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 0 0 8px 12px;
|
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .caption {
|
|
|
|
font-size: 0.8em;
|
|
|
|
padding: 8px 0 0 12px;
|
|
|
|
color: var(--fgTransparentWeak);
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .input {
|
2021-09-29 15:50:45 +00:00
|
|
|
$height: 42px;
|
2020-11-25 12:31:34 +00:00
|
|
|
position: relative;
|
|
|
|
|
|
|
|
> input {
|
2021-09-29 15:50:45 +00:00
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
2020-11-25 12:31:34 +00:00
|
|
|
display: block;
|
|
|
|
height: $height;
|
|
|
|
width: 100%;
|
|
|
|
margin: 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
padding: 0 12px;
|
2020-11-25 12:31:34 +00:00
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
|
|
|
font-size: 1em;
|
2021-09-29 15:50:45 +00:00
|
|
|
color: var(--fg);
|
|
|
|
background: var(--panel);
|
|
|
|
border: solid 0.5px var(--inputBorder);
|
|
|
|
border-radius: 6px;
|
2020-11-25 12:31:34 +00:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
2021-09-29 15:50:45 +00:00
|
|
|
transition: border-color 0.1s ease-out;
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
&:hover {
|
|
|
|
border-color: var(--inputBorderHover);
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix,
|
|
|
|
> .suffix {
|
2021-09-29 15:50:45 +00:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2020-11-25 12:31:34 +00:00
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
top: 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
padding: 0 12px;
|
2020-11-25 12:31:34 +00:00
|
|
|
font-size: 1em;
|
2021-09-29 15:50:45 +00:00
|
|
|
height: $height;
|
2020-11-25 12:31:34 +00:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> * {
|
|
|
|
display: inline-block;
|
|
|
|
min-width: 16px;
|
|
|
|
max-width: 150px;
|
2021-03-02 13:57:16 +00:00
|
|
|
overflow: hidden;
|
2020-11-25 12:31:34 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix {
|
|
|
|
left: 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
padding-right: 6px;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
> .suffix {
|
|
|
|
right: 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
padding-left: 6px;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.focused {
|
|
|
|
> input {
|
|
|
|
border-color: var(--accent);
|
|
|
|
//box-shadow: 0 0 0 4px var(--focus);
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
&.disabled {
|
|
|
|
opacity: 0.7;
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
&, * {
|
|
|
|
cursor: not-allowed !important;
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|