forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
254 lines (242 loc) · 7.91 KB
/
index.tsx
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import type { CSSProperties, ExtractPropTypes, PropType, VNode } from 'vue';
import { watch, defineComponent, ref, reactive, onMounted } from 'vue';
import { initDefaultProps, findDOMNode } from '../_util/props-util';
import { withInstall } from '../_util/type';
import { getOffsetLeft } from './util';
import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import KeyCode from '../_util/KeyCode';
import StarFilled from '@ant-design/icons-vue/StarFilled';
import Tooltip from '../tooltip';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import Star from './Star';
import useRefs from '../_util/hooks/useRefs';
import { useInjectFormItemContext } from '../form/FormItemContext';
import type { Direction } from '../config-provider';
import type { FocusEventHandler, KeyboardEventHandler } from '../_util/EventInterface';
import useStyle from './style';
export const rateProps = () => ({
prefixCls: String,
count: Number,
value: Number,
allowHalf: { type: Boolean, default: undefined },
allowClear: { type: Boolean, default: undefined },
tooltips: Array as PropType<string[]>,
disabled: { type: Boolean, default: undefined },
character: PropTypes.any,
autofocus: { type: Boolean, default: undefined },
tabindex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
direction: String as PropType<Direction>,
id: String,
onChange: Function as PropType<(value: number) => void>,
onHoverChange: Function as PropType<(value: number) => void>,
'onUpdate:value': Function as PropType<(value: number) => void>,
onFocus: Function as PropType<FocusEventHandler>,
onBlur: Function as PropType<FocusEventHandler>,
onKeydown: Function as PropType<KeyboardEventHandler>,
});
export type RateProps = Partial<ExtractPropTypes<ReturnType<typeof rateProps>>>;
const Rate = defineComponent({
compatConfig: { MODE: 3 },
name: 'ARate',
inheritAttrs: false,
props: initDefaultProps(rateProps(), {
value: 0,
count: 5,
allowHalf: false,
allowClear: true,
tabindex: 0,
direction: 'ltr',
}),
// emits: ['hoverChange', 'update:value', 'change', 'focus', 'blur', 'keydown'],
setup(props, { slots, attrs, emit, expose }) {
const { prefixCls, direction } = useConfigInject('rate', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const formItemContext = useInjectFormItemContext();
const rateRef = ref();
const [setRef, starRefs] = useRefs();
const state = reactive({
value: props.value,
focused: false,
cleanedValue: null,
hoverValue: undefined,
});
watch(
() => props.value,
() => {
state.value = props.value;
},
);
const getStarDOM = (index: number) => {
return findDOMNode(starRefs.value.get(index));
};
const getStarValue = (index: number, x: number) => {
const reverse = direction.value === 'rtl';
let value = index + 1;
if (props.allowHalf) {
const starEle = getStarDOM(index);
const leftDis = getOffsetLeft(starEle);
const width = starEle.clientWidth;
if (reverse && x - leftDis > width / 2) {
value -= 0.5;
} else if (!reverse && x - leftDis < width / 2) {
value -= 0.5;
}
}
return value;
};
const changeValue = (value: number) => {
if (props.value === undefined) {
state.value = value;
}
emit('update:value', value);
emit('change', value);
formItemContext.onFieldChange();
};
const onHover = (e: MouseEvent, index: number) => {
const hoverValue = getStarValue(index, e.pageX);
if (hoverValue !== state.cleanedValue) {
state.hoverValue = hoverValue;
state.cleanedValue = null;
}
emit('hoverChange', hoverValue);
};
const onMouseLeave = () => {
state.hoverValue = undefined;
state.cleanedValue = null;
emit('hoverChange', undefined);
};
const onClick = (event: MouseEvent, index: number) => {
const { allowClear } = props;
const newValue = getStarValue(index, event.pageX);
let isReset = false;
if (allowClear) {
isReset = newValue === state.value;
}
onMouseLeave();
changeValue(isReset ? 0 : newValue);
state.cleanedValue = isReset ? newValue : null;
};
const onFocus = (e: FocusEvent) => {
state.focused = true;
emit('focus', e);
};
const onBlur = (e: FocusEvent) => {
state.focused = false;
emit('blur', e);
formItemContext.onFieldBlur();
};
const onKeyDown = (event: KeyboardEvent) => {
const { keyCode } = event;
const { count, allowHalf } = props;
const reverse = direction.value === 'rtl';
if (keyCode === KeyCode.RIGHT && state.value < count && !reverse) {
if (allowHalf) {
state.value += 0.5;
} else {
state.value += 1;
}
changeValue(state.value);
event.preventDefault();
} else if (keyCode === KeyCode.LEFT && state.value > 0 && !reverse) {
if (allowHalf) {
state.value -= 0.5;
} else {
state.value -= 1;
}
changeValue(state.value);
event.preventDefault();
} else if (keyCode === KeyCode.RIGHT && state.value > 0 && reverse) {
if (allowHalf) {
state.value -= 0.5;
} else {
state.value -= 1;
}
changeValue(state.value);
event.preventDefault();
} else if (keyCode === KeyCode.LEFT && state.value < count && reverse) {
if (allowHalf) {
state.value += 0.5;
} else {
state.value += 1;
}
changeValue(state.value);
event.preventDefault();
}
emit('keydown', event);
};
const focus = () => {
if (!props.disabled) {
rateRef.value.focus();
}
};
const blur = () => {
if (!props.disabled) {
rateRef.value.blur();
}
};
expose({
focus,
blur,
});
onMounted(() => {
const { autofocus, disabled } = props;
if (autofocus && !disabled) {
focus();
}
});
const characterRender = (node: VNode, { index }) => {
const { tooltips } = props;
if (!tooltips) return node;
return <Tooltip title={tooltips[index]}>{node}</Tooltip>;
};
return () => {
const { count, allowHalf, disabled, tabindex, id = formItemContext.id.value } = props;
const { class: className, style } = attrs;
const stars = [];
const disabledClass = disabled ? `${prefixCls.value}-disabled` : '';
const character = props.character || slots.character || (() => <StarFilled />);
for (let index = 0; index < count; index++) {
stars.push(
<Star
ref={setRef(index)}
key={index}
index={index}
count={count}
disabled={disabled}
prefixCls={`${prefixCls.value}-star`}
allowHalf={allowHalf}
value={state.hoverValue === undefined ? state.value : state.hoverValue}
onClick={onClick}
onHover={onHover}
character={character}
characterRender={characterRender}
focused={state.focused}
/>,
);
}
const rateClassName = classNames(prefixCls.value, disabledClass, className, {
[hashId.value]: true,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
});
return wrapSSR(
<ul
{...attrs}
id={id}
class={rateClassName}
style={style as CSSProperties}
onMouseleave={disabled ? null : onMouseLeave}
tabindex={disabled ? -1 : tabindex}
onFocus={disabled ? null : onFocus}
onBlur={disabled ? null : onBlur}
onKeydown={disabled ? null : onKeyDown}
ref={rateRef}
role="radiogroup"
>
{stars}
</ul>,
);
};
},
});
export default withInstall(Rate);