Skip to content

Commit 7ec594c

Browse files
committed
fix: add useRef hook
1 parent 2b78d2d commit 7ec594c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

components/_util/hooks/useRef.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { onBeforeUpdate, readonly, ref, DeepReadonly, UnwrapRef } from 'vue';
2+
3+
export type UseRef = [(el: any) => void, DeepReadonly<UnwrapRef<any[]>>];
4+
5+
export const useRef = (): UseRef => {
6+
const refs = ref<any>([]);
7+
const setRef = (el: any) => {
8+
refs.value.push(el);
9+
};
10+
onBeforeUpdate(() => {
11+
refs.value = [];
12+
});
13+
return [setRef, readonly(refs)];
14+
};

components/rate/index.tsx

+4-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Tooltip from '../tooltip';
1919
import useConfigInject from '../_util/hooks/useConfigInject';
2020

2121
import Star from './Star';
22+
import { useRef } from '../_util/hooks/useRef';
2223

2324
export const rateProps = {
2425
prefixCls: PropTypes.string,
@@ -52,22 +53,16 @@ const Rate = defineComponent({
5253
setup(props, { slots, attrs, emit, expose }) {
5354
const { prefixCls, direction } = useConfigInject('rate', props);
5455
const rateRef = ref();
55-
const starRefs = ref([]);
56+
const [setRef, starRefs] = useRef();
5657
const state = reactive({
5758
sValue: props.value,
5859
focused: false,
5960
cleanedValue: null,
6061
hoverValue: undefined,
6162
});
62-
const saveRef = (el: any) => {
63-
starRefs.value.push(el);
64-
};
65-
onBeforeUpdate(() => {
66-
starRefs.value = [];
67-
});
6863

6964
const getStarDOM = index => {
70-
return findDOMNode(starRefs.value[index]);
65+
return findDOMNode(starRefs[index]);
7166
};
7267
const getStarValue = (index, x) => {
7368
const reverse = direction.value === 'rtl';
@@ -200,7 +195,7 @@ const Rate = defineComponent({
200195
for (let index = 0; index < count; index++) {
201196
stars.push(
202197
<Star
203-
ref={saveRef}
198+
ref={setRef}
204199
key={index}
205200
index={index}
206201
count={count}

0 commit comments

Comments
 (0)