misskey/packages/frontend/src/components/MkSelect.vue

295 lines
5.9 KiB
Vue
Raw Normal View History

<template>
2021-09-29 15:50:45 +00:00
<div class="vblkjoeq">
<div class="label" @click="focus"><slot name="label"></slot></div>
2023-01-08 02:12:11 +00:00
<div ref="container" class="input" :class="{ inline, disabled, focused }" @mousedown.prevent="show">
2021-11-19 10:36:12 +00:00
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
<select
ref="inputEl"
2021-12-25 16:42:50 +00:00
v-model="v"
v-adaptive-border
2021-11-19 10:36:12 +00:00
class="select"
:disabled="disabled"
2021-09-29 15:50:45 +00:00
:required="required"
:readonly="readonly"
:placeholder="placeholder"
@focus="focused = true"
@blur="focused = false"
2021-09-29 15:50:45 +00:00
@input="onInput"
>
<slot></slot>
</select>
2023-01-06 01:11:47 +00:00
<div ref="suffixEl" class="suffix"><i class="ti ti-chevron-down" :class="[$style.chevron, { [$style.chevronOpening]: opening }]"></i></div>
</div>
2021-09-29 15:50:45 +00:00
<div class="caption"><slot name="caption"></slot></div>
<MkButton v-if="manualSave && changed" primary @click="updated"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs, VNode, useSlots } from 'vue';
import MkButton from '@/components/MkButton.vue';
2021-11-11 17:02:25 +00:00
import * as os from '@/os';
import { useInterval } from '@/scripts/use-interval';
2022-07-20 13:24:26 +00:00
import { i18n } from '@/i18n';
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);
2023-01-06 01:11:47 +00:00
const opening = 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 =
2023-01-03 23:32:44 +00:00
props.small ? 34 :
props.large ? 40 :
2022-12-29 08:16:35 +00:00
37;
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 15:50:45 +00:00
watch(v, newValue => {
if (!props.manualSave) {
updated();
}
2021-09-29 15:50:45 +00:00
invalid.value = inputEl.value.validity.badInput;
});
2021-09-29 15:50:45 +00: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,
});
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
2023-01-08 01:24:30 +00:00
function show(ev: MouseEvent) {
focused.value = true;
2023-01-06 01:11:47 +00:00
opening.value = true;
2021-09-29 15:50:45 +00:00
const menu = [];
let options = slots.default!();
2021-10-21 21:23:23 +00:00
const pushOption = (option: VNode) => {
menu.push({
text: option.children,
active: computed(() => v.value === option.props.value),
action: () => {
v.value = option.props.value;
},
});
};
2021-10-21 21:23:23 +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 if (vnode.props == null) { // v-if で条件が false のときにこうなる
// nop?
} else {
const option = vnode;
pushOption(option);
}
}
};
scanOptions(options);
os.popupMenu(menu, container.value, {
width: container.value.offsetWidth,
2023-01-06 01:11:47 +00:00
onClosing: () => {
opening.value = false;
},
}).then(() => {
focused.value = false;
});
2023-01-08 01:24:30 +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;
}
}
> .input {
position: relative;
2021-10-21 21:23:23 +00:00
cursor: pointer;
2021-10-21 21:23:23 +00:00
&:hover {
> .select {
2021-12-25 16:42:50 +00:00
border-color: var(--inputBorderHover) !important;
2021-10-21 21:23:23 +00:00
}
}
> .select {
2021-09-29 15:50:45 +00:00
appearance: none;
-webkit-appearance: none;
display: block;
height: v-bind("height + 'px'");
width: 100%;
2021-09-29 15:50:45 +00:00
margin: 0;
padding: 0 12px;
font: inherit;
font-weight: normal;
font-size: 1em;
2021-09-29 15:50:45 +00:00
color: var(--fg);
2021-12-25 16:42:50 +00:00
background: var(--panel);
2021-11-28 11:07:37 +00:00
border: solid 1px var(--panel);
2021-09-29 15:50:45 +00:00
border-radius: 6px;
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;
2022-06-30 14:07:45 +00:00
user-select: none;
}
> .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;
font-size: 1em;
height: v-bind("height + 'px'");
pointer-events: none;
&:empty {
display: none;
}
> * {
2021-09-29 15:50:45 +00:00
display: inline-block;
min-width: 16px;
2021-09-29 15:50:45 +00:00
max-width: 150px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
> .prefix {
2021-09-29 15:50:45 +00:00
left: 0;
padding-right: 6px;
}
> .suffix {
2021-09-29 15:50:45 +00:00
right: 0;
padding-left: 6px;
}
&.inline {
display: inline-block;
margin: 0;
}
&.focused {
> select {
2021-12-25 16:42:50 +00:00
border-color: var(--accent) !important;
2021-09-29 15:50:45 +00:00
}
}
&.disabled {
opacity: 0.7;
2021-09-29 15:50:45 +00:00
&, * {
cursor: not-allowed !important;
}
}
}
}
</style>
2023-01-06 01:11:47 +00:00
<style lang="scss" module>
.chevron {
2023-01-08 01:24:30 +00:00
transition: transform 0.1s ease-out;
2023-01-06 01:11:47 +00:00
}
.chevronOpening {
transform: rotateX(180deg);
}
</style>