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