IndexPage.vue 5.94 KB
Newer Older
hucy's avatar
hucy committed
1
<template>
hucy's avatar
hucy committed
2 3 4 5 6
  <div class="page">
    <div class="actions">
      <q-toggle v-model="state.dense" label="Dense" />
      <q-toggle v-model="state.disable" label="Disable" />
      <q-toggle v-model="state.readonly" label="Readonly" />
hucy's avatar
hucy committed
7 8 9 10 11 12 13
      <q-btn label="提交" color="primary" @click="onSubmit" />
      <q-btn label="重置" color="primary" @click="onResetForm" />
      <q-btn label="改变" color="primary" @click="onChange" />
    </div>
    <div class="page2-form">
      <Com.MyForm
        ref="myForm"
hucy's avatar
hucy committed
14 15 16
        :dense="state.dense"
        :disable="state.disable"
        :readonly="state.readonly"
hucy's avatar
hucy committed
17 18 19
        :config="formState.config"
        v-model="formState.value"
      >
hucy's avatar
hucy committed
20 21 22
        <template #test>
          <div>测试</div>
        </template>
hucy's avatar
hucy committed
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
        <template #class>
          <div class="item-content">
            <div class="label-title text-required">班级</div>
            <div class="label-content">
              <q-option-group
                v-model="formState.value.class"
                :options="classOpt"
                color="primary"
                inline
                :disable="state.disable || state.readonly"
              />
            </div>
          </div>
        </template>
        <template #workingDay>
          <div class="item-content">
            <div class="label-title text-required">工作日</div>
            <div class="label-content">
              <q-option-group
                v-model="formState.value.workingDay"
                color="primary"
                type="checkbox"
                :options="workingDayOpt"
                inline
                :disable="state.disable || state.readonly"
              />
            </div>
          </div>
        </template>
hucy's avatar
hucy committed
52 53 54 55 56 57 58 59 60 61 62
      </Com.MyForm>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: 'PAGE2',
};
</script>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';
hucy's avatar
hucy committed
63 64
import { useMessage } from 'src/common/hooks';
import { delEmptyObjkey, isEmpty } from 'src/common/utils';
hucy's avatar
hucy committed
65 66 67 68
import Com from 'src/components';
import { form } from './config';
import COLORLIST from './colorList';

hucy's avatar
hucy committed
69 70
const { success, warn } = useMessage();

hucy's avatar
hucy committed
71 72 73 74
const myForm = ref<any>(null);

const colorPalette = ref<any>([]);

hucy's avatar
hucy committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
const classOpt = reactive([
  {
    label: 'A班',
    value: 'a',
  },
  {
    label: 'B班',
    value: 'b',
  },
  {
    label: 'C班',
    value: 'c',
  },
]);

const workingDayOpt = reactive([
  {
    label: '周一',
    value: 'mon',
  },
  {
    label: '周二',
    value: 'tue',
  },
  {
    label: '周三',
    value: 'wed',
  },
  {
    label: '周四',
    value: 'thu',
  },
  {
    label: '周五',
    value: 'fri',
  },
]);

hucy's avatar
hucy committed
113
const formState = reactive({
hucy's avatar
hucy committed
114
  config: [] as any[],
hucy's avatar
hucy committed
115
  value: {
hucy's avatar
hucy committed
116
    name: '法外狂徒张三',
hucy's avatar
hucy committed
117
    date1: '2022-12-31 12:30:30',
hucy's avatar
hucy committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    dateMultiple: ['2023/01/23', '2023/01/24'],
    dateRange: {
      from: '2023-01-16',
      to: '2023-01-19',
    },
    dateRangeMultiple: [
      {
        from: '2023/01/16',
        to: '2023/01/19',
      },
      {
        from: '2023/01/23',
        to: '2023/01/31',
      },
    ],
    class: 'a',
    workingDay: [] as any[],
hucy's avatar
hucy committed
135
  } as any,
hucy's avatar
hucy committed
136 137
});

