2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-11-25 13:31:34 +01:00
|
|
|
<template>
|
2023-05-02 02:07:57 +02:00
|
|
|
<div>
|
|
|
|
<div :class="$style.label" @click="focus"><slot name="label"></slot></div>
|
2023-05-08 01:13:41 +02:00
|
|
|
<div :class="[$style.input, { [$style.inline]: inline, [$style.disabled]: disabled, [$style.focused]: focused }]">
|
2023-05-02 02:22:37 +02:00
|
|
|
<div ref="prefixEl" :class="$style.prefix"><slot name="prefix"></slot></div>
|
2022-06-25 20:12:58 +02:00
|
|
|
<input
|
|
|
|
ref="inputEl"
|
2021-09-29 17:50:45 +02:00
|
|
|
v-model="v"
|
2021-12-25 17:42:50 +01:00
|
|
|
v-adaptive-border
|
2023-05-02 02:07:57 +02:00
|
|
|
:class="$style.inputCore"
|
2021-11-19 11:36:12 +01:00
|
|
|
:type="type"
|
2021-09-29 17:50:45 +02:00
|
|
|
:disabled="disabled"
|
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
|
|
|
:pattern="pattern"
|
|
|
|
:autocomplete="autocomplete"
|
2023-10-25 14:20:36 +02:00
|
|
|
:autocapitalize="autocapitalize"
|
2021-09-29 17:50:45 +02:00
|
|
|
:spellcheck="spellcheck"
|
|
|
|
:step="step"
|
2021-11-19 11:36:12 +01:00
|
|
|
:list="id"
|
2023-10-08 10:56:44 +02:00
|
|
|
:min="min"
|
|
|
|
:max="max"
|
2021-09-29 17:50:45 +02:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
|
|
|
@keydown="onKeydown($event)"
|
|
|
|
@input="onInput"
|
|
|
|
>
|
2021-11-19 11:36:12 +01:00
|
|
|
<datalist v-if="datalist" :id="id">
|
2023-02-22 06:47:51 +01:00
|
|
|
<option v-for="data in datalist" :key="data" :value="data"/>
|
2021-09-29 17:50:45 +02:00
|
|
|
</datalist>
|
2023-05-02 02:22:37 +02:00
|
|
|
<div ref="suffixEl" :class="$style.suffix"><slot name="suffix"></slot></div>
|
2020-11-25 13:31:34 +01:00
|
|
|
</div>
|
2023-05-02 02:07:57 +02:00
|
|
|
<div :class="$style.caption"><slot name="caption"></slot></div>
|
2021-02-13 04:28:26 +01:00
|
|
|
|
2023-09-30 21:53:52 +02:00
|
|
|
<MkButton v-if="manualSave && changed" primary :class="$style.save" @click="updated"><i class="ph-check ph-bold ph-lg"></i> {{ i18n.ts.save }}</MkButton>
|
2021-09-29 17:50:45 +02:00
|
|
|
</div>
|
2020-11-25 13:31:34 +01:00
|
|
|
</template>
|
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
<script lang="ts" setup>
|
2023-02-16 15:09:41 +01:00
|
|
|
import { onMounted, nextTick, ref, shallowRef, watch, computed, toRefs } from 'vue';
|
2021-09-29 17:50:45 +02:00
|
|
|
import { debounce } from 'throttle-debounce';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { useInterval } from '@/scripts/use-interval.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
const props = defineProps<{
|
2023-02-20 08:40:24 +01:00
|
|
|
modelValue: string | number | null;
|
2023-02-09 09:54:30 +01:00
|
|
|
type?: 'text' | 'number' | 'password' | 'email' | 'url' | 'date' | 'time' | 'search' | 'datetime-local';
|
2022-06-28 08:59:49 +02:00
|
|
|
required?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
pattern?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
autofocus?: boolean;
|
2023-02-20 08:40:24 +01:00
|
|
|
autocomplete?: string;
|
2023-10-25 14:20:36 +02:00
|
|
|
autocapitalize?: string;
|
2022-06-28 08:59:49 +02:00
|
|
|
spellcheck?: boolean;
|
|
|
|
step?: any;
|
|
|
|
datalist?: string[];
|
2023-10-08 10:57:48 +02:00
|
|
|
min?: number;
|
2023-10-19 13:26:40 +02:00
|
|
|
max?: number | string;
|
2022-06-28 08:59:49 +02:00
|
|
|
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-03 02:12:37 +01:00
|
|
|
const inputEl = shallowRef<HTMLElement>();
|
|
|
|
const prefixEl = shallowRef<HTMLElement>();
|
|
|
|
const suffixEl = shallowRef<HTMLElement>();
|
2022-06-28 08:59:49 +02:00
|
|
|
const height =
|
2023-01-15 06:18:45 +01:00
|
|
|
props.small ? 33 :
|
|
|
|
props.large ? 39 :
|
|
|
|
36;
|
2022-06-28 08:59:49 +02:00
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev: KeyboardEvent) => {
|
|
|
|
changed.value = true;
|
|
|
|
emit('change', ev);
|
|
|
|
};
|
|
|
|
const onKeydown = (ev: KeyboardEvent) => {
|
2023-01-28 07:15:29 +01:00
|
|
|
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
|
|
|
|
|
2022-06-28 08:59:49 +02: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 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
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
watch(modelValue, newValue => {
|
|
|
|
v.value = newValue;
|
|
|
|
});
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
watch(v, newValue => {
|
|
|
|
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になってしまうので、定期的に計算する
|
|
|
|
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 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-09-22 07:12:33 +02:00
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
focus,
|
|
|
|
});
|
2020-11-25 13:31:34 +01:00
|
|
|
</script>
|
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
<style lang="scss" module>
|
|
|
|
.label {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 0 0 8px 0;
|
|
|
|
user-select: none;
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2021-09-29 17:50:45 +02:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
.caption {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 8px 0 0 0;
|
|
|
|
color: var(--fgTransparentWeak);
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
.input {
|
|
|
|
position: relative;
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&.focused {
|
|
|
|
> .inputCore {
|
|
|
|
border-color: var(--accent) !important;
|
|
|
|
//box-shadow: 0 0 0 4px var(--focus);
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&.disabled {
|
|
|
|
opacity: 0.7;
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
&,
|
|
|
|
> .inputCore {
|
|
|
|
cursor: not-allowed !important;
|
2021-09-29 17:50:45 +02:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-02 02:07:57 +02:00
|
|
|
.inputCore {
|
|
|
|
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);
|
2023-10-01 01:20:20 +02:00
|
|
|
border-radius: 5px;
|
2023-05-02 02:07:57 +02:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
|
|
|
transition: border-color 0.1s ease-out;
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
border-color: var(--inputBorderHover) !important;
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
}
|
2021-11-28 12:07:37 +01:00
|
|
|
|
2023-05-02 02:07:57 +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'");
|
2023-05-02 02:22:37 +02:00
|
|
|
min-width: 16px;
|
|
|
|
max-width: 150px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
box-sizing: border-box;
|
2023-05-02 02:07:57 +02:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
2021-11-28 12:07:37 +01:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-02 02:07:57 +02:00
|
|
|
|
|
|
|
.prefix {
|
|
|
|
left: 0;
|
|
|
|
padding-right: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.suffix {
|
|
|
|
right: 0;
|
|
|
|
padding-left: 6px;
|
|
|
|
}
|
|
|
|
.save {
|
|
|
|
margin: 8px 0 0 0;
|
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
</style>
|