paricafe/packages/frontend/src/components/MkTextarea.vue

198 lines
3.8 KiB
Vue
Raw Normal View History

<template>
2021-09-29 10:50:45 -05:00
<div class="adhpbeos">
<div class="label" @click="focus"><slot name="label"></slot></div>
<div class="input" :class="{ disabled, focused, tall, pre }">
2022-07-20 08:24:26 -05:00
<textarea
ref="inputEl"
2021-09-29 10:50:45 -05:00
v-model="v"
2021-12-25 10:42:50 -06:00
v-adaptive-border
2021-11-19 04:36:12 -06:00
:class="{ code, _monospace: code }"
2021-09-29 10:50:45 -05:00
:disabled="disabled"
:required="required"
:readonly="readonly"
:placeholder="placeholder"
:pattern="pattern"
:autocomplete="autocomplete"
:spellcheck="spellcheck"
@focus="focused = true"
@blur="focused = false"
@keydown="onKeydown($event)"
@input="onInput"
></textarea>
</div>
2021-09-29 10:50:45 -05:00
<div class="caption"><slot name="caption"></slot></div>
2021-02-12 21:28:26 -06:00
<MkButton v-if="manualSave && changed" primary class="save" @click="updated"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
2021-09-29 10:50:45 -05:00
</div>
</template>
<script lang="ts" setup>
import { onMounted, nextTick, ref, watch, computed, toRefs, shallowRef } from 'vue';
2021-09-29 10:50:45 -05:00
import { debounce } from 'throttle-debounce';
import MkButton from '@/components/MkButton.vue';
2022-07-20 08:24:26 -05:00
import { i18n } from '@/i18n';
const props = defineProps<{
modelValue: string | null;
required?: boolean;
readonly?: boolean;
disabled?: boolean;
pattern?: string;
placeholder?: string;
autofocus?: boolean;
autocomplete?: string;
spellcheck?: boolean;
debounce?: boolean;
manualSave?: boolean;
code?: boolean;
tall?: boolean;
pre?: boolean;
}>();
const emit = defineEmits<{
(ev: 'change', _ev: KeyboardEvent): void;
(ev: 'keydown', _ev: KeyboardEvent): void;
(ev: 'enter'): void;
(ev: 'update:modelValue', value: string): void;
}>();
const { modelValue, autofocus } = toRefs(props);
const v = ref<string>(modelValue.value ?? '');
const focused = ref(false);
const changed = ref(false);
const invalid = ref(false);
const filled = computed(() => v.value !== '' && v.value != null);
const inputEl = shallowRef<HTMLTextAreaElement>();
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
emit('change', ev);
};
const onKeydown = (ev: KeyboardEvent) => {
if (ev.isComposing || ev.key === 'Process' || ev.keyCode === 229) return;
emit('keydown', ev);
if (ev.code === 'Enter') {
emit('enter');
}
};
const updated = () => {
changed.value = false;
emit('update:modelValue', v.value ?? '');
};
const debouncedUpdated = debounce(1000, updated);
watch(modelValue, newValue => {
v.value = newValue;
});
watch(v, newValue => {
if (!props.manualSave) {
if (props.debounce) {
debouncedUpdated();
} else {
updated();
}
}
invalid.value = inputEl.value.validity.badInput;
});
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
</script>
<style lang="scss" scoped>
2021-09-29 10:50:45 -05:00
.adhpbeos {
> .label {
font-size: 0.85em;
2021-11-28 05:07:37 -06:00
padding: 0 0 8px 0;
2021-09-29 10:50:45 -05:00
user-select: none;
&:empty {
display: none;
}
}
> .caption {
2021-11-28 05:07:37 -06:00
font-size: 0.85em;
padding: 8px 0 0 0;
2021-09-29 10:50:45 -05:00
color: var(--fgTransparentWeak);
&:empty {
display: none;
}
}
> .input {
position: relative;
2021-09-29 10:50:45 -05:00
> textarea {
2021-09-29 10:50:45 -05:00
appearance: none;
-webkit-appearance: none;
display: block;
width: 100%;
min-width: 100%;
max-width: 100%;
min-height: 130px;
margin: 0;
2021-09-29 10:50:45 -05:00
padding: 12px;
font: inherit;
font-weight: normal;
font-size: 1em;
2021-09-29 10:50:45 -05:00
color: var(--fg);
2021-12-25 10:42:50 -06:00
background: var(--panel);
border: solid 1px var(--panel);
2021-09-29 10:50:45 -05:00
border-radius: 6px;
outline: none;
box-shadow: none;
2021-09-29 10:50:45 -05:00
box-sizing: border-box;
transition: border-color 0.1s ease-out;
2021-09-29 10:50:45 -05:00
&:hover {
2021-12-25 10:42:50 -06:00
border-color: var(--inputBorderHover) !important;
2021-09-29 10:50:45 -05:00
}
}
&.focused {
> textarea {
2021-12-25 10:42:50 -06:00
border-color: var(--accent) !important;
}
}
2021-09-29 10:50:45 -05:00
&.disabled {
opacity: 0.7;
&, * {
cursor: not-allowed !important;
}
}
&.tall {
> textarea {
min-height: 200px;
}
}
2021-09-29 10:50:45 -05:00
&.pre {
> textarea {
white-space: pre;
}
}
}
2021-11-28 05:07:37 -06:00
> .save {
margin: 8px 0 0 0;
}
}
</style>