Skip to content

Commit f6f4580

Browse files
Conrad Chanmergify[bot]
andauthored
feat(activeannotation): change active annotation id to null on blur (#442)
* feat(activeannotation): change active annotation id to null on blur * chore: address PR comments Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 52c2b6a commit f6f4580

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/components/AnnotationTarget/AnnotationTarget.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const AnnotationTarget = (props: Props, ref: React.Ref<HTMLAnchorElement>): JSX.
1818
event.stopPropagation();
1919
event.nativeEvent.stopImmediatePropagation(); // Prevents document event handlers from executing
2020
};
21+
const handleBlur = (event: React.SyntheticEvent): void => {
22+
// Rely on the native browser blur of a focused element to determine when to de-select an annotation from
23+
// being the active annotation id
24+
cancelEvent(event);
25+
onSelect(null);
26+
};
2127
const handleClick = (event: React.MouseEvent): void => {
2228
cancelEvent(event);
2329
onSelect(annotationId);
@@ -42,6 +48,7 @@ const AnnotationTarget = (props: Props, ref: React.Ref<HTMLAnchorElement>): JSX.
4248
ref={ref}
4349
className={classNames('ba-AnnotationTarget', className)}
4450
href="#" // Needed for IE11 to handle click events properly
51+
onBlur={handleBlur}
4552
onClick={handleClick}
4653
onFocus={handleFocus}
4754
onKeyPress={handleKeyPress}

src/components/AnnotationTarget/__tests__/AnnotationTarget-test.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,20 @@ describe('AnnotationTarget', () => {
4242
});
4343

4444
describe('mouse event handlers', () => {
45-
test.each(['click', 'focus'])('should cancel the %s event and trigger onSelect', event => {
45+
test.each`
46+
event | onSelectArgument
47+
${'blur'} | ${null}
48+
${'click'} | ${'1'}
49+
${'focus'} | ${'1'}
50+
`('should cancel the $event and trigger onSelect with $onSelectArgument', ({ event, onSelectArgument }) => {
4651
const wrapper = getWrapper();
4752

4853
wrapper.simulate(event, mockEvent);
4954

5055
expect(mockEvent.nativeEvent.stopImmediatePropagation).toHaveBeenCalled();
5156
expect(mockEvent.preventDefault).toHaveBeenCalled();
5257
expect(mockEvent.stopPropagation).toHaveBeenCalled();
53-
expect(defaults.onSelect).toHaveBeenCalledWith('1');
58+
expect(defaults.onSelect).toHaveBeenCalledWith(onSelectArgument);
5459
});
5560
});
5661

0 commit comments

Comments
 (0)