Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactivePick): new function (#424)
- Loading branch information
Showing
6 changed files
with
121 additions
and
1 deletion.
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
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,98 @@ | ||
--- | ||
category: Utilities | ||
--- | ||
|
||
# reactivePick | ||
|
||
Reactively pick fields from a reactive object. | ||
|
||
## Usage | ||
|
||
```js | ||
import { reactivePick } from '@vueuse/core' | ||
|
||
const obj = reactive({ | ||
x: 0, | ||
y: 0, | ||
elementX: 0, | ||
elementY: 0, | ||
}) | ||
|
||
const picked = reactivePick(obj, 'x', 'elementX') // { x: number, elementX: number } | ||
``` | ||
|
||
### Scenarios | ||
|
||
#### Selectively passing props to child | ||
|
||
```html | ||
<script setup> | ||
import { defineProps } from 'vue' | ||
import { reactivePick } from '@vueuse/core' | ||
const props = defineProps({ | ||
value: { | ||
default: 'value', | ||
}, | ||
color: { | ||
type: String, | ||
}, | ||
font: { | ||
type: String, | ||
} | ||
}) | ||
const childProps = reactivePick(props, 'color', 'font') | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<!-- only passes "color" and "font" props to child --> | ||
<ChildComp v-bind="childProps" /> | ||
</div> | ||
</template> | ||
``` | ||
|
||
#### Selectively wrap reactive object | ||
|
||
Instead of doing this | ||
|
||
```ts | ||
import { reactive } from 'vue' | ||
import { useElementBounding } from '@vueuse/core' | ||
|
||
const { height, width } = useElementBounding() // object of refs | ||
const size = reactive({ height, width }) | ||
``` | ||
|
||
Now we can just have this | ||
|
||
```ts | ||
import { reactivePick, useElementBounding } from '@vueuse/core' | ||
|
||
const size = reactivePick(useElementBounding(), 'height', 'width') | ||
``` | ||
|
||
<!--FOOTER_STARTS--> | ||
## Type Declarations | ||
|
||
```typescript | ||
/** | ||
* Reactively pick fields from a reactive object | ||
* | ||
* @link https://vueuse.js.org/reactivePick | ||
*/ | ||
export declare function reactivePick<T extends object, K extends keyof T>( | ||
obj: T, | ||
...keys: K[] | ||
): { | ||
[S in K]: UnwrapRef<T[S]> | ||
} | ||
``` | ||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/shared/reactivePick/index.ts) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/shared/reactivePick/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,13 @@ | ||
import { reactive, toRef, UnwrapRef } from 'vue-demi' | ||
|
||
/** | ||
* Reactively pick fields from a reactive object | ||
* | ||
* @link https://vueuse.js.org/reactivePick | ||
*/ | ||
export function reactivePick<T extends object, K extends keyof T>( | ||
obj: T, | ||
...keys: K[] | ||
): { [S in K]: UnwrapRef<T[S]> } { | ||
return reactive(Object.fromEntries(keys.map(k => [k, toRef(obj, k)]))) as any | ||
} |