Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React events: initial implementation of disabled prop #15458

Merged
merged 1 commit into from
Apr 22, 2019
Merged
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
10 changes: 10 additions & 0 deletions packages/react-events/src/Focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,20 @@ const FocusResponder = {
const shouldStopPropagation =
props.stopPropagation === undefined ? true : props.stopPropagation;

if (props.disabled) {
if (state.isFocused) {
dispatchFocusOutEvents(context, props, state);
state.isFocused = false;
state.focusTarget = null;
}
return false;
}

// Focus doesn't handle capture target events at this point
if (phase === CAPTURE_PHASE) {
return false;
}

switch (type) {
case 'focus': {
if (!state.isFocused) {
Expand Down
11 changes: 11 additions & 0 deletions packages/react-events/src/Hover.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ const HoverResponder = {
): boolean {
const {type} = event;

if (props.disabled) {
if (state.isHovered) {
dispatchHoverEndEvents(event, context, props, state);
state.ignoreEmulatedMouseEvents = false;
}
if (state.isTouched) {
state.isTouched = false;
}
return false;
}

// Hover doesn't handle capture target events at this point
if (event.phase === CAPTURE_PHASE) {
return false;
Expand Down
7 changes: 7 additions & 0 deletions packages/react-events/src/Press.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ const PressResponder = {
): boolean {
const {phase, target, type} = event;
if (props.disabled) {
dispatchPressEndEvents(context, props, state);
context.removeRootEventTypes(rootEventTypes);
state.ignoreEmulatedMouseEvents = false;
return false;
}
// Press doesn't handle capture target events at this point
if (phase === CAPTURE_PHASE) {
return false;
Expand Down
23 changes: 23 additions & 0 deletions packages/react-events/src/__tests__/Focus-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ describe('Focus event responder', () => {
container = null;
});

describe('disabled', () => {
let onBlur, onFocus, ref;

beforeEach(() => {
onBlur = jest.fn();
onFocus = jest.fn();
ref = React.createRef();
const element = (
<Focus disabled={true} onBlur={onBlur} onFocus={onFocus}>
<div ref={ref} />
</Focus>
);
ReactDOM.render(element, container);
});

it('prevents custom events being dispatched', () => {
ref.current.dispatchEvent(createFocusEvent('focus'));
ref.current.dispatchEvent(createFocusEvent('blur'));
expect(onFocus).not.toBeCalled();
expect(onBlur).not.toBeCalled();
});
});

describe('onBlur', () => {
let onBlur, ref;

Expand Down
26 changes: 26 additions & 0 deletions packages/react-events/src/__tests__/Hover-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ describe('Hover event responder', () => {
container = null;
});

describe('disabled', () => {
let onHoverStart, onHoverEnd, ref;

beforeEach(() => {
onHoverStart = jest.fn();
onHoverEnd = jest.fn();
ref = React.createRef();
const element = (
<Hover
disabled={true}
onHoverStart={onHoverStart}
onHoverEnd={onHoverEnd}>
<div ref={ref} />
</Hover>
);
ReactDOM.render(element, container);
});

it('prevents custom events being dispatched', () => {
ref.current.dispatchEvent(createPointerEvent('pointerover'));
ref.current.dispatchEvent(createPointerEvent('pointerout'));
expect(onHoverStart).not.toBeCalled();
expect(onHoverEnd).not.toBeCalled();
});
});

describe('onHoverStart', () => {
let onHoverStart, ref;

Expand Down
29 changes: 29 additions & 0 deletions packages/react-events/src/__tests__/Press-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,35 @@ describe('Event responder: Press', () => {
container = null;
});

describe('disabled', () => {
let onPressStart, onPress, onPressEnd, ref;

beforeEach(() => {
onPressStart = jest.fn();
onPress = jest.fn();
onPressEnd = jest.fn();
ref = React.createRef();
const element = (
<Press
disabled={true}
onPressStart={onPressStart}
onPress={onPress}
onPressEnd={onPressEnd}>
<div ref={ref} />
</Press>
);
ReactDOM.render(element, container);
});

it('prevents custom events being dispatched', () => {
ref.current.dispatchEvent(createPointerEvent('pointerdown'));
ref.current.dispatchEvent(createPointerEvent('pointerup'));
expect(onPressStart).not.toBeCalled();
expect(onPress).not.toBeCalled();
expect(onPressEnd).not.toBeCalled();
});
});

describe('onPressStart', () => {
let onPressStart, ref;

Expand Down