WIP update remaining characters to use computed properties.

This commit is contained in:
Bryan Mauger 2023-05-15 09:48:26 -04:00
parent e7edc06515
commit 8859f33bec
2 changed files with 45 additions and 35 deletions

View file

@ -1,20 +1,24 @@
<template> <template>
<div class="textarea-question"> <div class="textarea-question">
<div class="label-wrapper mb-1" :aria-label="TextAreaContentWidget"><!-- Wrap label and span because v-html prevents v-if from displaying if v-if <span> is inside <label>--> <div class="label-wrapper mb-1" :aria-label="TextAreaContentWidget">
<label for="textarea-comments" class="fw-bold" v-html="TextAreaContentWidget"></label> <span v-if="!isRequired" class="fw-normal ms-1">(Optional)</span> <!-- Wrap label and span because v-html prevents v-if from displaying if v-if <span> is inside <label>-->
<label for="textarea-comments" class="fw-bold" v-html="TextAreaContentWidget"></label>
<span v-if="!isRequired" class="fw-normal ms-1">(Optional)</span>
</div> </div>
<textarea <textarea
id="textarea-comments" v-model="text"
class="p-4" @keyup="updateCount"
:maxlength=maxLength id="textarea-comments"
role="textbox" class="p-4"
aria-multiline="true" :maxlength="maxLength"
:aria-required=isRequired> role="textbox"
</textarea> aria-multiline="true"
<p tabindex="0" class="caption mt-2 mb-0" id="charactersremaining"> :aria-required="isRequired">
{{maxLength}}/{{maxLength}} characters remaining </textarea>
</p> <p tabindex="0" class="caption mt-2 mb-0" id="charactersremaining" :style="{ color: countdownColor }">
</div> {{ remainingCount }}/{{ maxLength }} characters remaining
</p>
</div>
</template> </template>
<script> <script>
@ -23,9 +27,17 @@ export default {
props: { props: {
cmsWidgetName: String, cmsWidgetName: String,
isRequired: Boolean, isRequired: Boolean,
maxLength: Number, maxLength: {
type: Number,
default: 250
},
modelValue: String, modelValue: String,
}, },
data() {
return {
text: ''
};
},
setup(props) { setup(props) {
const inputId = !props.customInputId ? `input-${crypto.randomUUID()}` : props.customInputId; const inputId = !props.customInputId ? `input-${crypto.randomUUID()}` : props.customInputId;
@ -35,11 +47,11 @@ export default {
switch (typeof modelValue) { switch (typeof modelValue) {
case "number": case "number":
initialValue = modelValue; initialValue = modelValue;
break; break;
default: default:
initialValue = modelValue && modelValue.length > 0 ? modelValue : ""; initialValue = modelValue && modelValue.length > 0 ? modelValue : "";
break; break;
} }
const fieldOptions = { const fieldOptions = {
@ -60,18 +72,19 @@ export default {
this.$emit("update:modelValue", newValue); this.$emit("update:modelValue", newValue);
}, },
}, },
remainingCount() {
return this.maxLength - this.text.length;
},
countdownColor() {
return this.remainingCount <= (this.maxLength * 0.1) ? 'red' : '';
}
}, },
mounted: function () { //Remaining characters countdown methods: {
const my_textarea = document.getElementById("textarea-comments"); updateCount() {
const remaining = document.getElementById("charactersremaining"); if (this.text.length > this.maxLength) {
const remain = this.maxLength; this.text = this.text.slice(0, this.maxLength);
}
my_textarea.addEventListener("input", () => { }
const total = remain - my_textarea.value.length;
const color = total < remain * 0.1 ? "red" : null; //Change text to red when the total remaining goes below 10%;
remaining.innerHTML = `${total}/${this.maxLength} characters remaining`;
remaining.style.color = color;
});
}, },
}; };
</script> </script>

View file

@ -1,8 +1,5 @@
<template> <template>
<textareaquestion <textareaquestion cmsWidgetName="TextAreaContentWidget" maxLength="250" />
cmsWidgetName="TextAreaContentWidget"
maxLength=250
/>
</template> </template>
<script> <script>
@ -15,7 +12,7 @@ import { getDamageString, isGlassAvailableForCarId } from "@/helpers/damage-help
export default { export default {
name: "customer-details", name: "customer-details",
components: { components: {
textareaquestion textareaquestion,
}, },
async beforeRouteEnter(to, from, next) { async beforeRouteEnter(to, from, next) {
// Call APIs // Call APIs
@ -40,6 +37,6 @@ export default {
textAreaLabelCopy() { textAreaLabelCopy() {
return this.getCmsContent("TextAreaContentWidget", "QuestionText"); return this.getCmsContent("TextAreaContentWidget", "QuestionText");
}, },
} },
}; };
</script> </script>