hippofish/packages/client/src/components/form/select.vue

327 lines
6.3 KiB
Vue
Raw Normal View History

<template>
2023-04-08 02:01:42 +02:00
<div class="vblkjoeq">
<div class="label" @click="focus"><slot name="label"></slot></div>
<div
ref="container"
class="input"
:class="{ inline, disabled, focused }"
@mousedown.prevent="show"
>
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
<select
ref="inputEl"
v-model="v"
v-adaptive-border
class="select"
:disabled="disabled"
:required="required"
:readonly="readonly"
:placeholder="placeholder"
@focus="focused = true"
@blur="focused = false"
@input="onInput"
2023-04-08 02:01:42 +02:00
>
<slot></slot>
</select>
<div ref="suffixEl" class="suffix">
<i
class="ph-caret-down ph-lg"
:class="[
$style.chevron,
{ [$style.chevronOpening]: opening },
]"
></i>
2023-04-08 02:01:42 +02:00
</div>
</div>
<div class="caption"><slot name="caption"></slot></div>
2023-04-08 02:01:42 +02:00
<MkButton v-if="manualSave && changed" primary @click="updated"
><i :class="icon('ph-floppy-disk-back')"></i>
2023-04-08 02:01:42 +02:00
{{ i18n.ts.save }}</MkButton
>
</div>
</template>
<script lang="ts" setup>
2023-07-17 00:32:32 +02:00
import type { VNode } from "vue";
2023-04-08 02:01:42 +02:00
import {
2023-07-17 00:32:32 +02:00
computed,
2023-04-08 02:01:42 +02:00
nextTick,
2023-07-17 00:32:32 +02:00
onMounted,
2023-04-08 02:01:42 +02:00
ref,
toRefs,
useSlots,
2023-07-17 00:32:32 +02:00
watch,
2023-04-08 02:01:42 +02:00
} from "vue";
import MkButton from "@/components/MkButton.vue";
import * as os from "@/os";
import { useInterval } from "@/scripts/use-interval";
import { i18n } from "@/i18n";
import icon from "@/scripts/icon";
2024-04-04 12:48:23 +02:00
import type { MenuItem } from "@/types/menu";
const props = defineProps<{
modelValue: string | null;
required?: boolean;
readonly?: boolean;
disabled?: boolean;
placeholder?: string;
autofocus?: boolean;
inline?: boolean;
manualSave?: boolean;
small?: boolean;
large?: boolean;
}>();
const emit = defineEmits<{
2023-04-08 02:01:42 +02:00
(ev: "change", _ev: KeyboardEvent): void;
(ev: "update:modelValue", value: string | null): void;
}>();
const slots = useSlots();
const { modelValue, autofocus } = toRefs(props);
const v = ref(modelValue.value);
const focused = ref(false);
const opening = ref(false);
const changed = ref(false);
const invalid = ref(false);
2024-04-04 12:48:23 +02:00
const inputEl = ref<HTMLInputElement | null>(null);
const prefixEl = ref<HTMLElement | null>(null);
const suffixEl = ref<HTMLElement | null>(null);
const container = ref<HTMLElement | null>(null);
const height = props.small ? 33 : props.large ? 39 : 36;
2024-04-04 12:48:23 +02:00
const focus = () => inputEl.value!.focus();
const onInput = (ev) => {
changed.value = true;
2023-04-08 02:01:42 +02:00
emit("change", ev);
};
const updated = () => {
changed.value = false;
2023-04-08 02:01:42 +02:00
emit("update:modelValue", v.value);
};
2023-04-08 02:01:42 +02:00
watch(modelValue, (newValue) => {
v.value = newValue;
});
2021-09-29 17:50:45 +02:00
2024-04-04 12:48:23 +02:00
watch(v, (_newValue) => {
if (!props.manualSave) {
updated();
}
2021-09-29 17:50:45 +02:00
2024-04-04 12:48:23 +02:00
invalid.value = inputEl.value!.validity.badInput;
});
2021-09-29 17:50:45 +02:00
// このコンポーネントが作成された時、非表示状態である場合がある
// 非表示状態だと要素の幅などは0になってしまうので、定期的に計算する
2023-04-08 02:01:42 +02:00
useInterval(
() => {
2024-04-04 12:48:23 +02:00
if (inputEl.value == null) return;
2023-04-08 02:01:42 +02:00
if (prefixEl.value) {
if (prefixEl.value.offsetWidth) {
2024-04-04 12:48:23 +02:00
inputEl.value.style.paddingLeft = `${prefixEl.value.offsetWidth}px`;
2023-04-08 02:01:42 +02:00
}
}
2023-04-08 02:01:42 +02:00
if (suffixEl.value) {
if (suffixEl.value.offsetWidth) {
2024-04-04 12:48:23 +02:00
inputEl.value.style.paddingRight = `${suffixEl.value.offsetWidth}px`;
2023-04-08 02:01:42 +02:00
}
}
2023-04-08 02:01:42 +02:00
},
100,
{
immediate: true,
afterMounted: true,
2023-07-06 03:28:27 +02:00
},
2023-04-08 02:01:42 +02:00
);
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
2024-04-04 12:48:23 +02:00
function show(_ev: MouseEvent) {
focused.value = true;
opening.value = true;
2021-09-29 17:50:45 +02:00
2024-04-04 12:48:23 +02:00
const menu: MenuItem[] = [];
2023-07-17 00:32:32 +02:00
const options = slots.default!();
2021-10-21 23:23:23 +02:00
const pushOption = (option: VNode) => {
menu.push({
2024-04-04 12:48:23 +02:00
text: option.children as string,
active: computed(() => v.value === option.props?.value).value,
action: () => {
2024-04-04 12:48:23 +02:00
v.value = option.props?.value;
},
});
};
2021-10-21 23:23:23 +02:00
const scanOptions = (options: VNode[]) => {
for (const vnode of options) {
2023-04-08 02:01:42 +02:00
if (vnode.type === "optgroup") {
const optgroup = vnode;
menu.push({
2023-04-08 02:01:42 +02:00
type: "label",
2024-04-04 12:48:23 +02:00
text: optgroup.props?.label,
});
2024-04-04 12:48:23 +02:00
scanOptions(optgroup.children as VNode[]);
2023-04-08 02:01:42 +02:00
} else if (Array.isArray(vnode.children)) {
// 何故かフラグメントになってくることがある
const fragment = vnode;
2024-04-04 12:48:23 +02:00
scanOptions(fragment.children as VNode[]);
2023-04-08 02:01:42 +02:00
} else if (vnode.props == null) {
// v-if で条件が false のときにこうなる
// nop?
} else {
const option = vnode;
pushOption(option);
}
}
};
scanOptions(options);
2024-04-04 12:48:23 +02:00
os.popupMenu(menu, container.value!, {
width: container.value!.offsetWidth,
// onClosing: () => {
// opening.value = false;
// },
}).then(() => {
2024-04-04 12:48:23 +02:00
opening.value = false;
focused.value = false;
});
}
</script>
<style lang="scss" scoped>
2021-09-29 17:50:45 +02:00
.vblkjoeq {
> .label {
font-size: 0.85em;
padding: 0 0 8px 0;
user-select: none;
&:empty {
display: none;
}
}
> .caption {
font-size: 0.85em;
padding: 8px 0 0 0;
color: var(--fgTransparentWeak);
&:empty {
display: none;
2023-05-15 22:19:33 +02:00
}
}
> .input {
position: relative;
cursor: pointer;
&:hover {
2023-05-15 22:19:33 +02:00
> .select {
border-color: var(--inputBorderHover) !important;
2023-05-15 22:19:33 +02:00
}
}
> .select {
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);
border-radius: 6px;
outline: none;
box-shadow: none;
box-sizing: border-box;
cursor: pointer;
transition: border-color 0.1s ease-out;
pointer-events: none;
user-select: none;
}
> .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'");
pointer-events: none;
&:empty {
display: none;
2023-05-15 22:19:33 +02:00
}
> * {
2021-09-29 17:50:45 +02:00
display: inline-block;
min-width: 16px;
max-width: 150px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
> .prefix {
left: 0;
padding-right: 6px;
}
> .suffix {
right: 0;
padding-left: 6px;
}
&.inline {
display: inline-block;
margin: 0;
}
&.focused {
> select {
border-color: var(--accent) !important;
2021-09-29 17:50:45 +02:00
}
}
&.disabled {
opacity: 0.7;
&,
* {
cursor: not-allowed !important;
2021-09-29 17:50:45 +02:00
}
}
}
}
</style>
<style lang="scss" module>
.chevron {
transition: transform 0.1s ease-out;
}
.chevronOpening {
transform: rotateX(180deg);
}
</style>