ButtonCellRender.vue 2.52 KB
Newer Older
hucy's avatar
hucy committed
1
<script setup lang="ts">
hucy's avatar
hucy committed
2 3 4 5 6 7
import { reactive, watchEffect, computed } from 'vue';
import { isEmpty, every, omit } from 'src/common/utils';
import type {
  CellRenderButtonsType as ButtonsType,
  MoreButtonsType,
} from 'src/common/types';
hucy's avatar
hucy committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

const props = defineProps<{
  params: any;
}>();

/**
 * buttons: [
 *   {
 *     tooltip: '删除',
 *     round: true,
 *     icon: 'add',
 *     unelevated: true,
 *     callback: (data: any) => {
 *       onDel(data);
 *     },
 *   },
 * ];
 */

const state = reactive({
hucy's avatar
hucy committed
28 29
  buttons: [] as ButtonsType[],
  moreButtons: {} as MoreButtonsType,
hucy's avatar
hucy committed
30 31 32
});

watchEffect(() => {
hucy's avatar
hucy committed
33 34
  state.buttons = props.params.buttons || [];
  state.moreButtons = props.params.moreButtons || {};
hucy's avatar
hucy committed
35 36
});

hucy's avatar
hucy committed
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
const showMoreButtons = computed(() => {
  const hide = state.moreButtons.hide;
  const children = state.moreButtons.children;

  if (hide) {
    return false;
  } else {
    if (isEmpty(children)) {
      return false;
    } else {
      if (every(children, 'hide')) {
        return false;
      } else {
        return true;
      }
    }
  }
});

const showButtons = computed(() => {
  const buttons = state.buttons;
  if (isEmpty(buttons)) {
    return false;
  } else {
    if (every(buttons, 'hide')) {
      return false;
    } else {
      return true;
    }
  }
});

const moreButton = computed(() => {
  return omit(state.moreButtons, 'children');
hucy's avatar
hucy committed
71 72 73 74
});
</script>
<template>
  <div>
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
    <div class="row">
      <div class="q-gutter-xs" v-if="showButtons">
        <q-btn
          v-for="(item, index) in state.buttons"
          :key="index"
          v-bind="item"
          @click="item.callback(props.params)"
        >
          <q-tooltip v-if="item.tooltip">
            {{ item.tooltip }}
          </q-tooltip>
        </q-btn>
      </div>
      <div class="q-gutter-xs" v-if="showMoreButtons">
        <q-btn v-bind="moreButton">
          <q-menu>
            <q-list style="min-width: 100px">
              <q-item
                clickable
                v-close-popup
                v-for="(item, index) in state.moreButtons.children"
                :key="index"
                @click="item.callback(props.params)"
              >
                <q-item-section avatar v-if="!isEmpty(item.icon)">
                  <q-icon :color="item.color || 'grey-9'" :name="item.icon" />
                </q-item-section>
                <q-item-section>{{ item.label }}</q-item-section>
              </q-item>
            </q-list>
          </q-menu>
        </q-btn>
      </div>
hucy's avatar
hucy committed
108 109 110 111 112
    </div>
  </div>
</template>

<style lang="scss" scoped></style>