fix: lint errors for textarea #48

Closed
norm wants to merge 1 commit from (deleted):fix/textarea into main

View file

@ -12,7 +12,7 @@
:readonly="readonly"
:placeholder="placeholder"
:pattern="pattern"
:autocomplete="autocomplete"
:autocomplete="autocompleteValue"
:spellcheck="spellcheck"
@focus="focused = true"
@blur="focused = false"
@ -27,7 +27,7 @@
</template>
<script lang="ts" setup>
import { defineComponent, onMounted, onUnmounted, nextTick, ref, watch, computed, toRefs } from 'vue';
import { onMounted, nextTick, ref, watch, computed, toRefs } from 'vue';
import { debounce } from 'throttle-debounce';
import MkButton from '@/components/ui/button.vue';
import { i18n } from '@/i18n';
@ -62,34 +62,38 @@ const props = withDefaults(defineProps<{
manualSave: false,
});
const { modelValue, autofocus } = toRefs(props);
const { modelValue } = toRefs(props);
// modelValue is read only, so a separate ref is needed.
const v = $ref(modelValue.value);
const focused = $ref(false);
const changed = $ref(false);
const invalid = $ref(false);
const filled = computed(() => modelValue.value !== '' && modelValue.value != null);
const inputEl = $ref(null);
const focused = ref(false);
const changed = ref(false);
const invalid = ref(false);
Review

The contribution guide says to use ref sugar (i.e. $ref), which means we can avoid writing .value everywhere. However I forgot that this means the variable may not be const because it will be assigned to. The proper solution should be to use let instead of const.

The contribution guide says to use ref sugar (i.e. `$ref`), which means we can avoid writing `.value` everywhere. However I forgot that this means the variable may not be `const` because it will be assigned to. The proper solution should be to use `let` instead of `const`.
const inputEl = ref<HTMLTextAreaElement | null>(null);
const autocompleteValue = computed(() => props.autocomplete ? 'on' : 'off');
const focus = () => inputEl.focus();
const onInput = evt => {
changed = true;
const focus = (): void => {
if (inputEl.value) {
inputEl.value.focus();
}
};
const onInput = (evt: any): void => {
changed.value = true;
emit('change', evt);
};
const onKeydown = (evt: KeyboardEvent) => {
const onKeydown = (evt: KeyboardEvent): void => {
emit('keydown', evt);
if (evt.code === 'Enter') {
emit('enter');
}
};
const updated = () => {
changed = false;
const updated = (): void => {
changed.value = false;
emit('update:modelValue', v);
};
const debouncedUpdated = debounce(1000, updated);
watch(modelValue, newValue => {
watch(modelValue, () => {
if (!props.manualSave) {
if (props.debounce) {
debouncedUpdated();
@ -98,14 +102,13 @@ watch(modelValue, newValue => {
}
}
invalid = inputEl.validity.badInput;
invalid.value = inputEl.value?.validity.badInput || true;
Review

Not sure if this should default to true or false

Not sure if this should default to true or false
Review

Either way, using a logical or here is not the right solution. You probably meant to use ?? instead.

Since this ref will only be null until the element is loaded it should be better to default it to false.

Either way, using a logical or here is not the right solution. You probably meant to use `??` instead. Since this ref will only be `null` until the element is loaded it should be better to default it to `false`.
});
onMounted(() => {
nextTick(() => {
if (props.autofocus) {
inputEl.focus();
if (props.autofocus && inputEl.value) {
inputEl.value.focus();
}
});
});