2020-11-25 04:31:34 -08:00
|
|
|
<template>
|
2021-09-29 08:50:45 -07:00
|
|
|
<div class="matxzzsk">
|
|
|
|
<div class="label" @click="focus"><slot name="label"></slot></div>
|
|
|
|
<div class="input" :class="{ inline, disabled, focused }">
|
2021-11-19 02:36:12 -08:00
|
|
|
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
|
2022-06-25 11:12:58 -07:00
|
|
|
<input
|
|
|
|
ref="inputEl"
|
2021-09-29 08:50:45 -07:00
|
|
|
v-model="v"
|
2021-12-25 08:42:50 -08:00
|
|
|
v-adaptive-border
|
2021-11-19 02:36:12 -08:00
|
|
|
:type="type"
|
2021-09-29 08:50:45 -07:00
|
|
|
:disabled="disabled"
|
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:pattern="pattern"
|
|
|
|
:autocomplete="autocomplete"
|
|
|
|
:spellcheck="spellcheck"
|
|
|
|
:step="step"
|
2021-11-19 02:36:12 -08:00
|
|
|
:list="id"
|
2021-09-29 08:50:45 -07:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
|
|
|
@keydown="onKeydown($event)"
|
|
|
|
@input="onInput"
|
|
|
|
>
|
2021-11-19 02:36:12 -08:00
|
|
|
<datalist v-if="datalist" :id="id">
|
2021-09-29 08:50:45 -07:00
|
|
|
<option v-for="data in datalist" :value="data"/>
|
|
|
|
</datalist>
|
2021-11-19 02:36:12 -08:00
|
|
|
<div ref="suffixEl" class="suffix"><slot name="suffix"></slot></div>
|
2020-11-25 04:31:34 -08:00
|
|
|
</div>
|
2021-09-29 08:50:45 -07:00
|
|
|
<div class="caption"><slot name="caption"></slot></div>
|
2021-02-12 19:28:26 -08:00
|
|
|
|
2022-12-19 02:01:30 -08:00
|
|
|
<MkButton v-if="manualSave && changed" primary class="save" @click="updated"><i class="ti ti-check"></i> {{ i18n.ts.save }}</MkButton>
|
2021-09-29 08:50:45 -07:00
|
|
|
</div>
|
2020-11-25 04:31:34 -08:00
|
|
|
</template>
|
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 06:09:41 -08:00
|
|
|
import { onMounted, nextTick, ref, shallowRef, watch, computed, toRefs } from 'vue';
|
2021-09-29 08:50:45 -07:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2022-09-06 02:21:49 -07:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2022-06-25 11:12:58 -07:00
|
|
|
import { useInterval } from '@/scripts/use-interval';
|
2022-07-20 06:24:26 -07:00
|
|
|
import { i18n } from '@/i18n';
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
const props = defineProps<{
|
|
|
|
modelValue: string | number;
|
2023-02-09 00:54:30 -08:00
|
|
|
type?: 'text' | 'number' | 'password' | 'email' | 'url' | 'date' | 'time' | 'search' | 'datetime-local';
|
2022-06-27 23:59:49 -07:00
|
|
|
required?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
pattern?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
autofocus?: boolean;
|
|
|
|
autocomplete?: boolean;
|
|
|
|
spellcheck?: boolean;
|
|
|
|
step?: any;
|
|
|
|
datalist?: string[];
|
|
|
|
inline?: boolean;
|
|
|
|
debounce?: boolean;
|
|
|
|
manualSave?: boolean;
|
|
|
|
small?: boolean;
|
|
|
|
large?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'change', _ev: KeyboardEvent): void;
|
|
|
|
(ev: 'keydown', _ev: KeyboardEvent): void;
|
|
|
|
(ev: 'enter'): void;
|
|
|
|
(ev: 'update:modelValue', value: string | number): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const { modelValue, type, autofocus } = toRefs(props);
|
|
|
|
const v = ref(modelValue.value);
|
|
|
|
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);
|
2023-01-02 17:12:37 -08:00
|
|
|
const inputEl = shallowRef<HTMLElement>();
|
|
|
|
const prefixEl = shallowRef<HTMLElement>();
|
|
|
|
const suffixEl = shallowRef<HTMLElement>();
|
2022-06-27 23:59:49 -07:00
|
|
|
const height =
|
2023-01-14 21:18:45 -08:00
|
|
|
props.small ? 33 :
|
|
|
|
props.large ? 39 :
|
|
|
|
36;
|
2022-06-27 23:59:49 -07:00
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev: KeyboardEvent) => {
|
|
|
|
changed.value = true;
|
|
|
|
emit('change', ev);
|
|
|
|
};
|
|
|
|
const onKeydown = (ev: KeyboardEvent) => {
|
2023-01-27 22:15:29 -08:00
|
|
|
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
|
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
emit('keydown', ev);
|
|
|
|
|
|
|
|
if (ev.code === 'Enter') {
|
|
|
|
emit('enter');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
if (type.value === 'number') {
|
|
|
|
emit('update:modelValue', parseFloat(v.value));
|
|
|
|
} else {
|
|
|
|
emit('update:modelValue', v.value);
|
|
|
|
}
|
|
|
|
};
|
2021-09-29 08:50:45 -07:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
const debouncedUpdated = debounce(1000, updated);
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
watch(modelValue, newValue => {
|
|
|
|
v.value = newValue;
|
|
|
|
});
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
watch(v, newValue => {
|
|
|
|
if (!props.manualSave) {
|
|
|
|
if (props.debounce) {
|
|
|
|
debouncedUpdated();
|
|
|
|
} else {
|
|
|
|
updated();
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
invalid.value = inputEl.value.validity.badInput;
|
|
|
|
});
|
2022-06-25 11:12:58 -07:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
// このコンポーネントが作成された時、非表示状態である場合がある
|
|
|
|
// 非表示状態だと要素の幅などは0になってしまうので、定期的に計算する
|
|
|
|
useInterval(() => {
|
|
|
|
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, {
|
|
|
|
immediate: true,
|
|
|
|
afterMounted: true,
|
|
|
|
});
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2022-06-27 23:59:49 -07:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
});
|
2020-11-25 04:31:34 -08:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-12-27 01:29:39 -08:00
|
|
|
<style lang="scss" scoped>
|
2021-09-29 08:50:45 -07:00
|
|
|
.matxzzsk {
|
|
|
|
> .label {
|
|
|
|
font-size: 0.85em;
|
2021-11-28 03:07:37 -08:00
|
|
|
padding: 0 0 8px 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .caption {
|
2021-11-28 03:07:37 -08:00
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 8px 0 0 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
color: var(--fgTransparentWeak);
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .input {
|
|
|
|
position: relative;
|
|
|
|
|
|
|
|
> input {
|
2021-09-29 08:50:45 -07:00
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
2020-11-25 04:31:34 -08:00
|
|
|
display: block;
|
2022-06-27 23:59:49 -07:00
|
|
|
height: v-bind("height + 'px'");
|
2020-11-25 04:31:34 -08:00
|
|
|
width: 100%;
|
|
|
|
margin: 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
padding: 0 12px;
|
2020-11-25 04:31:34 -08:00
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
|
|
|
font-size: 1em;
|
2021-09-29 08:50:45 -07:00
|
|
|
color: var(--fg);
|
2021-12-25 08:42:50 -08:00
|
|
|
background: var(--panel);
|
|
|
|
border: solid 1px var(--panel);
|
2021-09-29 08:50:45 -07:00
|
|
|
border-radius: 6px;
|
2020-11-25 04:31:34 -08:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
2021-09-29 08:50:45 -07:00
|
|
|
transition: border-color 0.1s ease-out;
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2021-09-29 08:50:45 -07:00
|
|
|
&:hover {
|
2021-12-25 08:42:50 -08:00
|
|
|
border-color: var(--inputBorderHover) !important;
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix,
|
|
|
|
> .suffix {
|
2021-09-29 08:50:45 -07:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2020-11-25 04:31:34 -08:00
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
top: 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
padding: 0 12px;
|
2020-11-25 04:31:34 -08:00
|
|
|
font-size: 1em;
|
2022-06-27 23:59:49 -07:00
|
|
|
height: v-bind("height + 'px'");
|
2020-11-25 04:31:34 -08:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> * {
|
|
|
|
display: inline-block;
|
|
|
|
min-width: 16px;
|
|
|
|
max-width: 150px;
|
2021-03-02 05:57:16 -08:00
|
|
|
overflow: hidden;
|
2020-11-25 04:31:34 -08:00
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix {
|
|
|
|
left: 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
padding-right: 6px;
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
> .suffix {
|
|
|
|
right: 0;
|
2021-09-29 08:50:45 -07:00
|
|
|
padding-left: 6px;
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
|
2021-09-29 08:50:45 -07:00
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.focused {
|
|
|
|
> input {
|
2021-12-25 08:42:50 -08:00
|
|
|
border-color: var(--accent) !important;
|
2021-09-29 08:50:45 -07:00
|
|
|
//box-shadow: 0 0 0 4px var(--focus);
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2021-09-29 08:50:45 -07:00
|
|
|
&.disabled {
|
|
|
|
opacity: 0.7;
|
2020-11-25 04:31:34 -08:00
|
|
|
|
2021-09-29 08:50:45 -07:00
|
|
|
&, * {
|
|
|
|
cursor: not-allowed !important;
|
|
|
|
}
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
}
|
2021-11-28 03:07:37 -08:00
|
|
|
|
|
|
|
> .save {
|
|
|
|
margin: 8px 0 0 0;
|
|
|
|
}
|
2020-11-25 04:31:34 -08:00
|
|
|
}
|
|
|
|
</style>
|