Skip to content

Commit

Permalink
fix(cdk:utils): fix useControlledProp defaultValue behavior (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Aug 26, 2022
1 parent 1fe3d76 commit 0ac224c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
10 changes: 5 additions & 5 deletions packages/cdk/utils/src/composable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ export function useControlledProp<T, K extends keyof T>(props: T, key: K): [Comp
export function useControlledProp<T, K extends keyof T>(
props: T,
key: K,
defaultOrFactory: T[K] | (() => T[K]),
defaultOrFactory: Exclude<T[K], undefined> | (() => Exclude<T[K], undefined>),
): [ComputedRef<Exclude<T[K], undefined>>, (value: Exclude<T[K], undefined>) => void]
export function useControlledProp<T, K extends keyof T>(
props: T,
key: K,
defaultOrFactory?: T[K] | (() => T[K]),
defaultOrFactory?: Exclude<T[K], undefined> | (() => Exclude<T[K], undefined>),
): [ComputedRef<T[K]>, (value: T[K]) => void] {
const defaultValue = props[key] ?? (isFunction(defaultOrFactory) ? defaultOrFactory() : defaultOrFactory)
const tempProp = shallowRef(defaultValue)
const defaultValue = (isFunction(defaultOrFactory) ? defaultOrFactory() : defaultOrFactory)!
const tempProp = shallowRef(props[key])

watch(
() => props[key],
value => (tempProp.value = value),
)

const state = computed(() => props[key] ?? tempProp.value!)
const state = computed(() => props[key] ?? tempProp.value ?? defaultValue)

const setState = (value: T[K]) => {
if (value !== toRaw(state.value)) {
Expand Down
8 changes: 5 additions & 3 deletions packages/site/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ const router = createRouter({
})

// fix: vscode link policy
router.beforeEach(router => {
const redirectedFrom = router.redirectedFrom
router.beforeEach(route => {
const redirectedFrom = route.redirectedFrom
if (
redirectedFrom &&
redirectedFrom.path.indexOf('%23') &&
router.fullPath.replace('#', '%23') !== redirectedFrom.path
route.fullPath.replace('#', '%23') !== redirectedFrom.path
) {
return redirectedFrom.fullPath.replace('%23', '#')
}

return
})

createApp(App).use(router).use(IduxInstall).mount('#app')

0 comments on commit 0ac224c

Please sign in to comment.