Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <template>
- <div class="form-builder">
- <!-- Create Steps header from form groups-->
- <el-steps :active="activeStep" align-center>
- <template v-for="(formgroup, index) in formData">
- <el-step :title="index | camelToWords" :key="`form-group-${index}`" />
- </template>
- </el-steps>
- <el-form
- :ref="formName"
- v-loading="isLoading"
- label-width="120px"
- :model="formData"
- label-position="top">
- <template v-for="(formgroup, index, i) in formData">
- <div :key="`formgroup-${index}`" v-if="activeStep === i">
- <h3>{{ index | camelToWords }}</h3>
- <el-row :gutter="10">
- <template v-for="(formItem, index2) in formgroup">
- <el-col
- v-if="(formItem.dependency && checkDependency(formItem.dependency, index, index2)) || !formItem.dependency"
- :key="`formItem-${index2}`"
- :span="24"
- :gutter="0"
- :data-testid="`form-${formName}-col-${index2}`">
- <transition name="fade">
- <el-form-item
- :label="formItem.label"
- :rules="formItem.validation"
- :prop="`${[index]}.${[index2]}.value`">
- <template v-if="formItem.type === 'placeholder'">
- <div class="placeholder" />
- </template>
- <!-- Text Input Field -->
- <template v-else-if="formItem.type === 'textarea' || formItem.type === 'text' || formItem.type === 'email' || formItem.type === 'tel' || formItem.type === 'number'">
- <el-input
- v-model="formItem.value"
- :type="formItem.type"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :clearable="formItem.clearable"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <!-- Password Input Field -->
- <template v-else-if="formItem.type === 'password'">
- <el-input
- v-model="formItem.value"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :clearable="formItem.clearable"
- :data-testid="`form-${formName}-input-${index2}`"
- show-password />
- </template>
- <template v-else-if="formItem.type === 'radio'">
- <el-radio-group v-model="formItem.value">
- <template v-for="(option, index) in formItem.options">
- <el-radio :label="option.value" :key="`radio-option-${index}`">{{ option.label }}</el-radio>
- </template>
- </el-radio-group>
- </template>
- <template v-else-if="formItem.type === 'slider'">
- <el-slider
- v-model="formItem.value"
- :range="formItem.range"
- :marks="formItem.marks"
- :min="formItem.min"
- :max="formItem.max"
- :show-stops="formItem.showStops"
- :step="formItem.step"
- :format-tooltip="formItem.formatTooltip"
- />
- </template>
- <!-- Single Date Input Field -->
- <template v-else-if="formItem.type === 'time'">
- <el-time-picker
- v-model="formItem.value"
- :picker-options="formItem.options"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <!-- Single Date Input Field -->
- <template v-else-if="formItem.type === 'date'">
- <el-date-picker
- v-model="formItem.value"
- type="date"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :picker-options="formItem.options"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <!-- Single Date Time Input Field -->
- <template v-else-if="formItem.type === 'dateTime'">
- <el-date-picker
- v-model="formItem.value"
- type="datetime"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :picker-options="formItem.options"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <!-- Single DateRange Input Field -->
- <template v-else-if="formItem.type === 'dateRange'">
- <el-date-picker
- v-model="formItem.value"
- type="daterange"
- range-separator="To"
- start-placeholder="Start date"
- end-placeholder="End date"
- :picker-options="formItem.options"
- :disabled="isLoading"
- :placeholder="formItem.placeholder"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <!-- Color Picker Input Field -->
- <template v-else-if="formItem.type === 'color'">
- <el-color-picker
- v-model="formItem.value"
- :color-format="formItem.colorFormat"
- :predefine="formItem.predefinedColors"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <template v-else-if="formItem.type === 'switch'">
- <el-switch
- v-model="formItem.value"
- active-color="#35c58b"
- inactive-color="#ddd"
- :data-testid="`form-${formName}-input-${index2}`" />
- </template>
- <template v-else-if="formItem.type === 'select'">
- <el-select
- v-model="formItem.value"
- filterable
- class="dpl-block"
- :placeholder="formItem.placeholder"
- :data-testid="`form-${formName}-input-${index2}`">
- <el-option
- v-for="item in formItem.options"
- :key="item.value"
- :label="item.label"
- :value="item.value">
- <template v-if="!formItem.hideLabel">
- <span style="float: left">{{ item.label }}</span>
- <span style="float: right; color: #8492a6; font-size: 0.8em">{{ item.value }}</span>
- </template>
- </el-option>
- </el-select>
- </template>
- <template v-else-if="formItem.type === 'image-upload'">
- <el-upload
- class="image-uploader"
- :action="formItem.uploadAction"
- :show-file-list="false"
- :on-success="formItem.uploadSuccess"
- :before-upload="formItem.beforeUpload"
- :on-preview="formItem.handlePreview">
- <img
- v-if="formItem.imageUrl"
- :src="formItem.imageUrl"
- class="image">
- <i
- v-else
- class="el-icon-plus image-uploader-icon" /> Upload Image
- </el-upload>
- </template>
- <div
- v-if="formItem.help"
- class="system-message-container info">
- {{ formItem.help }}
- </div>
- </el-form-item>
- </transition>
- </el-col>
- </template>
- </el-row>
- </div>
- </template>
- <transition name="fade">
- <el-alert
- v-if="formError"
- title="Please fill out all required fields."
- type="error"
- effect="dark" />
- </transition>
- <template v-if="finalStep !== (activeStep + 1)">
- <el-button style="margin-top: 12px;" :key="`step-${index}`" @click="activeStep = activeStep + 1">Next step ›</el-button>
- </template>
- <template v-if="activeStep !== 0">
- <el-button style="margin-top: 12px;" :key="`step-${index}`" @click="activeStep = activeStep - 1">‹ Previous step</el-button>
- </template>
- <template v-if="finalStep === (activeStep + 1)">
- <footer>
- <transition name="fade">
- <FormFooterOptions v-if="formUpdated">
- <button
- class="button btn-cancel"
- @click="cancelForm(formName)">
- Cancel Changes
- </button>
- <button
- class="button cta"
- @click="submitForm(formName)">
- Submit Changes
- </button>
- </FormFooterOptions>
- </transition>
- </footer>
- </template>
- </el-form>
- <!-- <el-button style="margin-top: 12px;" :key="`step-${index}`">Next step</el-button>-->
- </div>
- </template>
- <script>
- import FormFooterOptions from "./FormFooterOptions"
- export default {
- name: "CoreFormBuilderElementUIHandler",
- components: {
- FormFooterOptions
- },
- filters: {
- camelToWords(string) {
- const result = string.replace(/([A-Z])/g, " $1")
- return result.charAt(0).toUpperCase() + result.slice(1)
- }
- },
- props: {
- value: {
- type: Object,
- required: true
- },
- isLoading: {
- type: Boolean,
- default: false
- },
- formName: {
- type: String,
- default: "defaultForm"
- }
- },
- data() {
- return {
- formUpdated: false,
- formError: false,
- activeStep: 0
- }
- },
- computed: {
- finalStep() {
- return Object.keys(this.value).length
- },
- formData: {
- get() {
- return this.value
- },
- set(val) {
- console.log(val)
- }
- }
- },
- watch: {
- formData: {
- handler(val) {
- if (val && !this.initialising) {
- this.formUpdated = true
- }
- },
- deep: true
- }
- },
- methods: {
- checkDependency(value, key) {
- if (typeof value === "object") {
- if (value.value === '*') {
- return this.formData[key][value.field].value != null
- } else {
- return this.formData[key][value.field].value === value.value
- }
- } else {
- return this.formData[key][value].value
- }
- },
- submitForm(formName) {
- this.$refs[formName].validate(valid => {
- if (valid) {
- this.formError = false
- this.$emit("submitForm", this.formData)
- } else {
- this.formError = true
- return false
- }
- })
- },
- cancelForm(formName) {
- this.$refs[formName].resetFields()
- this.$emit("cancelForm")
- }
- },
- created() {
- }
- }
- </script>
- <style lang="scss" scoped>
- /deep/ .el-form--label-top {
- .el-form-item__label {
- margin: 0;
- padding: 0;
- }
- }
- /deep/ .options-panel {
- margin: 4em -2em 0 -2em;
- }
- /deep/ .el-input__icon,
- /deep/ .el-date-editor .el-range__icon {
- color: #787879;
- }
- /deep/ .el-form-item__label {
- font-weight: bold;
- }
- footer {
- margin-top: 3em;
- }
- .dpl-block {
- display: block;
- }
- .placeholder {
- width: 100%;
- height: 6em;
- }
- .image-uploader {
- border-radius: 6px;
- cursor: pointer;
- position: relative;
- overflow: hidden;
- border: 1px dashed var(--main-border-color);
- transition: var(--main-transition);
- }
- .image-uploader:hover {
- border-color: var(--main-darker-grey);
- background: var(--main-light-blue);
- }
- .image-uploader-icon {
- font-size: 1em;
- color: #8c939d;
- width: 1em;
- height: 1em;
- margin: 0 1em;
- line-height: 1em;
- text-align: center;
- }
- .image {
- width: 1em;
- height: 1em;
- display: block;
- }
- </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement