2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:59:27 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-11-25 13:31:34 +01:00
|
|
|
<template>
|
2023-05-09 11:47:06 +02:00
|
|
|
<div>
|
|
|
|
<div :class="$style.label" @click="focus"><slot name="label"></slot></div>
|
|
|
|
<div ref="container" :class="[$style.input, { [$style.inline]: inline, [$style.disabled]: disabled, [$style.focused]: focused }]" @mousedown.prevent="show">
|
|
|
|
<div ref="prefixEl" :class="$style.prefix"><slot name="prefix"></slot></div>
|
2022-06-25 20:12:58 +02:00
|
|
|
<select
|
|
|
|
ref="inputEl"
|
2021-12-25 17:42:50 +01:00
|
|
|
v-model="v"
|
|
|
|
v-adaptive-border
|
2023-05-09 11:47:06 +02:00
|
|
|
:class="$style.inputCore"
|
2020-11-25 13:31:34 +01:00
|
|
|
:disabled="disabled"
|
2021-09-29 17:50:45 +02:00
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
2020-11-25 13:31:34 +01:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
2021-09-29 17:50:45 +02:00
|
|
|
@input="onInput"
|
2020-11-25 13:31:34 +01:00
|
|
|
>
|
|
|
|
<slot></slot>
|
|
|
|
</select>
|
2023-09-30 21:53:52 +02:00
|
|
|
<div ref="suffixEl" :class="$style.suffix"><i class="ph-caret-down ph-bold ph-lg" :class="[$style.chevron, { [$style.chevronOpening]: opening }]"></i></div>
|
2020-11-25 13:31:34 +01:00
|
|
|
</div>
|
2023-05-09 11:47:06 +02:00
|
|
|
<div :class="$style.caption"><slot name="caption"></slot></div>
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2024-02-06 21:23:37 +01:00
|
|
|
<MkButton v-if="manualSave && changed" primary :class="$style.save" @click="updated"><i class="ph-floppy-disk ph-bold ph-lg"></i> {{ i18n.ts.save }}</MkButton>
|
2020-11-25 13:31:34 +01:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
<script lang="ts" setup>
|
2024-01-30 11:53:53 +01:00
|
|
|
import { onMounted, nextTick, ref, watch, computed, toRefs, VNode, useSlots, VNodeChild } from 'vue';
|
2022-09-06 11:21:49 +02:00
|
|
|
import MkButton from '@/components/MkButton.vue';
|
2023-09-19 09:37:43 +02:00
|
|
|
import * as os from '@/os.js';
|
|
|
|
import { useInterval } from '@/scripts/use-interval.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2024-01-30 11:53:53 +01:00
|
|
|
import { MenuItem } from '@/types/menu.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 | null;
|
2022-06-28 08:59:49 +02:00
|
|
|
required?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
placeholder?: string;
|
|
|
|
autofocus?: boolean;
|
|
|
|
inline?: boolean;
|
|
|
|
manualSave?: boolean;
|
|
|
|
small?: boolean;
|
|
|
|
large?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
2024-01-30 11:53:53 +01:00
|
|
|
(ev: 'changeByUser', value: string | null): void;
|
2023-02-20 08:40:24 +01:00
|
|
|
(ev: 'update:modelValue', value: string | null): void;
|
2022-06-28 08:59:49 +02:00
|
|
|
}>();
|
|
|
|
|
|
|
|
const slots = useSlots();
|
|
|
|
|
|
|
|
const { modelValue, autofocus } = toRefs(props);
|
|
|
|
const v = ref(modelValue.value);
|
|
|
|
const focused = ref(false);
|
2023-01-06 02:11:47 +01:00
|
|
|
const opening = ref(false);
|
2022-06-28 08:59:49 +02:00
|
|
|
const changed = ref(false);
|
|
|
|
const invalid = ref(false);
|
|
|
|
const filled = computed(() => v.value !== '' && v.value != null);
|
2023-12-26 06:19:35 +01:00
|
|
|
const inputEl = ref<HTMLObjectElement | null>(null);
|
|
|
|
const prefixEl = ref<HTMLElement | null>(null);
|
|
|
|
const suffixEl = ref<HTMLElement | null>(null);
|
|
|
|
const container = ref<HTMLElement | null>(null);
|
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
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const focus = () => inputEl.value?.focus();
|
2022-06-28 08:59:49 +02:00
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
emit('update:modelValue', v.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
watch(modelValue, newValue => {
|
|
|
|
v.value = newValue;
|
|
|
|
});
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
watch(v, () => {
|
2022-06-28 08:59:49 +02:00
|
|
|
if (!props.manualSave) {
|
|
|
|
updated();
|
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
invalid.value = inputEl.value?.validity.badInput ?? true;
|
2022-06-28 08:59:49 +02:00
|
|
|
});
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
// このコンポーネントが作成された時、非表示状態である場合がある
|
|
|
|
// 非表示状態だと要素の幅などは0になってしまうので、定期的に計算する
|
|
|
|
useInterval(() => {
|
2024-01-30 11:53:53 +01:00
|
|
|
if (inputEl.value == null) return;
|
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
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,
|
|
|
|
});
|
2022-06-25 20:12:58 +02:00
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
function show() {
|
2022-06-28 08:59:49 +02:00
|
|
|
focused.value = true;
|
2023-01-06 02:11:47 +01:00
|
|
|
opening.value = true;
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const menu: MenuItem[] = [];
|
2022-06-28 08:59:49 +02:00
|
|
|
let options = slots.default!();
|
2021-10-21 23:23:23 +02:00
|
|
|
|
2022-06-28 08:59:49 +02:00
|
|
|
const pushOption = (option: VNode) => {
|
|
|
|
menu.push({
|
2024-01-30 11:53:53 +01:00
|
|
|
text: option.children as string,
|
|
|
|
active: computed(() => v.value === option.props?.value),
|
2022-06-28 08:59:49 +02:00
|
|
|
action: () => {
|
2024-01-30 11:53:53 +01:00
|
|
|
v.value = option.props?.value;
|
2024-02-06 21:23:37 +01:00
|
|
|
changed.value = true;
|
2024-01-19 12:51:49 +01:00
|
|
|
emit('changeByUser', v.value);
|
2022-06-28 08:59:49 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2021-10-21 23:23:23 +02:00
|
|
|
|
2024-01-30 11:53:53 +01:00
|
|
|
const scanOptions = (options: VNodeChild[]) => {
|
2022-06-28 08:59:49 +02:00
|
|
|
for (const vnode of options) {
|
2024-01-30 11:53:53 +01:00
|
|
|
if (typeof vnode !== 'object' || vnode === null || Array.isArray(vnode)) continue;
|
2022-06-28 08:59:49 +02:00
|
|
|
if (vnode.type === 'optgroup') {
|
|
|
|
const optgroup = vnode;
|
2021-10-23 04:17:41 +02:00
|
|
|
menu.push({
|
2022-06-28 08:59:49 +02:00
|
|
|
type: 'label',
|
2024-01-30 11:53:53 +01:00
|
|
|
text: optgroup.props?.label,
|
2021-10-23 04:17:41 +02:00
|
|
|
});
|
2024-01-30 11:53:53 +01:00
|
|
|
if (Array.isArray(optgroup.children)) scanOptions(optgroup.children);
|
2022-06-28 08:59:49 +02:00
|
|
|
} else if (Array.isArray(vnode.children)) { // 何故かフラグメントになってくることがある
|
|
|
|
const fragment = vnode;
|
2024-01-30 11:53:53 +01:00
|
|
|
if (Array.isArray(fragment.children)) scanOptions(fragment.children);
|
2022-07-20 16:01:40 +02:00
|
|
|
} else if (vnode.props == null) { // v-if で条件が false のときにこうなる
|
|
|
|
// nop?
|
2022-06-28 08:59:49 +02:00
|
|
|
} else {
|
|
|
|
const option = vnode;
|
|
|
|
pushOption(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
scanOptions(options);
|
|
|
|
|
|
|
|
os.popupMenu(menu, container.value, {
|
2024-01-30 11:53:53 +01:00
|
|
|
width: container.value?.offsetWidth,
|
2023-01-06 02:11:47 +01:00
|
|
|
onClosing: () => {
|
|
|
|
opening.value = false;
|
|
|
|
},
|
2022-06-28 08:59:49 +02:00
|
|
|
}).then(() => {
|
|
|
|
focused.value = false;
|
|
|
|
});
|
2023-01-08 02:24:30 +01:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
</script>
|
|
|
|
|
2023-05-09 11:47:06 +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-09 11:47:06 +02:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2021-09-29 17:50:45 +02:00
|
|
|
}
|
2023-05-09 11:47:06 +02:00
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-09 11:47:06 +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-09 11:47:06 +02:00
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-09 11:47:06 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
.input {
|
|
|
|
position: relative;
|
|
|
|
cursor: pointer;
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
2021-10-21 23:23:23 +02:00
|
|
|
|
2023-05-09 11:47:06 +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-09 11:47:06 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
&.disabled {
|
|
|
|
opacity: 0.7;
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
&,
|
|
|
|
> .inputCore {
|
|
|
|
cursor: not-allowed !important;
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-05-09 11:47:06 +02:00
|
|
|
}
|
2020-11-25 13:31:34 +01:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
&:hover {
|
|
|
|
> .inputCore {
|
|
|
|
border-color: var(--inputBorderHover) !important;
|
2021-09-29 17:50:45 +02:00
|
|
|
}
|
2023-05-09 11:47:06 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-09 11:47:06 +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-31 19:44:34 +01:00
|
|
|
border-radius: var(--radius-sm);
|
2023-05-09 11:47:06 +02:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
|
|
|
box-sizing: border-box;
|
|
|
|
transition: border-color 0.1s ease-out;
|
|
|
|
cursor: pointer;
|
|
|
|
pointer-events: none;
|
|
|
|
user-select: none;
|
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-09 11:47:06 +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'");
|
|
|
|
min-width: 16px;
|
|
|
|
max-width: 150px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
box-sizing: border-box;
|
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
.prefix {
|
|
|
|
left: 0;
|
|
|
|
padding-right: 6px;
|
|
|
|
}
|
2021-09-29 17:50:45 +02:00
|
|
|
|
2023-05-09 11:47:06 +02:00
|
|
|
.suffix {
|
|
|
|
right: 0;
|
|
|
|
padding-left: 6px;
|
2020-11-25 13:31:34 +01:00
|
|
|
}
|
2023-01-06 02:11:47 +01:00
|
|
|
|
2024-02-06 21:23:37 +01:00
|
|
|
.save {
|
|
|
|
margin: 8px 0 0 0;
|
|
|
|
}
|
|
|
|
|
2023-01-06 02:11:47 +01:00
|
|
|
.chevron {
|
2023-01-08 02:24:30 +01:00
|
|
|
transition: transform 0.1s ease-out;
|
2023-01-06 02:11:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.chevronOpening {
|
|
|
|
transform: rotateX(180deg);
|
|
|
|
}
|
|
|
|
</style>
|