forked from microsoft/fluentui
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useMergedRefs should return an immutable function (microsoft#13712)
* useMergedRefs update to be immutable. * adding comment. * Change files * Updated to use refs. * removing unused import. * Updating api. * updating comments
- Loading branch information
Showing
4 changed files
with
101 additions
and
64 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
change/@uifabric-react-hooks-2020-06-19-12-26-25-fix-button-fixes.json
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,8 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "useMergedRefs: Merging refs should produce an immutable ref object.", | ||
"packageName": "@uifabric/react-hooks", | ||
"email": "dzearing@microsoft.com", | ||
"dependentChangeType": "patch", | ||
"date": "2020-06-19T19:26:25.030Z" | ||
} |
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 |
---|---|---|
@@ -1,53 +1,54 @@ | ||
## API Report File for "@uifabric/react-hooks" | ||
|
||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
```ts | ||
|
||
import { Async } from '@uifabric/utilities'; | ||
import * as React from 'react'; | ||
|
||
// @public (undocumented) | ||
export type ChangeCallback<TElement extends HTMLElement, TValue> = (ev: React.FormEvent<TElement> | undefined, newValue: TValue | undefined) => void; | ||
|
||
// @public | ||
export interface IUseBooleanCallbacks { | ||
setFalse: () => void; | ||
setTrue: () => void; | ||
toggle: () => void; | ||
} | ||
|
||
// @public | ||
export function useAsync(): Async; | ||
|
||
// @public | ||
export function useBoolean(initialState: boolean): [boolean, IUseBooleanCallbacks]; | ||
|
||
// @public | ||
export function useConst<T>(initialValue: T | (() => T)): T; | ||
|
||
// @public | ||
export function useConstCallback<T extends (...args: any[]) => any>(callback: T): T; | ||
|
||
// @public | ||
export function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue | undefined, defaultUncontrolledValue: TValue | undefined): Readonly<[TValue | undefined, (newValue: TValue | undefined) => void]>; | ||
|
||
// @public (undocumented) | ||
export function useControllableValue<TValue, TElement extends HTMLElement, TCallback extends ChangeCallback<TElement, TValue> | undefined>(controlledValue: TValue | undefined, defaultUncontrolledValue: TValue | undefined, onChange: TCallback): Readonly<[TValue | undefined, (newValue: TValue | undefined, ev?: React.FormEvent<TElement>) => void]>; | ||
|
||
// @public | ||
export function useForceUpdate(): () => void; | ||
|
||
// @public | ||
export function useId(prefix?: string, providedId?: string): string; | ||
|
||
// @public | ||
export function useMergedRefs<T>(...refs: React.Ref<T>[]): (instance: T) => void; | ||
|
||
// @public | ||
export function useOnEvent<TElement extends Element, TEvent extends Event>(element: React.RefObject<TElement | undefined | null> | TElement | Window | undefined | null, eventName: string, callback: (ev: TEvent) => void, useCapture?: boolean): void; | ||
|
||
|
||
// (No @packageDocumentation comment for this package) | ||
|
||
``` | ||
## API Report File for "@uifabric/react-hooks" | ||
|
||
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). | ||
```ts | ||
|
||
import { Async } from '@uifabric/utilities'; | ||
import * as React from 'react'; | ||
import { Ref } from 'react'; | ||
|
||
// @public (undocumented) | ||
export type ChangeCallback<TElement extends HTMLElement, TValue> = (ev: React.FormEvent<TElement> | undefined, newValue: TValue | undefined) => void; | ||
|
||
// @public | ||
export interface IUseBooleanCallbacks { | ||
setFalse: () => void; | ||
setTrue: () => void; | ||
toggle: () => void; | ||
} | ||
|
||
// @public | ||
export function useAsync(): Async; | ||
|
||
// @public | ||
export function useBoolean(initialState: boolean): [boolean, IUseBooleanCallbacks]; | ||
|
||
// @public | ||
export function useConst<T>(initialValue: T | (() => T)): T; | ||
|
||
// @public | ||
export function useConstCallback<T extends (...args: any[]) => any>(callback: T): T; | ||
|
||
// @public | ||
export function useControllableValue<TValue, TElement extends HTMLElement>(controlledValue: TValue | undefined, defaultUncontrolledValue: TValue | undefined): Readonly<[TValue | undefined, (newValue: TValue | undefined) => void]>; | ||
|
||
// @public (undocumented) | ||
export function useControllableValue<TValue, TElement extends HTMLElement, TCallback extends ChangeCallback<TElement, TValue> | undefined>(controlledValue: TValue | undefined, defaultUncontrolledValue: TValue | undefined, onChange: TCallback): Readonly<[TValue | undefined, (newValue: TValue | undefined, ev?: React.FormEvent<TElement>) => void]>; | ||
|
||
// @public | ||
export function useForceUpdate(): () => void; | ||
|
||
// @public | ||
export function useId(prefix?: string, providedId?: string): string; | ||
|
||
// @public | ||
export function useMergedRefs<T>(...refs: Ref<T>[]): (instance: T) => void; | ||
|
||
// @public | ||
export function useOnEvent<TElement extends Element, TEvent extends Event>(element: React.RefObject<TElement | undefined | null> | TElement | Window | undefined | null, eventName: string, callback: (ev: TEvent) => void, useCapture?: boolean): void; | ||
|
||
|
||
// (No @packageDocumentation comment for this package) | ||
|
||
``` |
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 |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import * as React from 'react'; | ||
import { useRef, useCallback, Ref, MutableRefObject } from 'react'; | ||
|
||
/** | ||
* React hook to merge multiple React refs (either MutableRefObjects or ref callbacks) into a single ref callback that | ||
* updates all provided refs | ||
* @param refs- Refs to collectively update with one ref value. | ||
*/ | ||
export function useMergedRefs<T>(...refs: React.Ref<T>[]): (instance: T) => void { | ||
return React.useCallback((value: T) => { | ||
refs.forEach(ref => { | ||
export function useMergedRefs<T>(...refs: Ref<T>[]): (instance: T) => void { | ||
const state = useRef<(Ref<T> | undefined)[]>(); | ||
|
||
// Update refs list. | ||
state.current = refs; | ||
|
||
return useCallback((value: T) => { | ||
for (const ref of state.current!) { | ||
if (typeof ref === 'function') { | ||
ref(value); | ||
} else if (ref) { | ||
// work around the immutability of the React.Ref type | ||
((ref as unknown) as React.MutableRefObject<T>).current = value; | ||
((ref as unknown) as MutableRefObject<T>).current = value; | ||
} | ||
}); | ||
}, refs); | ||
} | ||
}, []); | ||
} |