iceshrimp-legacy/packages/client/src/components/form/textarea.vue

281 lines
4.6 KiB
Vue
Raw Normal View History

<template>
2023-04-08 02:01:42 +02:00
<div class="adhpbeos">
2023-05-15 22:19:33 +02:00
<label>
<span class="label">
<slot name="label"></slot>
</span>
<div class="input" :class="{ disabled, focused, tall, pre }">
<textarea
ref="inputEl"
v-model="v"
v-adaptive-border
:class="{ code, _monospace: code }"
: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>
<div class="caption"><slot name="caption"></slot></div>
</label>
2021-02-13 04:28:26 +01:00
2023-04-08 02:01:42 +02:00
<MkButton
v-if="manualSave && changed"
primary
class="save"
@click="updated"
><i class="ph-floppy-disk-back ph-bold ph-lg"></i>
{{ i18n.ts.save }}</MkButton
>
</div>
</template>
<script lang="ts">
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
defineComponent,
2023-07-17 00:32:32 +02:00
nextTick,
2023-04-08 02:01:42 +02:00
onMounted,
onUnmounted,
ref,
toRefs,
2023-07-17 00:32:32 +02:00
watch,
2023-04-08 02:01:42 +02:00
} from "vue";
import { debounce } from "throttle-debounce";
import MkButton from "@/components/MkButton.vue";
import { i18n } from "@/i18n";
export default defineComponent({
2021-02-13 04:28:26 +01:00
components: {
2021-09-29 17:50:45 +02:00
MkButton,
2021-02-13 04:28:26 +01:00
},
2021-09-29 17:50:45 +02:00
props: {
2021-09-29 17:50:45 +02:00
modelValue: {
2022-07-20 15:24:26 +02:00
required: true,
2021-09-29 17:50:45 +02:00
},
type: {
type: String,
2022-07-20 15:24:26 +02:00
required: false,
},
required: {
type: Boolean,
2022-07-20 15:24:26 +02:00
required: false,
},
readonly: {
type: Boolean,
2022-07-20 15:24:26 +02:00
required: false,
},
2021-09-29 17:50:45 +02:00
disabled: {
type: Boolean,
2022-07-20 15:24:26 +02:00
required: false,
2021-09-29 17:50:45 +02:00
},
pattern: {
type: String,
2022-07-20 15:24:26 +02:00
required: false,
},
2021-09-29 17:50:45 +02:00
placeholder: {
type: String,
2022-07-20 15:24:26 +02:00
required: false,
},
2021-09-29 17:50:45 +02:00
autofocus: {
type: Boolean,
required: false,
2022-07-20 15:24:26 +02:00
default: false,
2021-09-29 17:50:45 +02:00
},
autocomplete: {
2022-07-20 15:24:26 +02:00
required: false,
2021-09-29 17:50:45 +02:00
},
spellcheck: {
2022-07-20 15:24:26 +02:00
required: false,
2021-09-29 17:50:45 +02:00
},
code: {
type: Boolean,
2022-07-20 15:24:26 +02:00
required: false,
},
tall: {
type: Boolean,
required: false,
2022-07-20 15:24:26 +02:00
default: false,
},
pre: {
type: Boolean,
required: false,
2022-07-20 15:24:26 +02:00
default: false,
},
2021-09-29 17:50:45 +02:00
debounce: {
type: Boolean,
required: false,
2022-07-20 15:24:26 +02:00
default: false,
2021-09-29 17:50:45 +02:00
},
2021-02-13 04:28:26 +01:00
manualSave: {
type: Boolean,
required: false,
2022-07-20 15:24:26 +02:00
default: false,
},
},
2021-09-29 17:50:45 +02:00
2023-04-08 02:01:42 +02:00
emits: ["change", "keydown", "enter", "update:modelValue"],
2021-09-29 17:50:45 +02:00
2021-02-13 04:28:26 +01:00
setup(props, context) {
2021-09-29 17:50:45 +02:00
const { modelValue, autofocus } = toRefs(props);
const v = ref(modelValue.value);
const focused = ref(false);
2021-02-13 04:28:26 +01:00
const changed = ref(false);
2021-09-29 17:50:45 +02:00
const invalid = ref(false);
2023-04-08 02:01:42 +02:00
const filled = computed(() => v.value !== "" && v.value != null);
2021-02-13 04:28:26 +01:00
const inputEl = ref(null);
2021-09-29 17:50:45 +02:00
2021-02-13 04:28:26 +01:00
const focus = () => inputEl.value.focus();
const onInput = (ev) => {
changed.value = true;
2023-04-08 02:01:42 +02:00
context.emit("change", ev);
2021-02-13 04:28:26 +01:00
};
2021-09-29 17:50:45 +02:00
const onKeydown = (ev: KeyboardEvent) => {
2023-04-08 02:01:42 +02:00
context.emit("keydown", ev);
2021-09-29 17:50:45 +02:00
2023-04-08 02:01:42 +02:00
if (ev.code === "Enter") {
context.emit("enter");
2021-09-29 17:50:45 +02:00
}
};
2021-02-13 04:28:26 +01:00
const updated = () => {
changed.value = false;
2023-04-08 02:01:42 +02:00
context.emit("update:modelValue", v.value);
2021-02-13 04:28:26 +01:00
};
2021-09-29 17:50:45 +02:00
const debouncedUpdated = debounce(1000, updated);
2023-04-08 02:01:42 +02:00
watch(modelValue, (newValue) => {
2021-02-13 04:28:26 +01:00
v.value = newValue;
});
watch($$(v), () => {
2021-02-13 04:28:26 +01:00
if (!props.manualSave) {
2021-09-29 17:50:45 +02:00
if (props.debounce) {
debouncedUpdated();
} else {
updated();
}
2021-02-13 04:28:26 +01:00
}
2021-09-29 17:50:45 +02:00
invalid.value = inputEl.value.validity.badInput;
2021-02-13 04:28:26 +01:00
});
2021-09-29 17:50:45 +02:00
onMounted(() => {
nextTick(() => {
if (autofocus.value) {
focus();
}
});
});
return {
2021-02-13 04:28:26 +01:00
v,
2021-09-29 17:50:45 +02:00
focused,
invalid,
2021-02-13 04:28:26 +01:00
changed,
2021-09-29 17:50:45 +02:00
filled,
inputEl,
2021-02-13 04:28:26 +01:00
focus,
onInput,
2021-09-29 17:50:45 +02:00
onKeydown,
updated,
2022-07-20 17:32:41 +02:00
i18n,
2021-02-13 04:28:26 +01:00
};
2021-09-29 17:50:45 +02:00
},
});
</script>
<style lang="scss" scoped>
2021-09-29 17:50:45 +02:00
.adhpbeos {
2023-05-15 22:19:33 +02:00
> label {
> .label {
font-size: 0.85em;
padding: 0 0 8px 0;
user-select: none;
&:empty {
display: none;
}
2021-09-29 17:50:45 +02:00
}
2023-05-15 22:19:33 +02:00
> .caption {
font-size: 0.85em;
padding: 8px 0 0 0;
color: var(--fgTransparentWeak);
2023-05-15 22:19:33 +02:00
&:empty {
display: none;
2021-09-29 17:50:45 +02:00
}
}
> .input {
2023-05-15 22:19:33 +02:00
position: relative;
2021-09-29 17:50:45 +02:00
> textarea {
2023-05-15 22:19:33 +02:00
appearance: none;
-webkit-appearance: none;
display: block;
width: 100%;
min-width: 100%;
max-width: 100%;
min-height: 130px;
margin: 0;
padding: 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;
transition: border-color 0.1s ease-out;
2023-05-15 22:19:33 +02:00
&:hover {
border-color: var(--inputBorderHover) !important;
}
}
2023-05-15 22:19:33 +02:00
&.focused {
> textarea {
border-color: var(--accent) !important;
}
2021-09-29 17:50:45 +02:00
}
2023-05-15 22:19:33 +02:00
&.disabled {
opacity: 0.7;
2023-05-15 22:19:33 +02:00
&,
* {
cursor: not-allowed !important;
}
}
2023-05-15 22:19:33 +02:00
&.tall {
> textarea {
min-height: 200px;
}
}
2023-05-15 22:19:33 +02:00
&.pre {
> textarea {
white-space: pre;
}
}
}
}
2021-11-28 12:07:37 +01:00
> .save {
margin: 8px 0 0 0;
}
}
</style>