clddup
2024-07-19 baf406e7e271ac90faa3aec31ceb44715331d9d0
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
<script lang="tsx">
  import { fileListProps } from '../props';
  import { isFunction, isDef } from '@/utils/is';
  import { useSortable } from '@/hooks/web/useSortable';
  import { useModalContext } from '@/components/Modal/src/hooks/useModalContext';
  import { defineComponent, CSSProperties, watch, nextTick, ref, onMounted } from 'vue';
  import { FileBasicColumn } from '../types/typing';
 
  export default defineComponent({
    name: 'FileList',
    props: fileListProps,
    setup(props, { emit }) {
      const modalFn = useModalContext();
      const sortableContainer = ref<HTMLTableSectionElement>();
 
      watch(
        () => props.dataSource,
        () => {
          nextTick(() => {
            modalFn?.redoModalHeight?.();
          });
        },
      );
 
      if (props.openDrag) {
        onMounted(() =>
          useSortable(sortableContainer, {
            ...props.dragOptions,
            onEnd: ({ oldIndex, newIndex }) => {
              // position unchanged
              if (oldIndex === newIndex) {
                return;
              }
              const { onAfterEnd } = props.dragOptions;
 
              if (isDef(oldIndex) && isDef(newIndex)) {
                const data = [...props.dataSource];
 
                const [oldItem] = data.splice(oldIndex, 1);
                data.splice(newIndex, 0, oldItem);
 
                nextTick(() => {
                  emit('update:dataSource', data);
 
                  isFunction(onAfterEnd) && onAfterEnd(data);
                });
              }
            },
          }).initSortable(),
        );
      }
 
      return () => {
        const { columns, actionColumn, dataSource } = props;
        let columnList: FileBasicColumn[];
        columnList = (
          actionColumn ? [...columns, actionColumn] : [...columns]
        ) as FileBasicColumn[];
 
        return (
          // x scrollbar
          <div class="overflow-x-auto">
            <table class="file-table">
              <colgroup>
                {columnList.map((item) => {
                  const { width = 0, dataIndex } = item;
                  const style: CSSProperties = {
                    width: `${width}px`,
                    minWidth: `${width}px`,
                  };
                  return <col style={width ? style : {}} key={dataIndex} />;
                })}
              </colgroup>
              <thead>
                <tr class="file-table-tr">
                  {columnList.map((item) => {
                    const { title = '', align = 'center', dataIndex } = item;
                    return (
                      <th class={['file-table-th', align]} key={dataIndex}>
                        {title}
                      </th>
                    );
                  })}
                </tr>
              </thead>
              <tbody ref={sortableContainer}>
                {dataSource.map((record = {}, index) => {
                  return (
                    <tr class="file-table-tr" key={`${index + record.name || ''}`}>
                      {columnList.map((item) => {
                        const { dataIndex = '', customRender, align = 'center' } = item;
                        const render = customRender && isFunction(customRender);
                        return (
                          <td class={['file-table-td break-all', align]} key={dataIndex}>
                            {render
                              ? customRender?.({ text: record[dataIndex], record })
                              : record[dataIndex]}
                          </td>
                        );
                      })}
                    </tr>
                  );
                })}
              </tbody>
            </table>
          </div>
        );
      };
    },
  });
</script>
<style lang="less">
  .file-table {
    width: 100%;
    border-collapse: collapse;
 
    .center {
      text-align: center;
    }
 
    .left {
      text-align: left;
    }
 
    .right {
      text-align: right;
    }
 
    &-th,
    &-td {
      padding: 12px 8px;
    }
 
    thead {
      background-color: @background-color-light;
    }
 
    table,
    td,
    th {
      border: 1px solid @border-color-base;
    }
  }
</style>