index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _typeof = require("@babel/runtime/helpers/typeof");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.mentionsProps = exports.default = exports.MentionsOption = void 0;
  8. var _vue = require("vue");
  9. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  10. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  11. var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
  12. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  13. var _classNames2 = _interopRequireDefault(require("../_util/classNames"));
  14. var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));
  15. var _vcMentions = _interopRequireWildcard(require("../vc-mentions"));
  16. var _mentionsProps = require("../vc-mentions/src/mentionsProps");
  17. var _useConfigInject2 = _interopRequireDefault(require("../_util/hooks/useConfigInject"));
  18. var _propsUtil = require("../_util/props-util");
  19. var _FormItemContext = require("../form/FormItemContext");
  20. var _omit = _interopRequireDefault(require("../_util/omit"));
  21. var _Option = require("../vc-mentions/src/Option");
  22. var _excluded = ["disabled", "getPopupContainer", "rows", "id"],
  23. _excluded2 = ["class"];
  24. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  25. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  26. var getMentions = function getMentions() {
  27. var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  28. var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  29. var _config$prefix = config.prefix,
  30. prefix = _config$prefix === void 0 ? '@' : _config$prefix,
  31. _config$split = config.split,
  32. split = _config$split === void 0 ? ' ' : _config$split;
  33. var prefixList = Array.isArray(prefix) ? prefix : [prefix];
  34. return value.split(split).map(function () {
  35. var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  36. var hitPrefix = null;
  37. prefixList.some(function (prefixStr) {
  38. var startStr = str.slice(0, prefixStr.length);
  39. if (startStr === prefixStr) {
  40. hitPrefix = prefixStr;
  41. return true;
  42. }
  43. return false;
  44. });
  45. if (hitPrefix !== null) {
  46. return {
  47. prefix: hitPrefix,
  48. value: str.slice(hitPrefix.length)
  49. };
  50. }
  51. return null;
  52. }).filter(function (entity) {
  53. return !!entity && !!entity.value;
  54. });
  55. };
  56. var mentionsProps = function mentionsProps() {
  57. return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, _mentionsProps.mentionsProps), {}, {
  58. loading: {
  59. type: Boolean,
  60. default: undefined
  61. },
  62. onFocus: {
  63. type: Function
  64. },
  65. onBlur: {
  66. type: Function
  67. },
  68. onSelect: {
  69. type: Function
  70. },
  71. onChange: {
  72. type: Function
  73. },
  74. onPressenter: {
  75. type: Function
  76. },
  77. 'onUpdate:value': {
  78. type: Function
  79. },
  80. notFoundContent: _vueTypes.default.any,
  81. defaultValue: String,
  82. id: String
  83. });
  84. };
  85. exports.mentionsProps = mentionsProps;
  86. var Mentions = (0, _vue.defineComponent)({
  87. compatConfig: {
  88. MODE: 3
  89. },
  90. name: 'AMentions',
  91. inheritAttrs: false,
  92. props: mentionsProps(),
  93. slots: ['notFoundContent', 'option'],
  94. setup: function setup(props, _ref) {
  95. var _ref2, _props$value;
  96. var slots = _ref.slots,
  97. emit = _ref.emit,
  98. attrs = _ref.attrs,
  99. expose = _ref.expose;
  100. var _useConfigInject = (0, _useConfigInject2.default)('mentions', props),
  101. prefixCls = _useConfigInject.prefixCls,
  102. renderEmpty = _useConfigInject.renderEmpty,
  103. direction = _useConfigInject.direction;
  104. var focused = (0, _vue.ref)(false);
  105. var vcMentions = (0, _vue.ref)(null);
  106. var value = (0, _vue.ref)((_ref2 = (_props$value = props.value) !== null && _props$value !== void 0 ? _props$value : props.defaultValue) !== null && _ref2 !== void 0 ? _ref2 : '');
  107. var formItemContext = (0, _FormItemContext.useInjectFormItemContext)();
  108. (0, _vue.watch)(function () {
  109. return props.value;
  110. }, function (val) {
  111. value.value = val;
  112. });
  113. var handleFocus = function handleFocus(e) {
  114. focused.value = true;
  115. emit('focus', e);
  116. };
  117. var handleBlur = function handleBlur(e) {
  118. focused.value = false;
  119. emit('blur', e);
  120. formItemContext.onFieldBlur();
  121. };
  122. var handleSelect = function handleSelect() {
  123. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  124. args[_key] = arguments[_key];
  125. }
  126. emit.apply(void 0, ['select'].concat(args));
  127. focused.value = true;
  128. };
  129. var handleChange = function handleChange(val) {
  130. if (props.value === undefined) {
  131. value.value = val;
  132. }
  133. emit('update:value', val);
  134. emit('change', val);
  135. formItemContext.onFieldChange();
  136. };
  137. var getNotFoundContent = function getNotFoundContent() {
  138. var notFoundContent = props.notFoundContent;
  139. if (notFoundContent !== undefined) {
  140. return notFoundContent;
  141. }
  142. if (slots.notFoundContent) {
  143. return slots.notFoundContent();
  144. }
  145. return renderEmpty.value('Select');
  146. };
  147. var getOptions = function getOptions() {
  148. var _slots$default;
  149. return (0, _propsUtil.flattenChildren)(((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)) || []).map(function (item) {
  150. var _item$children, _item$children$defaul;
  151. return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (0, _propsUtil.getOptionProps)(item)), {}, {
  152. label: (_item$children = item.children) === null || _item$children === void 0 ? void 0 : (_item$children$defaul = _item$children.default) === null || _item$children$defaul === void 0 ? void 0 : _item$children$defaul.call(_item$children)
  153. });
  154. });
  155. };
  156. var focus = function focus() {
  157. vcMentions.value.focus();
  158. };
  159. var blur = function blur() {
  160. vcMentions.value.blur();
  161. };
  162. expose({
  163. focus: focus,
  164. blur: blur
  165. });
  166. return function () {
  167. var _classNames;
  168. var disabled = props.disabled,
  169. getPopupContainer = props.getPopupContainer,
  170. _props$rows = props.rows,
  171. rows = _props$rows === void 0 ? 1 : _props$rows,
  172. _props$id = props.id,
  173. id = _props$id === void 0 ? formItemContext.id.value : _props$id,
  174. restProps = (0, _objectWithoutProperties2.default)(props, _excluded);
  175. var className = attrs.class,
  176. otherAttrs = (0, _objectWithoutProperties2.default)(attrs, _excluded2);
  177. var otherProps = (0, _omit.default)(restProps, ['defaultValue', 'onUpdate:value', 'prefixCls']);
  178. var mergedClassName = (0, _classNames2.default)(className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-disabled"), disabled), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-focused"), focused.value), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'), _classNames));
  179. var mentionsProps = (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({
  180. prefixCls: prefixCls.value
  181. }, otherProps), {}, {
  182. disabled: disabled,
  183. direction: direction.value,
  184. filterOption: props.filterOption,
  185. getPopupContainer: getPopupContainer,
  186. options: props.options || getOptions(),
  187. class: mergedClassName
  188. }, otherAttrs), {}, {
  189. rows: rows,
  190. onChange: handleChange,
  191. onSelect: handleSelect,
  192. onFocus: handleFocus,
  193. onBlur: handleBlur,
  194. ref: vcMentions,
  195. value: value.value,
  196. id: id
  197. });
  198. return (0, _vue.createVNode)(_vcMentions.default, mentionsProps, {
  199. notFoundContent: getNotFoundContent,
  200. option: slots.option
  201. });
  202. };
  203. }
  204. });
  205. /* istanbul ignore next */
  206. var MentionsOption = (0, _vue.defineComponent)((0, _objectSpread2.default)((0, _objectSpread2.default)({
  207. compatConfig: {
  208. MODE: 3
  209. }
  210. }, _vcMentions.Option), {}, {
  211. name: 'AMentionsOption',
  212. props: _Option.optionProps
  213. }));
  214. exports.MentionsOption = MentionsOption;
  215. var _default = (0, _extends2.default)(Mentions, {
  216. Option: MentionsOption,
  217. getMentions: getMentions,
  218. install: function install(app) {
  219. app.component(Mentions.name, Mentions);
  220. app.component(MentionsOption.name, MentionsOption);
  221. return app;
  222. }
  223. });
  224. exports.default = _default;