list-control.tsx 5.2 KB
Newer Older
1
import { ListModel } from '@ibiz-template/model';
2
import { ControlVO } from '@ibiz-template/service';
3
import { useListController, useNamespace } from '@ibiz-template/vue-util';
4 5 6 7 8 9 10 11 12 13
import {
  defineComponent,
  h,
  PropType,
  onMounted,
  watch,
  getCurrentInstance,
} from 'vue';
import './list-control.scss';
import { useListEvent } from './list-control.util';
14 15 16 17 18 19 20 21

export const ListControl = defineComponent({
  name: 'ListControl',
  props: {
    modelData: {
      type: ListModel,
      required: true,
    },
22
    context: { type: Object as PropType<IContext>, required: true },
23
    params: { type: Object as PropType<IParams>, default: () => ({}) },
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
    /**
     * 行数据默认激活模式
     * - 0 不激活
     * - 1 单击激活
     * - 2 双击激活(默认值)
     *
     * @type {(number | 0 | 1 | 2)}
     */
    mdCtrlActiveMode: { type: Number, default: 2 },
    /**
     * 是否默认选中第一条数据
     *
     * @type boolean
     */
    isSelectFirstDefault: { type: Boolean, required: false },
    /**
     * 默认选中的数据
     *
     * @type Array
     */
    defaultSelectKeys: { type: Array<string>, required: false },
    /**
     * 是否包含在导航视图内
     *
     * @type boolean
     */
    isExpView: { type: Boolean, required: false },
51 52 53
  },
  setup(props) {
    const { proxy } = getCurrentInstance()!;
54
    const ns = useNamespace('control-list');
55 56
    const c = useListController(
      proxy,
57 58
      props.modelData!,
      props.context!,
59 60
      props.params,
    );
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

    const { handleClick, handleDblClick } = useListEvent(c);

    onMounted(() => {
      // 外部传入默认选中数组,第一次加载数据后选中其中第一项,未传入默认数据,选中全部数据第一项
      watch(
        () => c,
        (newVal, oldVal) => {
          if (props.isExpView && newVal && newVal !== oldVal) {
            let defaultSelectItem = c.items[0];
            if (props.defaultSelectKeys && props.defaultSelectKeys.length > 0) {
              const defaultItem = c.items.find((item: ControlVO) => {
                return item.srfkey === props.defaultSelectKeys![0];
              });
              if (defaultItem) {
                defaultSelectItem = defaultItem;
              }
            }
            // 默认选中数据
            if (defaultSelectItem) {
              c.onSelectionChange([defaultSelectItem]);
            }
          }
        },
        { immediate: true },
      );

      watch(
        () => c.items.length,
        (newVal, oldVal) => {
          // 导航删除后高亮第一条
          if (props.isExpView && newVal && newVal + 1 === oldVal) {
            c.onSelectionChange([c.items[0]]);
          }
        },
        { immediate: true, deep: true },
      );
    });

    // 绘制默认列表项
    const renderDefaultItem = (item: ControlVO) => {
      const findIndex = c.selectedData.findIndex(data => {
        return data.srfkey === item.srfkey;
      });
      const itemClass = [ns.b('item'), ns.is('active', findIndex !== -1)];
      return (
        <div
          class={itemClass}
          key={item.srfkey}
          onClick={() => handleClick(item)}
          onDblclick={() => handleDblClick(item)}
        >
          {c.layoutPanelProvider
            ? h(c.layoutPanelProvider.component, {
                props: {
                  modelData: c.model,
                  context: c.context,
                  params: c.params,
                  inputData: item,
                },
              })
            : `${item.srfmajortext}:${item.srfkey}`}
        </div>
      );
    };

    // // 绘制分组
    // const renderGroup = () => {
    //   return c.groupData.map((group: IData) => {
    //     return (
    //       <el-collapse-item class={ns.e('group-item')}>
    //         {{
    //           title: () => {
    //             return <b>{group.group}</b>;
    //           },
    //           default: () => {
    //             if (group.children.length > 0) {
    //               return (
    //                 <div>
    //                   {group.children.map((groupChild: ControlVO) => {
    //                     return renderDefaultItem(groupChild);
    //                   })}
    //                 </div>
    //               );
    //             }
    //             return <div class={ns.e('group-item-empty')}>无数据</div>;
    //           },
    //         }}
    //       </el-collapse-item>
    //     );
    //   });
    // };

    // 绘制卡片内容
    const renderListContent = () => {
      if (!c.model.source.enableGroup) {
        return c.items.map(item => {
          return renderDefaultItem(item);
        });
      }
      return <el-collapse></el-collapse>;
    };

    // 绘制加载更多
    const renderLoadMore = () => {
      return Object.is(c.total, c.items.length) ? null : (
        <div
          onClick={(e: MouseEvent) => c.loadMore(e)}
          class={ns.e('load-more')}
        >
          加载更多
        </div>
      );
    };

    return { c, ns, renderListContent, renderLoadMore };
177 178 179 180
  },
  render() {
    return (
      <control-layout modelData={this.c.model}>
181
        {this.c.complete && [this.renderListContent(), this.renderLoadMore()]}
182 183 184 185
      </control-layout>
    );
  },
});