forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
189 lines (175 loc) · 4.36 KB
/
index.jsx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import classNames from 'classnames';
import omit from 'omit.js';
import PropTypes from '../_util/vue-types';
import VcMentions from '../vc-mentions';
import { mentionsProps } from '../vc-mentions/src/mentionsProps';
import Base from '../base';
import Spin from '../spin';
import BaseMixin from '../_util/BaseMixin';
import { ConfigConsumerProps } from '../config-provider';
import {
getOptionProps,
getComponentFromProp,
getListeners,
filterEmpty,
} from '../_util/props-util';
const { Option } = VcMentions;
function loadingFilterOption() {
return true;
}
function getMentions(value = '', config) {
const { prefix = '@', split = ' ' } = config || {};
const prefixList = Array.isArray(prefix) ? prefix : [prefix];
return value
.split(split)
.map((str = '') => {
let hitPrefix = null;
prefixList.some(prefixStr => {
const startStr = str.slice(0, prefixStr.length);
if (startStr === prefixStr) {
hitPrefix = prefixStr;
return true;
}
return false;
});
if (hitPrefix !== null) {
return {
prefix: hitPrefix,
value: str.slice(hitPrefix.length),
};
}
return null;
})
.filter(entity => !!entity && !!entity.value);
}
const Mentions = {
name: 'AMentions',
mixins: [BaseMixin],
inheritAttrs: false,
model: {
prop: 'value',
event: 'change',
},
Option: { ...Option, name: 'AMentionsOption' },
getMentions,
props: {
...mentionsProps,
loading: PropTypes.bool,
},
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
data() {
return {
focused: false,
};
},
mounted() {
this.$nextTick(() => {
if (this.autoFocus) {
this.focus();
}
});
},
methods: {
onFocus(...args) {
this.$emit('focus', ...args);
this.setState({
focused: true,
});
},
onBlur(...args) {
this.$emit('blur', ...args);
this.setState({
focused: false,
});
},
onSelect(...args) {
this.$emit('select', ...args);
this.setState({
focused: true,
});
},
onChange(val) {
this.$emit('change', val);
},
getNotFoundContent(renderEmpty) {
const h = this.$createElement;
const notFoundContent = getComponentFromProp(this, 'notFoundContent');
if (notFoundContent !== undefined) {
return notFoundContent;
}
return renderEmpty(h, 'Select');
},
getOptions() {
const { loading } = this.$props;
const children = filterEmpty(this.$slots.default || []);
if (loading) {
return (
<Option value="ANTD_SEARCHING" disabled>
<Spin size="small" />
</Option>
);
}
return children;
},
getFilterOption() {
const { filterOption, loading } = this.$props;
if (loading) {
return loadingFilterOption;
}
return filterOption;
},
focus() {
this.$refs.vcMentions.focus();
},
blur() {
this.$refs.vcMentions.blur();
},
},
render() {
const { focused } = this.$data;
const { getPrefixCls, renderEmpty } = this.configProvider;
const {
prefixCls: customizePrefixCls,
disabled,
getPopupContainer,
...restProps
} = getOptionProps(this);
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
const otherProps = omit(restProps, ['loading']);
const mergedClassName = classNames({
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-focused`]: focused,
});
const mentionsProps = {
props: {
prefixCls,
notFoundContent: this.getNotFoundContent(renderEmpty),
...otherProps,
disabled,
filterOption: this.getFilterOption(),
getPopupContainer: getPopupContainer,
children: this.getOptions(),
},
class: mergedClassName,
attrs: { rows: 1, ...this.$attrs },
on: {
...getListeners(this),
change: this.onChange,
select: this.onSelect,
focus: this.onFocus,
blur: this.onBlur,
},
ref: 'vcMentions',
};
return <VcMentions {...mentionsProps} />;
},
};
/* istanbul ignore next */
Mentions.install = function(Vue) {
Vue.use(Base);
Vue.component(Mentions.name, Mentions);
Vue.component(Mentions.Option.name, Mentions.Option);
};
export default Mentions;