Skip to content

Commit

Permalink
fix: useMergedTabsterAttributes should accept null/undefined values (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ling1726 authored Oct 9, 2024
1 parent bfc283c commit b474188
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: useMergedTabsterAttributes should accept null/undefined values",
"packageName": "@fluentui/react-tabster",
"email": "lingfangao@hotmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ export function useFocusWithin<TElement extends HTMLElement = HTMLElement>(): Re
export function useKeyboardNavAttribute<E extends HTMLElement>(): RefObject<E>;

// @internal
export const useMergedTabsterAttributes_unstable: (...attributes: Partial<Types.TabsterDOMAttribute>[]) => Types.TabsterDOMAttribute;
export const useMergedTabsterAttributes_unstable: (...attributes: (Partial<Types.TabsterDOMAttribute> | null | undefined)[]) => Types.TabsterDOMAttribute;

// @public
export const useModalAttributes: (options?: UseModalAttributesOptions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,17 @@ describe('useMergedTabsterAttributes', () => {
const { result } = renderHook(() => useMergedTabsterAttributes_unstable());
expect(result.current).toEqual({ 'data-tabster': undefined });
});

it('should ignore undefined/null values', () => {
const { result } = renderHook(() =>
useMergedTabsterAttributes_unstable(
{ 'data-tabster': '{"a":"1"}' },
null,
{ 'data-tabster': '{"b":"2"}' },
undefined,
{ 'data-tabster': '{"c":"3"}' },
),
);
expect(result.current).toEqual({ 'data-tabster': '{"a":"1","b":"2","c":"3"}' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { Types, TABSTER_ATTRIBUTE_NAME } from 'tabster';
* @returns single merged tabster attribute
*/
export const useMergedTabsterAttributes_unstable = (
...attributes: Partial<Types.TabsterDOMAttribute>[]
...attributes: (Partial<Types.TabsterDOMAttribute> | null | undefined)[]
): Types.TabsterDOMAttribute => {
'use no memo';

const stringAttributes = attributes.reduce<string[]>((acc, curr) => {
if (curr[TABSTER_ATTRIBUTE_NAME]) {
if (curr?.[TABSTER_ATTRIBUTE_NAME]) {
acc.push(curr[TABSTER_ATTRIBUTE_NAME]);
}
return acc;
Expand Down

0 comments on commit b474188

Please sign in to comment.