-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(computedInject): new function (#571)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
476eba1
commit 408e269
Showing
11 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<script setup lang="ts"> | ||
import { provide, ref } from 'vue-demi' | ||
import Receiver, { ArrayKey } from './demoReceiver.vue' | ||
const array = ref([{ key: 1, value: '1' }, { key: 2, value: '2' }, { key: 3, value: '3' }]) | ||
provide(ArrayKey, array) | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<p>Array: {{ array }}</p> | ||
<Receiver /> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<template> | ||
<div>ComputedArr: {{ computedArr }}</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { InjectionKey, Ref, defineComponent, ref } from 'vue-demi' | ||
import { computedInject } from './index' | ||
type OptionsRef = Ref<{ key: number; value: string }[]> | ||
export const ArrayKey: InjectionKey<OptionsRef> = Symbol('array') | ||
export default defineComponent({ | ||
name: 'DemoProvider', | ||
setup() { | ||
const computedArr = computedInject(ArrayKey, (source) => { | ||
if (!source) return ref([]) as OptionsRef | ||
const arr = [...source.value] | ||
arr.unshift({ key: 0, value: 'all' }) | ||
return arr | ||
}) | ||
return { | ||
computedArr, | ||
} | ||
}, | ||
}) | ||
</script> | ||
|
||
<style scoped> | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
category: Component | ||
--- | ||
|
||
# computedInject | ||
|
||
Combine computed and inject | ||
|
||
## Usage | ||
|
||
In Provider Component | ||
```ts | ||
import { InjectionKey, provide, Ref, ref } from 'vue-demi'; | ||
|
||
export const ArrayKey: InjectionKey<Ref<{ key: number, value: string }[]>> = Symbol() | ||
|
||
const array = ref([{ key: 1, value: '1' }, { key: 2, value: '2' }, { key: 3, value: '3' }]) | ||
|
||
provide(ArrayKey, array) | ||
``` | ||
|
||
In Receiver Component | ||
```ts | ||
import { computedInject } from '@vueuse/core' | ||
|
||
import { ArrayKey } from "./provider" | ||
|
||
const computedArray = computedInject(ArrayKey, (source) => { | ||
const arr = [...source.value] | ||
arr.unshift({ key: 0, value: 'all' }) | ||
return arr | ||
}) | ||
``` | ||
|
||
|
||
<!--FOOTER_STARTS--> | ||
## Type Declarations | ||
|
||
```typescript | ||
export declare type ComputedInjectGetter<T, K> = ( | ||
source: T | undefined, | ||
ctx?: any | ||
) => K | ||
export declare type ComputedInjectGetterWithDefault<T, K> = ( | ||
source: T, | ||
ctx?: any | ||
) => K | ||
export declare type ComputedInjectSetter<T> = (v: T) => void | ||
export interface WritableComputedInjectOptions<T, K> { | ||
get: ComputedInjectGetter<T, K> | ||
set: ComputedInjectSetter<K> | ||
} | ||
export interface WritableComputedInjectOptionsWithDefault<T, K> { | ||
get: ComputedInjectGetterWithDefault<T, K> | ||
set: ComputedInjectSetter<K> | ||
} | ||
export declare function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
getter: ComputedInjectGetter<T, K> | ||
): ComputedRef<K | undefined> | ||
export declare function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
options: WritableComputedInjectOptions<T, K> | ||
): ComputedRef<K | undefined> | ||
export declare function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
getter: ComputedInjectGetterWithDefault<T, K>, | ||
defaultSource: T, | ||
treatDefaultAsFactory?: false | ||
): ComputedRef<K> | ||
export declare function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
options: WritableComputedInjectOptionsWithDefault<T, K>, | ||
defaultSource: T | (() => T), | ||
treatDefaultAsFactory: true | ||
): ComputedRef<K> | ||
``` | ||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/core/computedInject/index.ts) • [Demo](https://github.com/vueuse/vueuse/blob/main/packages/core/computedInject/demo.vue) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/core/computedInject/index.md) | ||
|
||
|
||
<!--FOOTER_ENDS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useInjectedSetup, Key } from '../../.test' | ||
import { ref } from 'vue-demi' | ||
import { computedInject } from '.' | ||
|
||
describe('useCounter', () => { | ||
it('should be defined', () => { | ||
expect(computedInject).toBeDefined() | ||
}) | ||
|
||
it('should be computedRef', () => { | ||
useInjectedSetup(() => { | ||
const computedNum = computedInject(Key, (source) => { | ||
if (source) return source.value + 1 | ||
}) | ||
const anotherComputedNum = computedInject(Key, (source) => { | ||
if (source) return source.value + 10 | ||
}, ref(10)) | ||
|
||
expect(computedNum.value).toBe(2) | ||
expect(anotherComputedNum.value).toBe(11) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { computed, inject, ComputedRef, InjectionKey } from 'vue-demi' | ||
|
||
export type ComputedInjectGetter<T, K> = (source: T | undefined, ctx?: any) => K | ||
export type ComputedInjectGetterWithDefault<T, K> = (source: T, ctx?: any) => K | ||
export type ComputedInjectSetter<T> = (v: T) => void | ||
|
||
export interface WritableComputedInjectOptions<T, K> { | ||
get: ComputedInjectGetter<T, K> | ||
set: ComputedInjectSetter<K> | ||
} | ||
|
||
export interface WritableComputedInjectOptionsWithDefault<T, K> { | ||
get: ComputedInjectGetterWithDefault<T, K> | ||
set: ComputedInjectSetter<K> | ||
} | ||
|
||
export function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
getter: ComputedInjectGetter<T, K> | ||
): ComputedRef<K | undefined> | ||
export function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
options: WritableComputedInjectOptions<T, K> | ||
): ComputedRef<K | undefined> | ||
export function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
getter: ComputedInjectGetterWithDefault<T, K>, | ||
defaultSource: T, | ||
treatDefaultAsFactory?: false | ||
): ComputedRef<K> | ||
export function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
options: WritableComputedInjectOptionsWithDefault<T, K>, | ||
defaultSource: T | (() => T), | ||
treatDefaultAsFactory: true | ||
): ComputedRef<K> | ||
export function computedInject<T, K = any>( | ||
key: InjectionKey<T> | string, | ||
options: ComputedInjectGetter<T, K> | WritableComputedInjectOptions<T, K>, | ||
defaultSource?: T | (() => T), | ||
treatDefaultAsFactory?: boolean, | ||
) { | ||
let source = inject(key) as T | undefined | ||
if (defaultSource) | ||
source = inject(key, defaultSource) as T | ||
if (treatDefaultAsFactory) | ||
source = inject(key, defaultSource, treatDefaultAsFactory) as T | ||
|
||
if (typeof options === 'function') { | ||
return computed(ctx => options(source, ctx)) | ||
} | ||
else { | ||
return computed({ | ||
get: ctx => options.get(source, ctx), | ||
set: options.set, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters