Sharkey/packages/client/src/components/form/input.vue

322 lines
6 KiB
Vue
Raw Normal View History

<template>
2021-09-29 18:50:45 +03:00
<div class="matxzzsk">
<div class="label" @click="focus"><slot name="label"></slot></div>
<div class="input" :class="{ inline, disabled, focused }">
2021-11-19 12:36:12 +02:00
<div ref="prefixEl" class="prefix"><slot name="prefix"></slot></div>
<input
ref="inputEl"
2021-09-29 18:50:45 +03:00
v-model="v"
2021-12-25 18:42:50 +02:00
v-adaptive-border
2021-11-19 12:36:12 +02:00
:type="type"
2021-09-29 18:50:45 +03:00
:disabled="disabled"
:required="required"
:readonly="readonly"
:placeholder="placeholder"
:pattern="pattern"
:autocomplete="autocomplete"
:spellcheck="spellcheck"
:step="step"
2021-11-19 12:36:12 +02:00
:list="id"
2021-09-29 18:50:45 +03:00
@focus="focused = true"
@blur="focused = false"
@keydown="onKeydown($event)"
@input="onInput"
>
2021-11-19 12:36:12 +02:00
<datalist v-if="datalist" :id="id">
2021-09-29 18:50:45 +03:00
<option v-for="data in datalist" :value="data"/>
</datalist>
2021-11-19 12:36:12 +02:00
<div ref="suffixEl" class="suffix"><slot name="suffix"></slot></div>
</div>
2021-09-29 18:50:45 +03:00
<div class="caption"><slot name="caption"></slot></div>
2021-02-13 05:28:26 +02:00
2021-11-28 13:07:37 +02:00
<MkButton v-if="manualSave && changed" primary class="save" @click="updated"><i class="fas fa-check"></i> {{ $ts.save }}</MkButton>
2021-09-29 18:50:45 +03:00
</div>
</template>
<script lang="ts">
import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs } from 'vue';
2021-09-29 18:50:45 +03:00
import { debounce } from 'throttle-debounce';
import MkButton from '@/components/ui/button.vue';
import { useInterval } from '@/scripts/use-interval';
export default defineComponent({
2021-02-13 05:28:26 +02:00
components: {
2021-09-29 18:50:45 +03:00
MkButton,
},
2021-09-29 18:50:45 +03:00
props: {
2021-09-29 18:50:45 +03:00
modelValue: {
required: true,
},
type: {
type: String,
required: false,
},
required: {
type: Boolean,
required: false,
},
readonly: {
type: Boolean,
required: false,
},
disabled: {
type: Boolean,
required: false,
},
pattern: {
type: String,
required: false,
},
placeholder: {
type: String,
required: false,
},
autofocus: {
type: Boolean,
required: false,
default: false,
},
autocomplete: {
required: false,
},
spellcheck: {
required: false,
},
step: {
required: false,
},
datalist: {
type: Array,
required: false,
},
inline: {
type: Boolean,
required: false,
default: false,
},
2021-09-29 18:50:45 +03:00
debounce: {
type: Boolean,
required: false,
default: false,
2021-09-29 18:50:45 +03:00
},
2021-02-13 05:28:26 +02:00
manualSave: {
type: Boolean,
required: false,
default: false,
},
},
2021-09-29 18:50:45 +03:00
emits: ['change', 'keydown', 'enter', 'update:modelValue'],
setup(props, context) {
2021-09-29 18:50:45 +03:00
const { modelValue, type, autofocus } = toRefs(props);
const v = ref(modelValue.value);
const id = Math.random().toString(); // TODO: uuid?
const focused = ref(false);
const changed = ref(false);
const invalid = ref(false);
const filled = computed(() => v.value !== '' && v.value != null);
2021-11-28 13:07:37 +02:00
const inputEl = ref<HTMLElement>();
const prefixEl = ref<HTMLElement>();
const suffixEl = ref<HTMLElement>();
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
context.emit('change', ev);
};
const onKeydown = (ev: KeyboardEvent) => {
context.emit('keydown', ev);
if (ev.code === 'Enter') {
context.emit('enter');
}
};
2021-02-13 05:28:26 +02:00
const updated = () => {
changed.value = false;
if (type.value === 'number') {
2021-09-29 18:50:45 +03:00
context.emit('update:modelValue', parseFloat(v.value));
2021-02-13 05:28:26 +02:00
} else {
2021-09-29 18:50:45 +03:00
context.emit('update:modelValue', v.value);
2021-02-13 05:28:26 +02:00
}
};
2021-09-29 18:50:45 +03:00
const debouncedUpdated = debounce(1000, updated);
watch(modelValue, newValue => {
v.value = newValue;
});
watch(v, newValue => {
2021-02-13 05:28:26 +02:00
if (!props.manualSave) {
2021-09-29 18:50:45 +03:00
if (props.debounce) {
debouncedUpdated();
} else {
updated();
}
}
invalid.value = inputEl.value.validity.badInput;
});
// このコンポーネントが作成された時、非表示状態である場合がある
// 非表示状態だと要素の幅などは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();
}
});
});
return {
id,
v,
focused,
invalid,
changed,
filled,
inputEl,
prefixEl,
suffixEl,
focus,
onInput,
onKeydown,
2021-02-13 05:28:26 +02:00
updated,
};
},
});
</script>
<style lang="scss" scoped>
2021-09-29 18:50:45 +03:00
.matxzzsk {
> .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;
}
}
> .input {
2021-09-29 18:50:45 +03:00
$height: 42px;
position: relative;
> input {
2021-09-29 18:50:45 +03:00
appearance: none;
-webkit-appearance: none;
display: block;
height: $height;
width: 100%;
margin: 0;
2021-09-29 18:50:45 +03:00
padding: 0 12px;
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);
border: solid 1px var(--panel);
2021-09-29 18:50:45 +03:00
border-radius: 6px;
outline: none;
box-shadow: none;
box-sizing: border-box;
2021-09-29 18:50:45 +03:00
transition: border-color 0.1s ease-out;
2021-09-29 18:50:45 +03:00
&:hover {
2021-12-25 18:42:50 +02:00
border-color: var(--inputBorderHover) !important;
}
}
> .prefix,
> .suffix {
2021-09-29 18:50:45 +03:00
display: flex;
align-items: center;
position: absolute;
z-index: 1;
top: 0;
2021-09-29 18:50:45 +03:00
padding: 0 12px;
font-size: 1em;
2021-09-29 18:50:45 +03:00
height: $height;
pointer-events: none;
&:empty {
display: none;
}
> * {
display: inline-block;
min-width: 16px;
max-width: 150px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
> .prefix {
left: 0;
2021-09-29 18:50:45 +03:00
padding-right: 6px;
}
> .suffix {
right: 0;
2021-09-29 18:50:45 +03:00
padding-left: 6px;
}
2021-09-29 18:50:45 +03:00
&.inline {
display: inline-block;
margin: 0;
}
&.focused {
> input {
2021-12-25 18:42:50 +02:00
border-color: var(--accent) !important;
2021-09-29 18:50:45 +03:00
//box-shadow: 0 0 0 4px var(--focus);
}
}
2021-09-29 18:50:45 +03:00
&.disabled {
opacity: 0.7;
2021-09-29 18:50:45 +03:00
&, * {
cursor: not-allowed !important;
}
}
}
2021-11-28 13:07:37 +02:00
> .save {
margin: 8px 0 0 0;
}
}
</style>