Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(react-keytips): handle KEYTIP_REMOVED/UPDATED in useKeytipsManager",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
"comment": "fix(react-keytips): handle KEYTIP_REMOVED/UPDATED in useKeytipsManager",
"comment": "fix: handle KEYTIP_REMOVED/UPDATED in useKeytipsManager",

"packageName": "@fluentui-contrib/react-keytips",
"email": "rwinkelman@users.noreply.github.com",
"dependentChangeType": "patch"
}
49 changes: 49 additions & 0 deletions packages/react-keytips/src/hooks/useKeytipsManager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useKeytipsManager } from './useKeytipsManager';
import { renderHook } from '@testing-library/react';

jest.mock('@fluentui/react-components', () => {
return {
useId: () => 'keytip-id',
useFluent: jest.fn().mockReturnValue({
targetDocument: document,
}),
};
});

describe('useKeytipsManager', () => {
it('keeps registered keytips in sync through update and removal', () => {
const { result } = renderHook(() => useKeytipsManager());

const uniqueId = result.current.register({
content: 'A',
keySequences: ['a'],
});

expect(result.current.getKeytips()[uniqueId]).toEqual(
expect.objectContaining({
uniqueId,
content: 'A',
keySequences: ['a'],
})
);

result.current.update({
uniqueId,
content: 'B',
keySequences: ['b'],
});

expect(result.current.getKeytips()[uniqueId]).toEqual(
expect.objectContaining({
uniqueId,
content: 'B',
keySequences: ['b'],
})
);
expect(result.current.getKeytips()[uniqueId].content).toBe('B');

result.current.unregister(uniqueId);

expect(result.current.getKeytips()[uniqueId]).toBeUndefined();
});
});
8 changes: 8 additions & 0 deletions packages/react-keytips/src/hooks/useKeytipsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export function useKeytipsManager(): {
keytips.current[keytip.uniqueId] = keytip;
});

subscribe(EVENTS.KEYTIP_UPDATED, (keytip) => {
keytips.current[keytip.uniqueId] = keytip;
});

subscribe(EVENTS.KEYTIP_REMOVED, (uniqueId) => {
delete keytips.current[uniqueId];
});

subscribe(EVENTS.ENTER_KEYTIP_MODE, () => {
isKeytipModeEnabled.current = true;
});
Expand Down
Loading