2020-04-10 11:12:37 +00:00
|
|
|
<template>
|
2022-04-01 15:29:03 +00:00
|
|
|
<div
|
|
|
|
class="form chat-bubble agent"
|
|
|
|
:class="$dm('bg-white', 'dark:bg-slate-700')"
|
|
|
|
>
|
2020-04-10 11:12:37 +00:00
|
|
|
<form @submit.prevent="onSubmit">
|
2021-06-08 11:02:01 +00:00
|
|
|
<div
|
|
|
|
v-for="item in items"
|
|
|
|
:key="item.key"
|
|
|
|
class="form-block"
|
|
|
|
:class="{
|
|
|
|
'has-submitted': hasSubmitted,
|
|
|
|
}"
|
|
|
|
>
|
2022-04-01 15:29:03 +00:00
|
|
|
<label :class="$dm('text-black-900', 'dark:text-slate-50')">{{
|
|
|
|
item.label
|
|
|
|
}}</label>
|
2020-04-10 11:12:37 +00:00
|
|
|
<input
|
2021-06-08 11:02:01 +00:00
|
|
|
v-if="item.type === 'email'"
|
|
|
|
v-model="formValues[item.name]"
|
2022-04-01 15:29:03 +00:00
|
|
|
:class="inputColor"
|
2021-06-08 11:02:01 +00:00
|
|
|
:type="item.type"
|
|
|
|
:pattern="item.regex"
|
|
|
|
:title="item.title"
|
|
|
|
:required="item.required && 'required'"
|
|
|
|
:name="item.name"
|
|
|
|
:placeholder="item.placeholder"
|
|
|
|
:disabled="!!submittedValues.length"
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
v-else-if="item.type === 'text'"
|
2020-04-10 11:12:37 +00:00
|
|
|
v-model="formValues[item.name]"
|
2022-04-01 15:29:03 +00:00
|
|
|
:class="inputColor"
|
2021-06-08 11:02:01 +00:00
|
|
|
:required="item.required && 'required'"
|
|
|
|
:pattern="item.pattern"
|
|
|
|
:title="item.title"
|
2020-04-10 11:12:37 +00:00
|
|
|
:type="item.type"
|
|
|
|
:name="item.name"
|
|
|
|
:placeholder="item.placeholder"
|
|
|
|
:disabled="!!submittedValues.length"
|
|
|
|
/>
|
|
|
|
<textarea
|
|
|
|
v-else-if="item.type === 'text_area'"
|
|
|
|
v-model="formValues[item.name]"
|
2022-04-01 15:29:03 +00:00
|
|
|
:class="inputColor"
|
2021-06-08 11:02:01 +00:00
|
|
|
:required="item.required && 'required'"
|
|
|
|
:title="item.title"
|
2020-04-10 11:12:37 +00:00
|
|
|
:name="item.name"
|
|
|
|
:placeholder="item.placeholder"
|
|
|
|
:disabled="!!submittedValues.length"
|
|
|
|
/>
|
2020-07-08 07:43:48 +00:00
|
|
|
<select
|
|
|
|
v-else-if="item.type === 'select'"
|
|
|
|
v-model="formValues[item.name]"
|
2022-04-01 15:29:03 +00:00
|
|
|
:class="inputColor"
|
2021-06-08 11:02:01 +00:00
|
|
|
:required="item.required && 'required'"
|
2020-07-08 07:43:48 +00:00
|
|
|
>
|
|
|
|
<option
|
|
|
|
v-for="option in item.options"
|
|
|
|
:key="option.key"
|
|
|
|
:value="option.value"
|
|
|
|
>
|
|
|
|
{{ option.label }}
|
|
|
|
</option>
|
|
|
|
</select>
|
2021-06-08 11:02:01 +00:00
|
|
|
<span class="error-message">
|
|
|
|
{{ item.pattern_error || $t('CHAT_FORM.INVALID.FIELD') }}
|
|
|
|
</span>
|
2020-04-10 11:12:37 +00:00
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
v-if="!submittedValues.length"
|
2020-05-04 18:18:47 +00:00
|
|
|
class="button block"
|
2020-04-10 11:12:37 +00:00
|
|
|
type="submit"
|
2020-05-09 16:32:43 +00:00
|
|
|
:style="{ background: widgetColor, borderColor: widgetColor }"
|
2021-06-08 11:02:01 +00:00
|
|
|
@click="onSubmitClick"
|
2020-04-10 11:12:37 +00:00
|
|
|
>
|
2020-07-08 07:43:48 +00:00
|
|
|
{{ buttonLabel || $t('COMPONENTS.FORM_BUBBLE.SUBMIT') }}
|
2020-04-10 11:12:37 +00:00
|
|
|
</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2020-05-09 16:32:43 +00:00
|
|
|
import { mapGetters } from 'vuex';
|
2022-04-01 15:29:03 +00:00
|
|
|
import darkModeMixin from 'widget/mixins/darkModeMixin.js';
|
|
|
|
|
2020-04-10 11:12:37 +00:00
|
|
|
export default {
|
2022-04-01 15:29:03 +00:00
|
|
|
mixins: [darkModeMixin],
|
2020-04-10 11:12:37 +00:00
|
|
|
props: {
|
2020-07-08 07:43:48 +00:00
|
|
|
buttonLabel: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
2020-04-10 11:12:37 +00:00
|
|
|
items: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [],
|
|
|
|
},
|
|
|
|
submittedValues: {
|
|
|
|
type: Array,
|
|
|
|
default: () => [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
formValues: {},
|
2021-06-08 11:02:01 +00:00
|
|
|
hasSubmitted: false,
|
2020-04-10 11:12:37 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
computed: {
|
2020-05-09 16:32:43 +00:00
|
|
|
...mapGetters({
|
|
|
|
widgetColor: 'appConfig/getWidgetColor',
|
|
|
|
}),
|
2022-04-01 15:29:03 +00:00
|
|
|
inputColor() {
|
|
|
|
return `${this.$dm('bg-white', 'dark:bg-slate-600')}
|
|
|
|
${this.$dm('text-black-900', 'dark:text-slate-50')}`;
|
|
|
|
},
|
2020-04-10 11:12:37 +00:00
|
|
|
isFormValid() {
|
|
|
|
return this.items.reduce((acc, { name }) => {
|
|
|
|
return !!this.formValues[name] && acc;
|
|
|
|
}, true);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
mounted() {
|
2020-05-04 18:18:47 +00:00
|
|
|
if (this.submittedValues.length) {
|
|
|
|
this.updateFormValues();
|
|
|
|
} else {
|
|
|
|
this.setFormDefaults();
|
|
|
|
}
|
2020-04-10 11:12:37 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2021-06-08 11:02:01 +00:00
|
|
|
onSubmitClick() {
|
|
|
|
this.hasSubmitted = true;
|
|
|
|
},
|
2020-04-10 11:12:37 +00:00
|
|
|
onSubmit() {
|
|
|
|
if (!this.isFormValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.$emit('submit', this.formValues);
|
|
|
|
},
|
2020-05-04 18:18:47 +00:00
|
|
|
buildFormObject(formObjectArray) {
|
|
|
|
return formObjectArray.reduce((acc, obj) => {
|
2020-04-10 11:12:37 +00:00
|
|
|
return {
|
|
|
|
...acc,
|
2020-05-04 18:18:47 +00:00
|
|
|
[obj.name]: obj.value || obj.default,
|
2020-04-10 11:12:37 +00:00
|
|
|
};
|
|
|
|
}, {});
|
|
|
|
},
|
2020-05-04 18:18:47 +00:00
|
|
|
updateFormValues() {
|
|
|
|
this.formValues = this.buildFormObject(this.submittedValues);
|
|
|
|
},
|
|
|
|
setFormDefaults() {
|
|
|
|
this.formValues = this.buildFormObject(this.items);
|
|
|
|
},
|
2020-04-10 11:12:37 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped lang="scss">
|
|
|
|
@import '~widget/assets/scss/variables.scss';
|
|
|
|
|
|
|
|
.form {
|
|
|
|
padding: $space-normal;
|
2020-05-04 18:18:47 +00:00
|
|
|
width: 80%;
|
2020-04-10 11:12:37 +00:00
|
|
|
|
|
|
|
.form-block {
|
2020-05-04 18:18:47 +00:00
|
|
|
width: 90%;
|
2020-04-10 11:12:37 +00:00
|
|
|
padding-bottom: $space-small;
|
|
|
|
}
|
|
|
|
|
|
|
|
label {
|
|
|
|
display: block;
|
2020-05-04 18:18:47 +00:00
|
|
|
font-weight: $font-weight-medium;
|
2020-04-10 11:12:37 +00:00
|
|
|
padding: $space-smaller 0;
|
|
|
|
text-transform: capitalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
input,
|
|
|
|
textarea {
|
|
|
|
border-radius: $space-smaller;
|
|
|
|
border: 1px solid $color-border;
|
|
|
|
display: block;
|
2020-07-08 07:43:48 +00:00
|
|
|
font-family: inherit;
|
2020-04-10 11:12:37 +00:00
|
|
|
font-size: $font-size-default;
|
|
|
|
line-height: 1.5;
|
2020-05-04 18:18:47 +00:00
|
|
|
padding: $space-one;
|
|
|
|
width: 100%;
|
2020-04-10 11:12:37 +00:00
|
|
|
|
|
|
|
&:disabled {
|
|
|
|
background: $color-background-light;
|
|
|
|
}
|
|
|
|
}
|
2020-05-04 18:18:47 +00:00
|
|
|
|
2020-07-08 07:43:48 +00:00
|
|
|
textarea {
|
|
|
|
resize: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
width: 110%;
|
|
|
|
padding: $space-smaller;
|
|
|
|
-webkit-appearance: none;
|
|
|
|
-moz-appearance: none;
|
|
|
|
appearance: none;
|
|
|
|
border: 1px solid $color-border;
|
|
|
|
border-radius: $space-smaller;
|
|
|
|
font-family: inherit;
|
|
|
|
font-size: $space-normal;
|
|
|
|
font-weight: normal;
|
|
|
|
line-height: 1.5;
|
|
|
|
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' width='32' height='24' viewBox='0 0 32 24'><polygon points='0,0 32,0 16,24' style='fill: rgb%28110, 111, 115%29'></polygon></svg>");
|
|
|
|
background-origin: content-box;
|
|
|
|
background-position: right -1.6rem center;
|
|
|
|
background-repeat: no-repeat;
|
|
|
|
background-size: 9px 6px;
|
|
|
|
padding-right: 2.4rem;
|
|
|
|
}
|
|
|
|
|
2020-05-04 18:18:47 +00:00
|
|
|
.button {
|
|
|
|
font-size: $font-size-default;
|
|
|
|
}
|
2021-06-08 11:02:01 +00:00
|
|
|
|
|
|
|
.error-message {
|
|
|
|
display: none;
|
|
|
|
margin-top: $space-smaller;
|
|
|
|
color: $color-error;
|
|
|
|
}
|
|
|
|
|
|
|
|
.has-submitted {
|
|
|
|
input:invalid {
|
|
|
|
border: 1px solid $color-error;
|
|
|
|
}
|
|
|
|
|
|
|
|
input:invalid + .error-message {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea:invalid {
|
|
|
|
border: 1px solid $color-error;
|
|
|
|
}
|
|
|
|
|
|
|
|
textarea:invalid + .error-message {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 11:12:37 +00:00
|
|
|
}
|
|
|
|
</style>
|