2020-01-30 04:37:25 +09:00
|
|
|
<template>
|
2021-08-06 22:29:19 +09:00
|
|
|
<div class="vblkjoeq">
|
|
|
|
<div class="label" @click="focus"><slot name="label"></slot></div>
|
|
|
|
<div class="input" :class="{ inline, disabled, focused }">
|
|
|
|
<div class="prefix" ref="prefixEl"><slot name="prefix"></slot></div>
|
|
|
|
<select ref="inputEl"
|
2020-01-30 04:37:25 +09:00
|
|
|
v-model="v"
|
|
|
|
:disabled="disabled"
|
2021-08-06 22:29:19 +09:00
|
|
|
:required="required"
|
|
|
|
:readonly="readonly"
|
|
|
|
:placeholder="placeholder"
|
2020-01-30 04:37:25 +09:00
|
|
|
@focus="focused = true"
|
|
|
|
@blur="focused = false"
|
2021-08-06 22:29:19 +09:00
|
|
|
@input="onInput"
|
2020-01-30 04:37:25 +09:00
|
|
|
>
|
|
|
|
<slot></slot>
|
|
|
|
</select>
|
2021-08-06 22:29:19 +09:00
|
|
|
<div class="suffix" ref="suffixEl"><i class="fas fa-chevron-down"></i></div>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2021-08-06 22:29:19 +09:00
|
|
|
<div class="caption"><slot name="caption"></slot></div>
|
|
|
|
|
|
|
|
<MkButton v-if="manualSave && changed" @click="updated" primary><i class="fas fa-save"></i> {{ $ts.save }}</MkButton>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-08-06 22:29:19 +09:00
|
|
|
import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs } from 'vue';
|
|
|
|
import MkButton from './button.vue';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
export default defineComponent({
|
2021-08-06 22:29:19 +09:00
|
|
|
components: {
|
|
|
|
MkButton,
|
|
|
|
},
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
props: {
|
2021-08-06 22:29:19 +09:00
|
|
|
modelValue: {
|
|
|
|
required: true
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
required: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2021-08-06 22:29:19 +09:00
|
|
|
readonly: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
disabled: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false
|
|
|
|
},
|
2021-08-06 22:29:19 +09:00
|
|
|
placeholder: {
|
|
|
|
type: String,
|
|
|
|
required: false
|
|
|
|
},
|
|
|
|
autofocus: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
inline: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2021-08-06 22:29:19 +09:00
|
|
|
manualSave: {
|
|
|
|
type: Boolean,
|
|
|
|
required: false,
|
|
|
|
default: false
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2021-08-06 22:29:19 +09: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);
|
|
|
|
|
|
|
|
const focus = () => inputEl.value.focus();
|
|
|
|
const onInput = (ev) => {
|
|
|
|
changed.value = true;
|
|
|
|
context.emit('change', ev);
|
2020-01-30 04:37:25 +09:00
|
|
|
};
|
2021-08-06 22:29:19 +09: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-01-30 04:37:25 +09:00
|
|
|
}
|
2021-08-06 22:29:19 +09: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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
v,
|
|
|
|
focused,
|
|
|
|
invalid,
|
|
|
|
changed,
|
|
|
|
filled,
|
|
|
|
inputEl,
|
|
|
|
prefixEl,
|
|
|
|
suffixEl,
|
|
|
|
focus,
|
|
|
|
onInput,
|
|
|
|
updated,
|
|
|
|
};
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
2021-08-06 22:29:19 +09:00
|
|
|
.vblkjoeq {
|
2021-08-07 19:09:06 +09:00
|
|
|
margin: 1.5em 0;
|
2021-08-06 22:29:19 +09:00
|
|
|
|
|
|
|
> .label {
|
|
|
|
font-size: 0.85em;
|
|
|
|
padding: 0 0 6px 6px;
|
|
|
|
font-weight: bold;
|
|
|
|
user-select: none;
|
2021-08-07 19:09:06 +09:00
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
2020-02-14 01:24:05 +09:00
|
|
|
}
|
|
|
|
|
2021-08-06 22:29:19 +09:00
|
|
|
> .caption {
|
|
|
|
font-size: 0.8em;
|
|
|
|
padding: 6px 0 0 6px;
|
|
|
|
color: var(--fgTransparentWeak);
|
2021-08-07 19:09:06 +09:00
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
> .input {
|
2021-08-06 22:29:19 +09:00
|
|
|
$height: 42px;
|
2020-02-20 02:40:53 +09:00
|
|
|
position: relative;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
> select {
|
2021-08-06 22:29:19 +09:00
|
|
|
appearance: none;
|
|
|
|
-webkit-appearance: none;
|
2020-01-30 04:37:25 +09:00
|
|
|
display: block;
|
2021-08-06 22:29:19 +09:00
|
|
|
height: $height;
|
2020-01-30 04:37:25 +09:00
|
|
|
width: 100%;
|
2021-08-06 22:29:19 +09:00
|
|
|
margin: 0;
|
2021-08-07 19:09:06 +09:00
|
|
|
padding: 0 12px;
|
2020-01-30 04:37:25 +09:00
|
|
|
font: inherit;
|
|
|
|
font-weight: normal;
|
2020-07-11 10:13:11 +09:00
|
|
|
font-size: 1em;
|
2021-08-06 22:29:19 +09:00
|
|
|
color: var(--fg);
|
|
|
|
background: var(--panel);
|
|
|
|
border: solid 1px var(--inputBorder);
|
|
|
|
border-radius: 6px;
|
2020-01-30 04:37:25 +09:00
|
|
|
outline: none;
|
|
|
|
box-shadow: none;
|
2021-08-06 22:29:19 +09:00
|
|
|
box-sizing: border-box;
|
|
|
|
cursor: pointer;
|
2020-03-06 22:54:23 +09:00
|
|
|
|
2021-08-06 22:29:19 +09:00
|
|
|
&:hover {
|
|
|
|
border-color: var(--inputBorderHover);
|
2020-03-06 22:54:23 +09:00
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix,
|
|
|
|
> .suffix {
|
2021-08-06 22:29:19 +09:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
position: absolute;
|
|
|
|
z-index: 1;
|
|
|
|
top: 0;
|
|
|
|
padding: 0 12px;
|
2020-07-11 10:13:11 +09:00
|
|
|
font-size: 1em;
|
2021-08-06 22:29:19 +09:00
|
|
|
height: $height;
|
2020-01-30 04:37:25 +09:00
|
|
|
pointer-events: none;
|
|
|
|
|
|
|
|
&:empty {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
> * {
|
2021-08-06 22:29:19 +09:00
|
|
|
display: inline-block;
|
2020-01-30 04:37:25 +09:00
|
|
|
min-width: 16px;
|
2021-08-06 22:29:19 +09:00
|
|
|
max-width: 150px;
|
|
|
|
overflow: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> .prefix {
|
2021-08-06 22:29:19 +09:00
|
|
|
left: 0;
|
|
|
|
padding-right: 6px;
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
> .suffix {
|
2021-08-06 22:29:19 +09:00
|
|
|
right: 0;
|
|
|
|
padding-left: 6px;
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
|
2021-08-06 22:29:19 +09:00
|
|
|
&.inline {
|
|
|
|
display: inline-block;
|
2020-01-30 04:37:25 +09:00
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
2021-08-06 22:29:19 +09:00
|
|
|
&.focused {
|
|
|
|
> select {
|
|
|
|
border-color: var(--accent);
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 22:29:19 +09:00
|
|
|
&.disabled {
|
|
|
|
opacity: 0.7;
|
|
|
|
|
|
|
|
&, * {
|
|
|
|
cursor: not-allowed !important;
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|