util.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import _typeof from 'babel-runtime/helpers/typeof';
  2. import { getPropsData, getSlotOptions, getKey, getAttrs, getComponentFromProp } from '../_util/props-util';
  3. import { cloneVNodes } from '../_util/vnode';
  4. export function toTitle(title) {
  5. if (typeof title === 'string') {
  6. return title.trim();
  7. }
  8. return '';
  9. }
  10. export function getValuePropValue(child) {
  11. if (!child) {
  12. return null;
  13. }
  14. var props = getPropsData(child);
  15. if ('value' in props) {
  16. return props.value;
  17. }
  18. if (getKey(child) !== undefined) {
  19. return getKey(child);
  20. }
  21. if (getSlotOptions(child).isSelectOptGroup) {
  22. var label = getComponentFromProp(child, 'label');
  23. if (label) {
  24. return label;
  25. }
  26. }
  27. throw new Error('Need at least a key or a value or a label (only for OptGroup) for ' + child);
  28. }
  29. export function getPropValue(child, prop) {
  30. if (prop === 'value') {
  31. return getValuePropValue(child);
  32. }
  33. if (prop === 'children') {
  34. var newChild = child.$slots ? cloneVNodes(child.$slots['default'], true) : cloneVNodes(child.componentOptions.children, true);
  35. if (newChild.length === 1 && !newChild[0].tag) {
  36. return newChild[0].text;
  37. }
  38. return newChild;
  39. }
  40. var data = getPropsData(child);
  41. if (prop in data) {
  42. return data[prop];
  43. } else {
  44. return getAttrs(child)[prop];
  45. }
  46. }
  47. export function isMultiple(props) {
  48. return props.multiple;
  49. }
  50. export function isCombobox(props) {
  51. return props.combobox;
  52. }
  53. export function isMultipleOrTags(props) {
  54. return props.multiple || props.tags;
  55. }
  56. export function isMultipleOrTagsOrCombobox(props) {
  57. return isMultipleOrTags(props) || isCombobox(props);
  58. }
  59. export function isSingleMode(props) {
  60. return !isMultipleOrTagsOrCombobox(props);
  61. }
  62. export function toArray(value) {
  63. var ret = value;
  64. if (value === undefined) {
  65. ret = [];
  66. } else if (!Array.isArray(value)) {
  67. ret = [value];
  68. }
  69. return ret;
  70. }
  71. export function getMapKey(value) {
  72. return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) + '-' + value;
  73. }
  74. export function preventDefaultEvent(e) {
  75. e.preventDefault();
  76. }
  77. export function findIndexInValueBySingleValue(value, singleValue) {
  78. var index = -1;
  79. if (value) {
  80. for (var i = 0; i < value.length; i++) {
  81. if (value[i] === singleValue) {
  82. index = i;
  83. break;
  84. }
  85. }
  86. }
  87. return index;
  88. }
  89. export function getLabelFromPropsValue(value, key) {
  90. var label = void 0;
  91. value = toArray(value);
  92. if (value) {
  93. for (var i = 0; i < value.length; i++) {
  94. if (value[i].key === key) {
  95. label = value[i].label;
  96. break;
  97. }
  98. }
  99. }
  100. return label;
  101. }
  102. export function getSelectKeys(menuItems, value) {
  103. if (value === null || value === undefined) {
  104. return [];
  105. }
  106. var selectedKeys = [];
  107. menuItems.forEach(function (item) {
  108. if (getSlotOptions(item).isMenuItemGroup) {
  109. selectedKeys = selectedKeys.concat(getSelectKeys(item.componentOptions.children, value));
  110. } else {
  111. var itemValue = getValuePropValue(item);
  112. var itemKey = item.key;
  113. if (findIndexInValueBySingleValue(value, itemValue) !== -1 && itemKey !== undefined) {
  114. selectedKeys.push(itemKey);
  115. }
  116. }
  117. });
  118. return selectedKeys;
  119. }
  120. export var UNSELECTABLE_STYLE = {
  121. userSelect: 'none',
  122. WebkitUserSelect: 'none'
  123. };
  124. export var UNSELECTABLE_ATTRIBUTE = {
  125. unselectable: 'on'
  126. };
  127. export function findFirstMenuItem(children) {
  128. for (var i = 0; i < children.length; i++) {
  129. var child = children[i];
  130. var props = getPropsData(child);
  131. if (getSlotOptions(child).isMenuItemGroup) {
  132. var found = findFirstMenuItem(child.componentOptions.children);
  133. if (found) {
  134. return found;
  135. }
  136. } else if (!(props.disabled || props.disabled === '')) {
  137. return child;
  138. }
  139. }
  140. return null;
  141. }
  142. export function includesSeparators(str, separators) {
  143. for (var i = 0; i < separators.length; ++i) {
  144. if (str.lastIndexOf(separators[i]) > 0) {
  145. return true;
  146. }
  147. }
  148. return false;
  149. }
  150. export function splitBySeparators(str, separators) {
  151. var reg = new RegExp('[' + separators.join() + ']');
  152. return str.split(reg).filter(function (token) {
  153. return token;
  154. });
  155. }
  156. export function defaultFilterFn(input, child) {
  157. var props = getPropsData(child);
  158. if (props.disabled) {
  159. return false;
  160. }
  161. var value = getPropValue(child, this.optionFilterProp);
  162. if (value.length && value[0].text) {
  163. value = value[0].text;
  164. } else {
  165. value = String(value);
  166. }
  167. return value.toLowerCase().indexOf(input.toLowerCase()) > -1;
  168. }
  169. export function validateOptionValue(value, props) {
  170. if (isSingleMode(props) || isMultiple(props)) {
  171. return;
  172. }
  173. if (typeof value !== 'string') {
  174. throw new Error('Invalid `value` of type `' + (typeof value === 'undefined' ? 'undefined' : _typeof(value)) + '` supplied to Option, ' + 'expected `string` when `tags/combobox` is `true`.');
  175. }
  176. }
  177. export function saveRef(instance, name) {
  178. return function (node) {
  179. instance[name] = node;
  180. };
  181. }
  182. export function generateUUID() {
  183. if (process.env.NODE_ENV === 'test') {
  184. return 'test-uuid';
  185. }
  186. var d = new Date().getTime();
  187. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  188. var r = (d + Math.random() * 16) % 16 | 0;
  189. d = Math.floor(d / 16);
  190. return (c === 'x' ? r : r & 0x7 | 0x8).toString(16);
  191. });
  192. return uuid;
  193. }