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