index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  4. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  5. import _typeof from "@babel/runtime/helpers/esm/typeof";
  6. var _this = this;
  7. import isPlainObject from 'lodash-es/isPlainObject';
  8. import classNames from '../classNames';
  9. import { isVNode, Fragment, Comment, Text, h } from 'vue';
  10. import { camelize, hyphenate, isOn, resolvePropValue } from '../util';
  11. import isValid from '../isValid';
  12. import initDefaultProps from './initDefaultProps';
  13. // function getType(fn) {
  14. // const match = fn && fn.toString().match(/^\s*function (\w+)/);
  15. // return match ? match[1] : '';
  16. // }
  17. var splitAttrs = function splitAttrs(attrs) {
  18. var allAttrs = Object.keys(attrs);
  19. var eventAttrs = {};
  20. var onEvents = {};
  21. var extraAttrs = {};
  22. for (var i = 0, l = allAttrs.length; i < l; i++) {
  23. var key = allAttrs[i];
  24. if (isOn(key)) {
  25. eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
  26. onEvents[key] = attrs[key];
  27. } else {
  28. extraAttrs[key] = attrs[key];
  29. }
  30. }
  31. return {
  32. onEvents: onEvents,
  33. events: eventAttrs,
  34. extraAttrs: extraAttrs
  35. };
  36. };
  37. var parseStyleText = function parseStyleText() {
  38. var cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  39. var camel = arguments.length > 1 ? arguments[1] : undefined;
  40. var res = {};
  41. var listDelimiter = /;(?![^(]*\))/g;
  42. var propertyDelimiter = /:(.+)/;
  43. if (_typeof(cssText) === 'object') return cssText;
  44. cssText.split(listDelimiter).forEach(function (item) {
  45. if (item) {
  46. var tmp = item.split(propertyDelimiter);
  47. if (tmp.length > 1) {
  48. var k = camel ? camelize(tmp[0].trim()) : tmp[0].trim();
  49. res[k] = tmp[1].trim();
  50. }
  51. }
  52. });
  53. return res;
  54. };
  55. var hasProp = function hasProp(instance, prop) {
  56. return instance[prop] !== undefined;
  57. };
  58. // 重构后直接使用 hasProp 替换
  59. var slotHasProp = function slotHasProp(slot, prop) {
  60. return hasProp(slot, prop);
  61. };
  62. var getScopedSlots = function getScopedSlots(ele) {
  63. return ele.data && ele.data.scopedSlots || {};
  64. };
  65. var getSlots = function getSlots(ele) {
  66. var componentOptions = ele.componentOptions || {};
  67. if (ele.$vnode) {
  68. componentOptions = ele.$vnode.componentOptions || {};
  69. }
  70. var children = ele.children || componentOptions.children || [];
  71. var slots = {};
  72. children.forEach(function (child) {
  73. if (!isEmptyElement(child)) {
  74. var name = child.data && child.data.slot || 'default';
  75. slots[name] = slots[name] || [];
  76. slots[name].push(child);
  77. }
  78. });
  79. return _objectSpread(_objectSpread({}, slots), getScopedSlots(ele));
  80. };
  81. var flattenChildren = function flattenChildren() {
  82. var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  83. var filterEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  84. var temp = Array.isArray(children) ? children : [children];
  85. var res = [];
  86. temp.forEach(function (child) {
  87. if (Array.isArray(child)) {
  88. res.push.apply(res, _toConsumableArray(flattenChildren(child, filterEmpty)));
  89. } else if (child && child.type === Fragment) {
  90. res.push.apply(res, _toConsumableArray(flattenChildren(child.children, filterEmpty)));
  91. } else if (child && isVNode(child)) {
  92. if (filterEmpty && !isEmptyElement(child)) {
  93. res.push(child);
  94. } else if (!filterEmpty) {
  95. res.push(child);
  96. }
  97. } else if (isValid(child)) {
  98. res.push(child);
  99. }
  100. });
  101. return res;
  102. };
  103. var getSlot = function getSlot(self) {
  104. var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  105. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  106. if (isVNode(self)) {
  107. if (self.type === Fragment) {
  108. return name === 'default' ? flattenChildren(self.children) : [];
  109. } else if (self.children && self.children[name]) {
  110. return flattenChildren(self.children[name](options));
  111. } else {
  112. return [];
  113. }
  114. } else {
  115. var res = self.$slots[name] && self.$slots[name](options);
  116. return flattenChildren(res);
  117. }
  118. };
  119. var getAllChildren = function getAllChildren(ele) {
  120. var componentOptions = ele.componentOptions || {};
  121. if (ele.$vnode) {
  122. componentOptions = ele.$vnode.componentOptions || {};
  123. }
  124. return ele.children || componentOptions.children || [];
  125. };
  126. var getSlotOptions = function getSlotOptions() {
  127. throw Error('使用 .type 直接取值');
  128. };
  129. var findDOMNode = function findDOMNode(instance) {
  130. var _instance$vnode;
  131. var node = (instance === null || instance === void 0 ? void 0 : (_instance$vnode = instance.vnode) === null || _instance$vnode === void 0 ? void 0 : _instance$vnode.el) || instance && (instance.$el || instance);
  132. while (node && !node.tagName) {
  133. node = node.nextSibling;
  134. }
  135. return node;
  136. };
  137. var getOptionProps = function getOptionProps(instance) {
  138. var res = {};
  139. if (instance.$ && instance.$.vnode) {
  140. var props = instance.$.vnode.props || {};
  141. Object.keys(instance.$props).forEach(function (k) {
  142. var v = instance.$props[k];
  143. var hyphenateKey = hyphenate(k);
  144. if (v !== undefined || hyphenateKey in props) {
  145. res[k] = v; // 直接取 $props[k]
  146. }
  147. });
  148. } else if (isVNode(instance) && _typeof(instance.type) === 'object') {
  149. var originProps = instance.props || {};
  150. var _props = {};
  151. Object.keys(originProps).forEach(function (key) {
  152. _props[camelize(key)] = originProps[key];
  153. });
  154. var options = instance.type.props || {};
  155. Object.keys(options).forEach(function (k) {
  156. var v = resolvePropValue(options, _props, k, _props[k]);
  157. if (v !== undefined || k in _props) {
  158. res[k] = v;
  159. }
  160. });
  161. }
  162. return res;
  163. };
  164. var getComponent = function getComponent(instance) {
  165. var prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  166. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  167. var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  168. var com = undefined;
  169. if (instance.$) {
  170. var temp = instance[prop];
  171. if (temp !== undefined) {
  172. return typeof temp === 'function' && execute ? temp(options) : temp;
  173. } else {
  174. com = instance.$slots[prop];
  175. com = execute && com ? com(options) : com;
  176. }
  177. } else if (isVNode(instance)) {
  178. var _temp = instance.props && instance.props[prop];
  179. if (_temp !== undefined && instance.props !== null) {
  180. return typeof _temp === 'function' && execute ? _temp(options) : _temp;
  181. } else if (instance.type === Fragment) {
  182. com = instance.children;
  183. } else if (instance.children && instance.children[prop]) {
  184. com = instance.children[prop];
  185. com = execute && com ? com(options) : com;
  186. }
  187. }
  188. if (Array.isArray(com)) {
  189. com = flattenChildren(com);
  190. com = com.length === 1 ? com[0] : com;
  191. com = com.length === 0 ? undefined : com;
  192. }
  193. return com;
  194. };
  195. var getComponentFromProp = function getComponentFromProp(instance, prop) {
  196. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  197. var execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  198. if (instance.$createElement) {
  199. // const h = instance.$createElement;
  200. var temp = instance[prop];
  201. if (temp !== undefined) {
  202. return typeof temp === 'function' && execute ? temp(h, options) : temp;
  203. }
  204. return instance.$scopedSlots[prop] && execute && instance.$scopedSlots[prop](options) || instance.$scopedSlots[prop] || instance.$slots[prop] || undefined;
  205. } else {
  206. // const h = instance.context.$createElement;
  207. var _temp2 = getPropsData(instance)[prop];
  208. if (_temp2 !== undefined) {
  209. return typeof _temp2 === 'function' && execute ? _temp2(h, options) : _temp2;
  210. }
  211. var slotScope = getScopedSlots(instance)[prop];
  212. if (slotScope !== undefined) {
  213. return typeof slotScope === 'function' && execute ? slotScope(h, options) : slotScope;
  214. }
  215. var slotsProp = [];
  216. var componentOptions = instance.componentOptions || {};
  217. (componentOptions.children || []).forEach(function (child) {
  218. if (child.data && child.data.slot === prop) {
  219. if (child.data.attrs) {
  220. delete child.data.attrs.slot;
  221. }
  222. if (child.tag === 'template') {
  223. slotsProp.push(child.children);
  224. } else {
  225. slotsProp.push(child);
  226. }
  227. }
  228. });
  229. return slotsProp.length ? slotsProp : undefined;
  230. }
  231. };
  232. var getAllProps = function getAllProps(ele) {
  233. var props = getOptionProps(ele);
  234. if (ele.$) {
  235. props = _objectSpread(_objectSpread({}, props), _this.$attrs);
  236. } else {
  237. props = _objectSpread(_objectSpread({}, ele.props), props);
  238. }
  239. return props;
  240. };
  241. var getPropsData = function getPropsData(ins) {
  242. var vnode = ins.$ ? ins.$ : ins;
  243. var res = {};
  244. var originProps = vnode.props || {};
  245. var props = {};
  246. Object.keys(originProps).forEach(function (key) {
  247. props[camelize(key)] = originProps[key];
  248. });
  249. var options = isPlainObject(vnode.type) ? vnode.type.props : {};
  250. options && Object.keys(options).forEach(function (k) {
  251. var v = resolvePropValue(options, props, k, props[k]);
  252. if (k in props) {
  253. // 仅包含 props,不包含默认值
  254. res[k] = v;
  255. }
  256. });
  257. return _objectSpread(_objectSpread({}, props), res); // 合并事件、未声明属性等
  258. };
  259. var getValueByProp = function getValueByProp(ele, prop) {
  260. return getPropsData(ele)[prop];
  261. };
  262. var getAttrs = function getAttrs(ele) {
  263. var data = ele.data;
  264. if (ele.$vnode) {
  265. data = ele.$vnode.data;
  266. }
  267. return data ? data.attrs || {} : {};
  268. };
  269. var getKey = function getKey(ele) {
  270. var key = ele.key;
  271. return key;
  272. };
  273. export function getEvents() {
  274. var ele = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  275. var on = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  276. var props = {};
  277. if (ele.$) {
  278. props = _objectSpread(_objectSpread({}, props), ele.$attrs);
  279. } else {
  280. props = _objectSpread(_objectSpread({}, props), ele.props);
  281. }
  282. return splitAttrs(props)[on ? 'onEvents' : 'events'];
  283. }
  284. export function getEvent(child, event) {
  285. return child.props && child.props[event];
  286. }
  287. // 获取 xxx.native 或者 原生标签 事件
  288. export function getDataEvents(child) {
  289. var events = {};
  290. if (child.data && child.data.on) {
  291. events = child.data.on;
  292. }
  293. return _objectSpread({}, events);
  294. }
  295. // use getListeners instead this.$listeners
  296. // https://github.com/vueComponent/ant-design-vue/issues/1705
  297. export function getListeners(context) {
  298. return (context.$vnode ? context.$vnode.componentOptions.listeners : context.$listeners) || {};
  299. }
  300. export function getClass(ele) {
  301. var props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  302. var tempCls = props.class || {};
  303. var cls = {};
  304. if (typeof tempCls === 'string') {
  305. tempCls.split(' ').forEach(function (c) {
  306. cls[c.trim()] = true;
  307. });
  308. } else if (Array.isArray(tempCls)) {
  309. classNames(tempCls).split(' ').forEach(function (c) {
  310. cls[c.trim()] = true;
  311. });
  312. } else {
  313. cls = _objectSpread(_objectSpread({}, cls), tempCls);
  314. }
  315. return cls;
  316. }
  317. export function getStyle(ele, camel) {
  318. var props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  319. var style = props.style || {};
  320. if (typeof style === 'string') {
  321. style = parseStyleText(style, camel);
  322. } else if (camel && style) {
  323. // 驼峰化
  324. var res = {};
  325. Object.keys(style).forEach(function (k) {
  326. return res[camelize(k)] = style[k];
  327. });
  328. return res;
  329. }
  330. return style;
  331. }
  332. export function getComponentName(opts) {
  333. return opts && (opts.Ctor.options.name || opts.tag);
  334. }
  335. export function isFragment(c) {
  336. return c.length === 1 && c[0].type === Fragment;
  337. }
  338. export function isEmptyContent(c) {
  339. return c === undefined || c === null || c === '' || Array.isArray(c) && c.length === 0;
  340. }
  341. export function isEmptyElement(c) {
  342. return c && (c.type === Comment || c.type === Fragment && c.children.length === 0 || c.type === Text && c.children.trim() === '');
  343. }
  344. export function isEmptySlot(c) {
  345. return !c || c().every(isEmptyElement);
  346. }
  347. export function isStringElement(c) {
  348. return c && c.type === Text;
  349. }
  350. export function filterEmpty() {
  351. var children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  352. var res = [];
  353. children.forEach(function (child) {
  354. if (Array.isArray(child)) {
  355. res.push.apply(res, _toConsumableArray(child));
  356. } else if ((child === null || child === void 0 ? void 0 : child.type) === Fragment) {
  357. res.push.apply(res, _toConsumableArray(filterEmpty(child.children)));
  358. } else {
  359. res.push(child);
  360. }
  361. });
  362. return res.filter(function (c) {
  363. return !isEmptyElement(c);
  364. });
  365. }
  366. export function filterEmptyWithUndefined(children) {
  367. if (children) {
  368. var coms = filterEmpty(children);
  369. return coms.length ? coms : undefined;
  370. } else {
  371. return children;
  372. }
  373. }
  374. export function mergeProps() {
  375. var args = [].slice.call(arguments, 0);
  376. var props = {};
  377. args.forEach(function () {
  378. var p = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  379. for (var _i = 0, _Object$entries = Object.entries(p); _i < _Object$entries.length; _i++) {
  380. var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
  381. k = _Object$entries$_i[0],
  382. v = _Object$entries$_i[1];
  383. props[k] = props[k] || {};
  384. if (isPlainObject(v)) {
  385. _extends(props[k], v);
  386. } else {
  387. props[k] = v;
  388. }
  389. }
  390. });
  391. return props;
  392. }
  393. function isValidElement(element) {
  394. if (Array.isArray(element) && element.length === 1) {
  395. element = element[0];
  396. }
  397. return element && element.__v_isVNode && _typeof(element.type) !== 'symbol'; // remove text node
  398. }
  399. function getPropsSlot(slots, props) {
  400. var _props$prop, _slots$prop;
  401. var prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
  402. return (_props$prop = props[prop]) !== null && _props$prop !== void 0 ? _props$prop : (_slots$prop = slots[prop]) === null || _slots$prop === void 0 ? void 0 : _slots$prop.call(slots);
  403. }
  404. export var getTextFromElement = function getTextFromElement(ele) {
  405. if (isValidElement(ele) && isStringElement(ele[0])) {
  406. return ele[0].children;
  407. }
  408. return ele;
  409. };
  410. export { splitAttrs, hasProp, getOptionProps, getComponent, getComponentFromProp, getSlotOptions, slotHasProp, getPropsData, getKey, getAttrs, getValueByProp, parseStyleText, initDefaultProps, isValidElement, camelize, getSlots, getSlot, getAllProps, getAllChildren, findDOMNode, flattenChildren, getPropsSlot };
  411. export default hasProp;