feat: bubble组件封装
This commit is contained in:
15
src/hooks/useEventCallback.ts
Normal file
15
src/hooks/useEventCallback.ts
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-08-20 23:12:14
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
|
||||
export function useEventCallback<T>(handler?: (value: T) => void): (value: T) => void {
|
||||
const callbackRef = ref(handler);
|
||||
const fn = ref((value: T) => {
|
||||
callbackRef.value && callbackRef.value(value);
|
||||
});
|
||||
callbackRef.value = handler;
|
||||
|
||||
return fn.value;
|
||||
}
|
||||
21
src/hooks/useState.ts
Normal file
21
src/hooks/useState.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/*
|
||||
* @Author: RenXiaoDong
|
||||
* @Date: 2025-08-20 23:14:22
|
||||
*/
|
||||
import type { Ref } from 'vue';
|
||||
import { ref } from 'vue';
|
||||
|
||||
export default function useState<T, R = Ref<T>>(
|
||||
defaultStateValue?: T | (() => T),
|
||||
): [R, (val: T) => void] {
|
||||
const initValue: T =
|
||||
typeof defaultStateValue === 'function' ? (defaultStateValue as any)() : defaultStateValue;
|
||||
|
||||
const innerValue = ref(initValue) as Ref<T>;
|
||||
|
||||
function triggerChange(newValue: T) {
|
||||
innerValue.value = newValue;
|
||||
}
|
||||
|
||||
return [innerValue as unknown as R, triggerChange];
|
||||
}
|
||||
Reference in New Issue
Block a user