hucy's avatar
hucy committed
138 139 140 141 142 143
const state = reactive({
  dense: false,
  disable: false,
  readonly: false,
});

hucy's avatar
hucy committed
144 145
onMounted(() => {
  getColorList();
hucy's avatar
hucy committed
146
  initFormConfig();
hucy's avatar
hucy committed
147 148
});

hucy's avatar
hucy committed
149 150 151 152 153 154 155 156 157 158
function initFormConfig() {
  formState.config = form.map((item: any) => {
    let obj: any = { ...item };
    if (obj.fild === 'colorPick2') {
      obj.bind.palette = colorPalette.value;
    }
    return obj;
  });
}

hucy's avatar
hucy committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
function getColorList() {
  let arr: any[] = [];
  const length = 7;
  for (let i = 0; i < length; i++) {
    for (const key in COLORLIST) {
      const item = (COLORLIST as any)[key];
      arr.push(item[i + 1]);
    }
  }
  colorPalette.value = arr;
}

function onChange() {
  let obj = {
    name: '王二小',
    age: 1000,
    sex: '男',
  };
  formState.value = obj;

  let newForm = form.map((item: any) => {
    if (item.fild === 'country') {
      item.bind.label = '爱好';
      item.bind.multiple = true;
      item.bind.options = [
        {
          label: '看电视',
          value: 'watch_tv',
        },
        {
          label: '睡懒觉',
          value: 'sleep_in',
        },
        {
          label: '跑步',
          value: 'run',
        },
        {
          label: '听音乐',
          value: 'listen_music',
        },
hucy's avatar
hucy committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
        {
          label: '微信',
          value: 'WeChat',
        },
        {
          label: '微博',
          value: 'Microblog',
        },
        {
          label: '测试1',
          value: '1',
        },
        {
          label: '测试2',
          value: '2',
        },
        {
          label: '测试3',
          value: '3',
        },
        {
          label: '测试4',
          value: '4',
        },
hucy's avatar
hucy committed
224 225 226 227 228 229 230 231
      ];
    }
    return item;
  });
  formState.config = newForm;
}

function onSubmit() {
hucy's avatar
hucy committed
232 233 234 235 236 237
  myForm.value.validate().then((suc: any) => {
    if (!suc) return;
    if (isEmpty(formState.value.workingDay)) {
      return warn('工作日必填');
    }
    success('校验通过');
hucy's avatar
hucy committed
238
    console.log('formValue', delEmptyObjkey(formState.value));
hucy's avatar
hucy committed
239 240 241 242 243 244 245 246 247
  });
}

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

<style lang="scss" scoped>
hucy's avatar
hucy committed
248 249 250 251 252 253 254 255 256 257 258 259
.page {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.actions {
  display: flex;
  column-gap: 10px;
  padding: 8px;
  margin-top: 20px;
  height: 56px;
}
hucy's avatar
hucy committed
260
.page2-form {
hucy's avatar
hucy committed
261
  margin-top: $padding-sm;
hucy's avatar
hucy committed
262
  margin-bottom: 20px;
hucy's avatar
hucy committed
263
  width: 800px;
hucy's avatar
hucy committed
264
  box-sizing: border-box;
hucy's avatar
hucy committed
265 266
  border: 1px solid $border-color;
}
hucy's avatar
hucy committed
267
.item-content {
hucy's avatar
hucy committed
268
  height: calc(100% - 20px);
hucy's avatar
hucy committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  width: 100%;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
}
.label-title {
  height: 22px;
  font-size: 14px;
  line-height: 22px;
  color: rgba(0, 0, 0, 0.871);
  margin-bottom: 2px;
  display: inline-block;
}
.label-content {
  flex: 1;
  display: flex;
  flex-direction: row;
  align-items: center;
  // padding: 0 12px;
  background-color: rgba(0, 0, 0, 0.05);
  border-radius: 4px 4px 0 0;
}
hucy's avatar
hucy committed
291
</style>