Item.js 4.67 KB
Newer Older
liang ce committed
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
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _mergeJSXProps from 'babel-helper-vue-jsx-merge-props';
import PropTypes from '../_util/vue-types';
import classNames from 'classnames';
import { getSlotOptions, getComponentFromProp, isEmptyElement } from '../_util/props-util';
import { Col } from '../grid';
import { ListGridType } from './index';

export var ListItemProps = {
  prefixCls: PropTypes.string,
  extra: PropTypes.any,
  actions: PropTypes.arrayOf(PropTypes.any),
  grid: ListGridType
};

export var ListItemMetaProps = {
  avatar: PropTypes.any,
  description: PropTypes.any,
  prefixCls: PropTypes.string,
  title: PropTypes.any
};

export var Meta = {
  functional: true,
  name: 'AListItemMeta',
  __ANT_LIST_ITEM_META: true,
  render: function render(h, context) {
    var props = context.props,
        slots = context.slots,
        listeners = context.listeners;

    var slotsMap = slots();
    var _props$prefixCls = props.prefixCls,
        prefixCls = _props$prefixCls === undefined ? 'ant-list' : _props$prefixCls;

    var avatar = props.avatar || slotsMap.avatar;
    var title = props.title || slotsMap.title;
    var description = props.description || slotsMap.description;
    var content = h(
      'div',
      { 'class': prefixCls + '-item-meta-content' },
      [title && h(
        'h4',
        { 'class': prefixCls + '-item-meta-title' },
        [title]
      ), description && h(
        'div',
        { 'class': prefixCls + '-item-meta-description' },
        [description]
      )]
    );
    return h(
      'div',
      _mergeJSXProps([{ on: listeners }, { 'class': prefixCls + '-item-meta' }]),
      [avatar && h(
        'div',
        { 'class': prefixCls + '-item-meta-avatar' },
        [avatar]
      ), (title || description) && content]
    );
  }
};

function getGrid(grid, t) {
  return grid[t] && Math.floor(24 / grid[t]);
}

export default {
  name: 'AListItem',
  Meta: Meta,
  props: ListItemProps,
  inject: {
    listContext: { 'default': function _default() {
        return {};
      } }
  },

  render: function render() {
    var h = arguments[0];
    var grid = this.listContext.grid;
    var _prefixCls = this.prefixCls,
        prefixCls = _prefixCls === undefined ? 'ant-list' : _prefixCls,
        $slots = this.$slots,
        $listeners = this.$listeners;

    var classString = prefixCls + '-item';
    var extra = getComponentFromProp(this, 'extra');
    var actions = getComponentFromProp(this, 'actions');
    var metaContent = [];
    var otherContent = [];

    ($slots['default'] || []).forEach(function (element) {
      if (!isEmptyElement(element)) {
        if (getSlotOptions(element).__ANT_LIST_ITEM_META) {
          metaContent.push(element);
        } else {
          otherContent.push(element);
        }
      }
    });

    var contentClassString = classNames(prefixCls + '-item-content', _defineProperty({}, prefixCls + '-item-content-single', metaContent.length < 1));
    var content = otherContent.length > 0 ? h(
      'div',
      { 'class': contentClassString },
      [otherContent]
    ) : null;

    var actionsContent = void 0;
    if (actions && actions.length > 0) {
      var actionsContentItem = function actionsContentItem(action, i) {
        return h(
          'li',
          { key: prefixCls + '-item-action-' + i },
          [action, i !== actions.length - 1 && h('em', { 'class': prefixCls + '-item-action-split' })]
        );
      };
      actionsContent = h(
        'ul',
        { 'class': prefixCls + '-item-action' },
        [actions.map(function (action, i) {
          return actionsContentItem(action, i);
        })]
      );
    }

    var extraContent = h(
      'div',
      { 'class': prefixCls + '-item-extra-wrap' },
      [h(
        'div',
        { 'class': prefixCls + '-item-main' },
        [metaContent, content, actionsContent]
      ), h(
        'div',
        { 'class': prefixCls + '-item-extra' },
        [extra]
      )]
    );

    var mainContent = grid ? h(
      Col,
      {
        attrs: {
          span: getGrid(grid, 'column'),
          xs: getGrid(grid, 'xs'),
          sm: getGrid(grid, 'sm'),
          md: getGrid(grid, 'md'),
          lg: getGrid(grid, 'lg'),
          xl: getGrid(grid, 'xl'),
          xxl: getGrid(grid, 'xxl')
        }
      },
      [h(
        'div',
        _mergeJSXProps([{ on: $listeners }, { 'class': classString }]),
        [extra && extraContent, !extra && metaContent, !extra && content, !extra && actionsContent]
      )]
    ) : h(
      'div',
      _mergeJSXProps([{ on: $listeners }, { 'class': classString }]),
      [extra && extraContent, !extra && metaContent, !extra && content, !extra && actionsContent]
    );

    return mainContent;
  }
};