File size: 2,292 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<template>
    <Alert
        v-if="descriptionToUse !== '' || $slots.default !== undefined"
        type="light"
        class="lh-2"
        extraClasses="p-0 m-0"
        style="padding: 0; margin: 0; line-height: 1.1em; margin-top: 4px"
    >
        <small>
            <slot></slot>
            <span v-if="$slots.default !== undefined">&nbsp;</span>
            <span
                v-if="descriptionToUse !== ''"
                v-html="descriptionToUse"
            ></span>
        </small>
    </Alert>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import Alert from "@/UI/Layout/Alert.vue";
import { Prop, Watch } from "vue-property-decorator";
import { isSentence } from "@/Core/Utils/StringUtils";

/**
 * FormElementDescription component
 */
@Options({
    components: {
        Alert,
    },
})
export default class FormElementDescription extends Vue {
    // NOTE: Prefer description, not the slot, because description is subject to
    // extra validation.
    @Prop({}) description!: string;
    @Prop({ default: "" }) warning!: string;
    @Prop({ default: true }) validate!: boolean;

    /**
     * Validate the description when it changes.
     *
     * @param {string} newVal The new description.
     */
    @Watch("description")
    onDescriptionChanged(newVal: string) {
        if (this.validate && !isSentence(newVal)) {
            const msg =
                "FormElementDescription: description must be a sentence (start with capital letter, end with punctuation): " +
                newVal;
            console.error(msg);
            throw new Error(msg);
        }
    }

    /**
     * The description to use.
     *
     * @returns {string} The description to use.
     */
    get descriptionToUse(): string {
        let desc = this.description;

        if (desc === undefined) desc = "";

        if (this.warning !== "") {
            desc = `${desc} <span class="text-danger">${this.warning}</span>`;
        }
        return desc;
    }

    /**
     * Validate the description when the component is mounted.
     */
    mounted() {
        this.onDescriptionChanged(this.description);
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style lang="scss" scoped></style>