Skip to content

Commit

Permalink
fix: onActivation hooks not called in v2
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Jul 2, 2019
1 parent b0cb28a commit ee1cb14
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
22 changes: 22 additions & 0 deletions _tests/FocusLock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,5 +1069,27 @@ describe('react-focus-lock', () => {
}, 1);
});
});

describe('Hooks', () => {
it('onActivation/Deactivation', () => {
const onActivation = sinon.spy();
const onDeactivation = sinon.spy();
const wrapper = mount(
<div>
<FocusLock onActivation={onActivation} onDeactivation={onDeactivation}>
<button id="b1">button1</button>
<button id="b2">button2</button>
</FocusLock>
</div>
);
sinon.assert.calledOnce(onActivation);
sinon.assert.notCalled(onDeactivation);

wrapper.unmount();

sinon.assert.calledOnce(onActivation);
sinon.assert.calledOnce(onDeactivation);
})
});
});
});
53 changes: 29 additions & 24 deletions src/Lock.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,49 @@ function FocusLock(props) {
const isActive = useRef(false);
const originalFocusedElement = useRef(null);

const {
children,
disabled,
noFocusGuards,
persistentFocus,
autoFocus,
allowTextSelection,
group,
className,
whiteList,
shards = emptyArray,
as: Container = 'div',
lockProps: containerProps = {},
sideCar: SideCar,

returnFocus: shouldReturnFocus,

onActivation: onActivationCallback,
onDeactivation: onDeactivationCallback,
} = props;

// SIDE EFFECT CALLBACKS

const onActivation = useCallback(() => {
originalFocusedElement.current = (
originalFocusedElement.current || (document && document.activeElement)
);
if (observed.current && props.onActivation) {
props.onActivation(observed.current);
if (observed.current && onActivationCallback) {
onActivationCallback(observed.current);
}
isActive.current = true;
}, []);
}, [onActivationCallback]);

const onDeactivation = useCallback(() => {
isActive.current = false;
if (props.onDeactivation) {
props.onDeactivation(observed.current);
if (onDeactivationCallback) {
onDeactivationCallback(observed.current);
}
}, []);
}, [onDeactivationCallback]);

const returnFocus = useCallback(() => {
const { current } = originalFocusedElement;
if (props.returnFocus && current && current.focus) {
if (shouldReturnFocus && current && current.focus) {
current.focus();
originalFocusedElement.current = null;
}
Expand All @@ -56,27 +77,11 @@ function FocusLock(props) {

const setObserveNode = useCallback((newObserved) => {
if (observed.current !== newObserved) {
observed.current = realObserved;
observed.current = newObserved;
setObserved(newObserved);
}
}, []);

const {
children,
disabled,
noFocusGuards,
persistentFocus,
autoFocus,
allowTextSelection,
group,
className,
whiteList,
shards = emptyArray,
as: Container = 'div',
lockProps: containerProps = {},
sideCar: SideCar,
} = props;

if (process.env.NODE_ENV !== 'production') {
if (typeof allowTextSelection !== 'undefined') {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit ee1cb14

Please sign in to comment.