MyForm.vue 10.5 KB
Newer Older
hucy's avatar
hucy committed
1
<template>
hucy's avatar
hucy committed
2 3 4 5 6 7 8 9 10 11
  <div>
    <q-form ref="myForm" class="my-form row fit">
      <div
        :class="['item', item.col || 'col-12']"
        v-for="(item, index) in state.config"
        :key="index"
      >
        <template v-if="item.solt">
          <div class="form-item-solt fit">
            <slot :name="item.solt"></slot>
hucy's avatar
hucy committed
12
          </div>
hucy's avatar
hucy committed
13 14 15 16 17 18 19 20 21 22
        </template>
        <template v-else-if="item.type === 'password'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <q-input
hucy's avatar
hucy committed
23 24 25
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
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
              :type="item.isPwd ? 'password' : 'text'"
              v-model="formValue[item.fild]"
              v-bind="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            >
              <template v-slot:append>
                <q-icon
                  :name="item.isPwd ? 'visibility_off' : 'visibility'"
                  class="cursor-pointer"
                  @click="item.isPwd = !item.isPwd"
                />
              </template>
            </q-input>
hucy's avatar
hucy committed
51
          </div>
hucy's avatar
hucy committed
52 53 54 55 56 57 58 59 60 61
        </template>
        <template v-else-if="item.type === 'date'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <date-time-pick
hucy's avatar
hucy committed
62 63 64
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
              format24h
              v-model="formValue[item.fild]"
              :config="item.bind"
              :label="item.label"
              :label-required="item.required"
            />
hucy's avatar
hucy committed
84
          </div>
hucy's avatar
hucy committed
85 86 87 88 89 90 91 92 93 94
        </template>
        <template v-else-if="item.type === 'time'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <time-pick
hucy's avatar
hucy committed
95 96 97
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
              format24h
              v-model="formValue[item.fild]"
              :config="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
hucy's avatar
hucy committed
115
          </div>
hucy's avatar
hucy committed
116 117 118 119 120 121 122 123 124 125
        </template>
        <template v-else-if="item.type === 'dateMultiple'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <date-multiple
hucy's avatar
hucy committed
126 127 128
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
              v-model="formValue[item.fild]"
              :config="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
hucy's avatar
hucy committed
145
          </div>
hucy's avatar
hucy committed
146 147 148 149 150 151 152 153 154 155
        </template>
        <template v-else-if="item.type === 'dateRange'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <date-range
hucy's avatar
hucy committed
156 157 158
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
              v-model="formValue[item.fild]"
              :config="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
hucy's avatar
hucy committed
175
          </div>
hucy's avatar
hucy committed
176 177 178 179 180 181 182 183 184 185
        </template>
        <template v-else-if="item.type === 'color'">
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
            <color-pick
hucy's avatar
hucy committed
186 187 188
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
              v-model="formValue[item.fild]"
              :config="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
hucy's avatar
hucy committed
205
          </div>
hucy's avatar
hucy committed
206 207 208 209 210 211 212 213 214
        </template>
        <template v-else>
          <div class="item-content">
            <div
              class="label-title"
              :class="{ 'text-required': item.required }"
            >
              {{ item.label }}
            </div>
hucy's avatar
hucy committed
215

hucy's avatar
hucy committed
216
            <q-input
hucy's avatar
hucy committed
217 218 219
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
              v-if="item.type !== 'select'"
              :type="item.type"
              v-model="formValue[item.fild]"
              v-bind="item.bind"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
            <q-select
              class="my-select"
hucy's avatar
hucy committed
240 241 242
              :class="{
                'form-label-padding-bottom': isEmpty(item.bind?.rules),
              }"
hucy's avatar
hucy committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
              v-if="item.type === 'select'"
              v-bind="item.bind"
              v-model="formValue[item.fild]"
              :dense="
                item.bind.dense === undefined ? props.dense : item.bind.dense
              "
              :disable="
                item.bind.disable === undefined
                  ? props.disable
                  : item.bind.disable
              "
              :readonly="
                item.bind.readonly === undefined
                  ? props.readonly
                  : item.bind.readonly
              "
            />
          </div>
        </template>
      </div>
    </q-form>
  </div>
hucy's avatar
hucy committed
265 266 267
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from 'vue';
hucy's avatar
hucy committed
268 269 270
import DateTimePick from './DateTimePick.vue';
import TimePick from './TimePick.vue';
import DateMultiple from './DateMultiple.vue';
hucy's avatar
hucy committed
271 272
import DateRange from './DateRange.vue';
import ColorPick from './ColorPick.vue';
hucy's avatar
hucy committed
273
import { cloneDeep, isObjEqual, isEmpty } from 'src/common/utils';
hucy's avatar
hucy committed
274 275 276 277 278 279

interface Props {
  config: any;
  modelValue: any;
  disable?: boolean;
  readonly?: boolean;
hucy's avatar
hucy committed
280
  dense?: boolean;
hucy's avatar
hucy committed
281 282 283 284 285 286 287 288 289 290
}
const props = withDefaults(defineProps<Props>(), {
  config: () => {
    return {};
  },
  modelValue: () => {
    return {};
  },
  disable: false,
  readonly: false,
hucy's avatar
hucy committed
291
  dense: false,
hucy's avatar
hucy committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
});

defineExpose({
  validate,
  reset,
});

const emit = defineEmits<{
  (e: 'update:modelValue', value: any): void;
}>();

const myForm = ref<any>(null);

const state = reactive({
  config: cloneDeep(props.config),
});
const formValue = ref<any>(cloneDeep(props.modelValue));

watch(
  () => props.config,
  (val) => {
    state.config = cloneDeep(val);
  },
  {
    deep: true,
  }
);

watch(
  () => props.modelValue,
  (val: any) => {
    if (!isObjEqual(val, formValue.value)) {
      formValue.value = val;
    }
  },
  {
    deep: true,
  }
);

watch(
  formValue.value,
  (val) => {
    emit('update:modelValue', val);
  },
  { deep: true }
);

onMounted(() => {
  //
});

async function validate() {
  return await myForm.value.validate().then((success: any) => {
    if (success) {
      return true;
    } else {
      return false;
    }
  });
}

function reset() {
  myForm.value.resetValidation();
}
</script>

<style lang="scss" scoped>
.item {
  padding: $padding-sm;
hucy's avatar
hucy committed
362 363 364
  padding-bottom: 0;
  padding-top: 0;
  // border: 1px solid red;
hucy's avatar
hucy committed
365
}
hucy's avatar
hucy committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
.item-content {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
}
.label-title {
  height: 22px;
  font-size: 14px;
  line-height: 22px;
  color: $gray-text;
  margin-bottom: 2px;
  display: inline-block;
}
hucy's avatar
hucy committed
381 382
.form-label-padding-bottom {
  padding-bottom: 20px;
hucy's avatar
hucy committed
383
}
hucy's avatar
hucy committed
384 385 386 387 388 389 390 391 392 393

// :deep(.my-select
//     > :nth-child(1)
//     > :nth-child(1)
//     > :nth-child(1)
//     > :nth-child(1)
//     > span) {
//   white-space: nowrap;
//   overflow: hidden;
// }
hucy's avatar
hucy committed
394
</style>