zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<template>
  <div class="app-table-search">
    <div class="header">
      <div class="collapse" @click="isWrapperCollapse = !isWrapperCollapse">
        <div class="title">{{ $t('components.table.TableSearch.title') }}</div>
        <el-icon class="icon">
          <Plus v-if="!isWrapperCollapse"/>
          <Minus v-if="isWrapperCollapse"/>
        </el-icon>
      </div>
      <col-setting v-bind="colSettingProps"/>
    </div>
    <div
      class="content"
      :style="`height:${isWrapperCollapse ? `${85 + (isCollapse ? (styleHeight-45) : 0)}px` : '0px'}`"
    >
      <div class="form"
      >
        <div class="input" :style="`height:${isCollapse ? `${styleHeight}px` : '45px'}`">
          <app-form-fields
            v-bind="formFieldsProps"
          >
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"/>
            </template>
          </app-form-fields>
        </div>
        <div class="footer">
          <el-button @click="onReset()" icon="RefreshRight">{{ $t('components.table.TableSearch.reset') }}</el-button>
          <el-button type="primary" @click="onSubmit()" icon="Search">
            {{ $t('components.table.TableSearch.search') }}
          </el-button>
          <div class="collapse" v-show="styleHeight > 45">
            <el-button type="primary" link @click="isCollapse = !isCollapse">
              {{ isCollapse ? $t('components.table.TableSearch.retract') : $t('components.table.TableSearch.expand') }}
              <el-icon class="el-icon--right" :style="`transform:rotate(${isCollapse ? '180deg' : 0})`">
                <ArrowDownBold/>
              </el-icon>
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
 
import ColSetting from "./ColSetting";
import * as tools from '@/utils/tools.js';
 
import {context} from '@/hooks';
 
const {useForceUpdate} = context;
 
const formFieldsRef = ref();
const colSettingRef = ref();
const isCollapse = ref(false);
const isWrapperCollapse = ref(true);
 
const props = defineProps({
  formFields: {
    type: Array,
    default: []
  },
  subSubmit: {
    type: Function,
    default: () => {
    }
  },
});
 
const useFields = ref([]);
 
const [$props, forceUpdate] = useForceUpdate(props);
 
const $forceUpdate = async (force) => {
  forceUpdate(force);
  await onInit();
}
 
const onInit = async () => {
 
  if (JSON.stringify(useFields.value) !== JSON.stringify($props.formFields)) {
    useFields.value = $props.formFields.map(e => {
      e.group = '';
      e.isShow = true;
      e.id = tools.uuid();
      e.item.rules = [];
      e.col.span = 8;
      return e;
    });
  }
  formFieldsProps.fields = useFields.value;
  formFieldsRef.value.$forceUpdate(formFieldsProps);
 
  colSettingProps.fields = [...useFields.value];
  colSettingRef.value.$forceUpdate(colSettingProps);
 
  styleHeight.value = getStyleHeight(formFieldsProps.fields);
}
 
const formFieldsProps = {
  ref: formFieldsRef,
  fields: []
}
 
const getStyleHeight = (list) => Math.ceil(list.filter(e => e.isShow).reduce((a, b) => a + b.col.span, 0) / 24) * 45
 
const colSettingProps = {
  ref: colSettingRef,
  fields: [],
  async subFields(list) {
    useFields.value = list;
    formFieldsProps.fields = useFields.value.filter(e => e.isShow);
    formFieldsRef.value.$forceUpdate(formFieldsProps);
    styleHeight.value = getStyleHeight(formFieldsProps.fields);
  }
};
 
const styleHeight = ref(0);
 
const onReset = () => {
  formFieldsRef.value.onReset();
}
 
const onSubmit = async () => {
  const data = await formFieldsRef.value.onValidate();
  props.subSubmit(data);
}
 
onMounted(() => {
  onInit();
})
 
 
defineExpose({
  formFields: formFieldsRef,
  $forceUpdate
});
 
</script>
 
<style lang="scss">
.app-table-search {
 
  .el-form-item {
    margin-bottom: 0;
    margin-top: 5px;
  }
}
</style>
 
<style scoped lang="scss">
.app-table-search {
  padding: 10px;
  background: #fff;
  border-radius: 5px;
  border: 1px solid var(--el-border-color-light);
 
  .el-form-item {
    margin-bottom: 0;
  }
 
  .header {
    display: flex;
    align-items: center;
 
    .collapse {
      margin-right: 10px;
      cursor: pointer;
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex: 1;
      overflow: hidden;
 
      .title {
        font-size: 14px;
        color: var(--el-text-color-primary);
        font-weight: 600;
      }
 
      .icon {
        padding: 0.5px;
        box-sizing: border-box;
        border: 1px solid var(--el-color-info-light-3);
      }
    }
  }
 
  .content {
    overflow: hidden;
    transition: height 0.28s;
 
    .form {
      .footer {
        display: flex;
        justify-content: flex-end;
        margin-top: 10px;
      }
 
      .input {
        overflow: hidden;
        transition: height 0.28s;
      }
 
      .collapse {
        display: flex;
        align-items: center;
        margin-left: 10px;
      }
    }
  }
}
</style>