2020-11-25 14:31:34 +02:00
|
|
|
<template>
|
2021-09-29 18:50:45 +03:00
|
|
|
<div class="vblkjoeq">
|
|
|
|
<div class="label" @click="focus"><slot name="label"></slot></div>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div ref="container" class="input" :class="{ inline, disabled, focused }" @click.prevent="onClick">
|
|
|
|
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
|
2022-06-25 21:12:58 +03:00
|
|
|
<select
|
|
|
|
ref="inputEl"
|
2021-12-25 18:42:50 +02:00
|
|
|
v-model="v"
|
|
|
|
v-adaptive-border
|
2021-11-19 12:36:12 +02:00
|
|
|
class="select"
|
2020-11-25 14:31:34 +02:00
|
|
|
:disabled="disabled"
|
2021-09-29 18:50:45 +03:00
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
2020-11-25 14:31:34 +02:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
2021-09-29 18:50:45 +03:00
|
|
|
@input="onInput"
|
2020-11-25 14:31:34 +02:00
|
|
|
>
|
|
|
|
<slot></slot>
|
|
|
|
</select>
|
2021-11-19 12:36:12 +02:00
|
|
|
<div ref="suffixEl" class="suffix"><i class="fas fa-chevron-down"></i></div>
|
2020-11-25 14:31:34 +02:00
|
|
|
</div>
|
2021-09-29 18:50:45 +03:00
|
|
|
<div class="caption"><slot name="caption"></slot></div>
|
|
|
|
|
2022-07-20 16:24:26 +03:00
|
|
|
<MkButton v-if="manualSave && changed" primary @click="updated"><i class="fas fa-save"></i> {{ i18n.ts.save }}</MkButton>
|
2020-11-25 14:31:34 +02:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs, VNode, useSlots } from 'vue';
|
2021-11-11 19:02:25 +02:00
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import * as os from '@/os';
|
2022-06-25 21:12:58 +03:00
|
|
|
import { useInterval } from '@/scripts/use-interval';
|
2022-07-20 16:24:26 +03:00
|
|
|
import { i18n } from '@/i18n';
|
2020-11-25 14:31:34 +02:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
const props = defineProps<{
|
|
|
|
modelValue: string;
|
|
|
|
required?: boolean;
|
|
|
|
readonly?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
placeholder?: string;
|
|
|
|
autofocus?: boolean;
|
|
|
|
inline?: boolean;
|
|
|
|
manualSave?: boolean;
|
|
|
|
small?: boolean;
|
|
|
|
large?: boolean;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
(ev: 'change', _ev: KeyboardEvent): void;
|
|
|
|
(ev: 'update:modelValue', value: string): void;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const slots = useSlots();
|
|
|
|
|
|
|
|
const { modelValue, autofocus } = toRefs(props);
|
|
|
|
const v = ref(modelValue.value);
|
|
|
|
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);
|
|
|
|
const container = ref(null);
|
|
|
|
const height =
|
2022-07-16 14:53:53 +03:00
|
|
|
props.small ? 36 :
|
|
|
|
props.large ? 40 :
|
|
|
|
38;
|
2022-06-28 09:59:49 +03:00
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
emit('change', ev);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
emit('update:modelValue', v.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
watch(modelValue, newValue => {
|
|
|
|
v.value = newValue;
|
|
|
|
});
|
2021-09-29 18:50:45 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
watch(v, newValue => {
|
|
|
|
if (!props.manualSave) {
|
|
|
|
updated();
|
|
|
|
}
|
2021-09-29 18:50:45 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
invalid.value = inputEl.value.validity.badInput;
|
|
|
|
});
|
2021-09-29 18:50:45 +03:00
|
|
|
|
2022-06-28 09:59:49 +03: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,
|
|
|
|
});
|
2022-06-25 21:12:58 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const onClick = (ev: MouseEvent) => {
|
|
|
|
focused.value = true;
|
2021-09-29 18:50:45 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
const menu = [];
|
|
|
|
let options = slots.default!();
|
2021-10-22 00:23:23 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
const pushOption = (option: VNode) => {
|
|
|
|
menu.push({
|
|
|
|
text: option.children,
|
|
|
|
active: v.value === option.props.value,
|
|
|
|
action: () => {
|
|
|
|
v.value = option.props.value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2021-10-22 00:23:23 +03:00
|
|
|
|
2022-06-28 09:59:49 +03:00
|
|
|
const scanOptions = (options: VNode[]) => {
|
|
|
|
for (const vnode of options) {
|
|
|
|
if (vnode.type === 'optgroup') {
|
|
|
|
const optgroup = vnode;
|
2021-10-23 05:17:41 +03:00
|
|
|
menu.push({
|
2022-06-28 09:59:49 +03:00
|
|
|
type: 'label',
|
|
|
|
text: optgroup.props.label,
|
2021-10-23 05:17:41 +03:00
|
|
|
});
|
2022-06-28 09:59:49 +03:00
|
|
|
scanOptions(optgroup.children);
|
|
|
|
} else if (Array.isArray(vnode.children)) { // 何故かフラグメントになってくることがある
|
|
|
|
const fragment = vnode;
|
|
|
|
scanOptions(fragment.children);
|
2022-07-20 17:01:40 +03:00
|
|
|
} else if (vnode.props == null) { // v-if で条件が false のときにこうなる
|
|
|
|
// nop?
|
2022-06-28 09:59:49 +03:00
|
|
|
} else {
|
|
|
|
const option = vnode;
|
|
|
|
pushOption(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
scanOptions(options);
|
|
|
|
|
|
|
|
os.popupMenu(menu, container.value, {
|
|
|
|
width: container.value.offsetWidth,
|
|
|
|
}).then(() => {
|
|
|
|
focused.value = false;
|
|
|
|
});
|
|
|
|
};
|
2020-11-25 14:31:34 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-09-29 18:50:45 +03:00
|
|
|
.vblkjoeq {
|
|
|
|
> .label {
|
|
|
|
font-size: 0.85em;
|
2021-11-28 13:07:37 +02:00
|
|
|
padding: 0 0 8px 0;
|
2021-09-29 18:50:45 +03:00
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .caption {
|
2021-11-28 13:07:37 +02:00
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 8px 0 0 0;
|
2021-09-29 18:50:45 +03:00
|
|
|
color: var(--fgTransparentWeak);
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 14:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .input {
|
|
|
|
position: relative;
|
2021-10-22 00:23:23 +03:00
|
|
|
cursor: pointer;
|
2020-11-25 14:31:34 +02:00
|
|
|
|
2021-10-22 00:23:23 +03:00
|
|
|
&:hover {
|
|
|
|
> .select {
|
2021-12-25 18:42:50 +02:00
|
|
|
border-color: var(--inputBorderHover) !important;
|
2021-10-22 00:23:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .select {
|
2021-09-29 18:50:45 +03:00
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
2020-11-25 14:31:34 +02:00
|
|
|
display: block;
|
2022-06-28 09:59:49 +03:00
|
|
|
height: v-bind("height + 'px'");
|
2020-11-25 14:31:34 +02:00
|
|
|
width: 100%;
|
2021-09-29 18:50:45 +03:00
|
|
|
margin: 0;
|
|
|
|
padding: 0 12px;
|
2020-11-25 14:31:34 +02:00
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
|
|
|
font-size: 1em;
|
2021-09-29 18:50:45 +03:00
|
|
|
color: var(--fg);
|
2021-12-25 18:42:50 +02:00
|
|
|
background: var(--panel);
|
2021-11-28 13:07:37 +02:00
|
|
|
border: solid 1px var(--panel);
|
2021-09-29 18:50:45 +03:00
|
|
|
border-radius: 6px;
|
2020-11-25 14:31:34 +02:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
2021-09-29 18:50:45 +03:00
|
|
|
box-sizing: border-box;
|
|
|
|
cursor: pointer;
|
|
|
|
transition: border-color 0.1s ease-out;
|
2021-10-22 00:23:23 +03:00
|
|
|
pointer-events: none;
|
2022-06-30 17:07:45 +03:00
|
|
|
user-select: none;
|
2020-11-25 14:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix,
|
|
|
|
> .suffix {
|
2021-09-29 18:50:45 +03:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
top: 0;
|
|
|
|
padding: 0 12px;
|
2020-11-25 14:31:34 +02:00
|
|
|
font-size: 1em;
|
2022-06-28 09:59:49 +03:00
|
|
|
height: v-bind("height + 'px'");
|
2020-11-25 14:31:34 +02:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> * {
|
2021-09-29 18:50:45 +03:00
|
|
|
display: inline-block;
|
2020-11-25 14:31:34 +02:00
|
|
|
min-width: 16px;
|
2021-09-29 18:50:45 +03:00
|
|
|
max-width: 150px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
2020-11-25 14:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix {
|
2021-09-29 18:50:45 +03:00
|
|
|
left: 0;
|
|
|
|
padding-right: 6px;
|
2020-11-25 14:31:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
> .suffix {
|
2021-09-29 18:50:45 +03:00
|
|
|
right: 0;
|
|
|
|
padding-left: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.focused {
|
|
|
|
> select {
|
2021-12-25 18:42:50 +02:00
|
|
|
border-color: var(--accent) !important;
|
2021-09-29 18:50:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.disabled {
|
2020-11-25 14:31:34 +02:00
|
|
|
opacity: 0.7;
|
2021-09-29 18:50:45 +03:00
|
|
|
|
|
|
|
&, * {
|
|
|
|
cursor: not-allowed !important;
|
|
|
|
}
|
2020-11-25 14:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|