2020-11-25 12:31:34 +00:00
|
|
|
<template>
|
2021-09-29 15:50:45 +00:00
|
|
|
<div class="vblkjoeq">
|
|
|
|
<div class="label" @click="focus"><slot name="label"></slot></div>
|
2021-11-19 10:36:12 +00:00
|
|
|
<div ref="container" class="input" :class="{ inline, disabled, focused }" @click.prevent="onClick">
|
|
|
|
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
|
2021-11-28 11:07:37 +00:00
|
|
|
<select ref="inputEl" v-model="v" v-panel
|
2021-11-19 10:36:12 +00:00
|
|
|
class="select"
|
2020-11-25 12:31:34 +00:00
|
|
|
:disabled="disabled"
|
2021-09-29 15:50:45 +00:00
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
2020-11-25 12:31:34 +00:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
2021-09-29 15:50:45 +00:00
|
|
|
@input="onInput"
|
2020-11-25 12:31:34 +00:00
|
|
|
>
|
|
|
|
<slot></slot>
|
|
|
|
</select>
|
2021-11-19 10:36:12 +00:00
|
|
|
<div ref="suffixEl" class="suffix"><i class="fas fa-chevron-down"></i></div>
|
2020-11-25 12:31:34 +00:00
|
|
|
</div>
|
2021-09-29 15:50:45 +00:00
|
|
|
<div class="caption"><slot name="caption"></slot></div>
|
|
|
|
|
2021-11-19 10:36:12 +00:00
|
|
|
<MkButton v-if="manualSave && changed" primary @click="updated"><i class="fas fa-save"></i> {{ $ts.save }}</MkButton>
|
2020-11-25 12:31:34 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-10-23 02:17:41 +00:00
|
|
|
import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs, VNode } from 'vue';
|
2021-11-11 17:02:25 +00:00
|
|
|
import MkButton from '@/components/ui/button.vue';
|
|
|
|
import * as os from '@/os';
|
2020-11-25 12:31:34 +00:00
|
|
|
|
|
|
|
export default defineComponent({
|
2021-09-29 15:50:45 +00:00
|
|
|
components: {
|
|
|
|
MkButton,
|
|
|
|
},
|
|
|
|
|
2020-11-25 12:31:34 +00:00
|
|
|
props: {
|
2021-09-29 15:50:45 +00:00
|
|
|
modelValue: {
|
|
|
|
required: true
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
|
|
|
required: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
readonly: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2020-11-25 12:31:34 +00:00
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
autofocus: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2020-11-25 12:31:34 +00:00
|
|
|
inline: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
manualSave: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
emits: ['change', 'update:modelValue'],
|
|
|
|
|
|
|
|
setup(props, context) {
|
|
|
|
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);
|
2021-10-21 21:23:23 +00:00
|
|
|
const container = ref(null);
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
context.emit('change', ev);
|
2020-11-25 12:31:34 +00:00
|
|
|
};
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
const updated = () => {
|
|
|
|
changed.value = false;
|
|
|
|
context.emit('update:modelValue', v.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
watch(modelValue, newValue => {
|
|
|
|
v.value = newValue;
|
|
|
|
});
|
|
|
|
|
|
|
|
watch(v, newValue => {
|
|
|
|
if (!props.manualSave) {
|
|
|
|
updated();
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
invalid.value = inputEl.value.validity.badInput;
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (autofocus.value) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// このコンポーネントが作成された時、非表示状態である場合がある
|
|
|
|
// 非表示状態だと要素の幅などは0になってしまうので、定期的に計算する
|
|
|
|
const clock = setInterval(() => {
|
|
|
|
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);
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
clearInterval(clock);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-10-21 21:23:23 +00:00
|
|
|
const onClick = (ev: MouseEvent) => {
|
|
|
|
focused.value = true;
|
|
|
|
|
|
|
|
const menu = [];
|
|
|
|
let options = context.slots.default();
|
|
|
|
|
2021-10-23 02:17:41 +00:00
|
|
|
const pushOption = (option: VNode) => {
|
|
|
|
menu.push({
|
|
|
|
text: option.children,
|
|
|
|
active: v.value === option.props.value,
|
|
|
|
action: () => {
|
|
|
|
v.value = option.props.value;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-10-31 10:35:35 +00:00
|
|
|
const scanOptions = (options: VNode[]) => {
|
|
|
|
for (const vnode of options) {
|
|
|
|
if (vnode.type === 'optgroup') {
|
|
|
|
const optgroup = vnode;
|
|
|
|
menu.push({
|
|
|
|
type: 'label',
|
|
|
|
text: optgroup.props.label,
|
|
|
|
});
|
|
|
|
scanOptions(optgroup.children);
|
|
|
|
} else if (Array.isArray(vnode.children)) { // 何故かフラグメントになってくることがある
|
|
|
|
const fragment = vnode;
|
|
|
|
scanOptions(fragment.children);
|
|
|
|
} else {
|
|
|
|
const option = vnode;
|
2021-10-23 02:17:41 +00:00
|
|
|
pushOption(option);
|
|
|
|
}
|
2021-10-21 21:23:23 +00:00
|
|
|
}
|
2021-10-31 10:35:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
scanOptions(options);
|
2021-10-21 21:23:23 +00:00
|
|
|
|
|
|
|
os.popupMenu(menu, container.value, {
|
|
|
|
width: container.value.offsetWidth,
|
|
|
|
}).then(() => {
|
|
|
|
focused.value = false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-29 15:50:45 +00:00
|
|
|
return {
|
|
|
|
v,
|
|
|
|
focused,
|
|
|
|
invalid,
|
|
|
|
changed,
|
|
|
|
filled,
|
|
|
|
inputEl,
|
|
|
|
prefixEl,
|
|
|
|
suffixEl,
|
2021-10-21 21:23:23 +00:00
|
|
|
container,
|
2021-09-29 15:50:45 +00:00
|
|
|
focus,
|
|
|
|
onInput,
|
2021-10-21 21:23:23 +00:00
|
|
|
onClick,
|
2021-09-29 15:50:45 +00:00
|
|
|
updated,
|
|
|
|
};
|
2020-11-25 12:31:34 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-09-29 15:50:45 +00:00
|
|
|
.vblkjoeq {
|
|
|
|
> .label {
|
|
|
|
font-size: 0.85em;
|
2021-11-28 11:07:37 +00:00
|
|
|
padding: 0 0 8px 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
user-select: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .caption {
|
2021-11-28 11:07:37 +00:00
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 8px 0 0 0;
|
2021-09-29 15:50:45 +00:00
|
|
|
color: var(--fgTransparentWeak);
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .input {
|
2021-09-29 15:50:45 +00:00
|
|
|
$height: 42px;
|
2020-11-25 12:31:34 +00:00
|
|
|
position: relative;
|
2021-10-21 21:23:23 +00:00
|
|
|
cursor: pointer;
|
2020-11-25 12:31:34 +00:00
|
|
|
|
2021-10-21 21:23:23 +00:00
|
|
|
&:hover {
|
|
|
|
> .select {
|
|
|
|
border-color: var(--inputBorderHover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .select {
|
2021-09-29 15:50:45 +00:00
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
2020-11-25 12:31:34 +00:00
|
|
|
display: block;
|
2021-09-29 15:50:45 +00:00
|
|
|
height: $height;
|
2020-11-25 12:31:34 +00:00
|
|
|
width: 100%;
|
2021-09-29 15:50:45 +00:00
|
|
|
margin: 0;
|
|
|
|
padding: 0 12px;
|
2020-11-25 12:31:34 +00:00
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
|
|
|
font-size: 1em;
|
2021-09-29 15:50:45 +00:00
|
|
|
color: var(--fg);
|
2021-11-28 11:07:37 +00:00
|
|
|
border: solid 1px var(--panel);
|
2021-09-29 15:50:45 +00:00
|
|
|
border-radius: 6px;
|
2020-11-25 12:31:34 +00:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
2021-09-29 15:50:45 +00:00
|
|
|
box-sizing: border-box;
|
|
|
|
cursor: pointer;
|
|
|
|
transition: border-color 0.1s ease-out;
|
2021-10-21 21:23:23 +00:00
|
|
|
pointer-events: none;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix,
|
|
|
|
> .suffix {
|
2021-09-29 15:50:45 +00:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
top: 0;
|
|
|
|
padding: 0 12px;
|
2020-11-25 12:31:34 +00:00
|
|
|
font-size: 1em;
|
2021-09-29 15:50:45 +00:00
|
|
|
height: $height;
|
2020-11-25 12:31:34 +00:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> * {
|
2021-09-29 15:50:45 +00:00
|
|
|
display: inline-block;
|
2020-11-25 12:31:34 +00:00
|
|
|
min-width: 16px;
|
2021-09-29 15:50:45 +00:00
|
|
|
max-width: 150px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix {
|
2021-09-29 15:50:45 +00:00
|
|
|
left: 0;
|
|
|
|
padding-right: 6px;
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
> .suffix {
|
2021-09-29 15:50:45 +00:00
|
|
|
right: 0;
|
|
|
|
padding-left: 6px;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
&.focused {
|
|
|
|
> select {
|
|
|
|
border-color: var(--accent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.disabled {
|
2020-11-25 12:31:34 +00:00
|
|
|
opacity: 0.7;
|
2021-09-29 15:50:45 +00:00
|
|
|
|
|
|
|
&, * {
|
|
|
|
cursor: not-allowed !important;
|
|
|
|
}
|
2020-11-25 12:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|