Skip to content

Commit

Permalink
Selected fields can't be blured by tab (#65) thanks to @gandalf92
Browse files Browse the repository at this point in the history
* * Disable inputs and set tabindex of parent container to 0 if select is disabled.

* * Updated snapshots and added various tests for input. and index.spec

* * Stop events from bubbeling and set tabindex of dropdown-input to -1

* * Updated tests an snapshots
  • Loading branch information
gandalf92 authored and sanusart committed Jan 23, 2020
1 parent 490bd59 commit 819bb5a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
7 changes: 7 additions & 0 deletions __tests__/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ exports[`<Select/> component <Select/> is disabled 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand Down Expand Up @@ -249,6 +250,7 @@ exports[`<Select/> component <Select/> renders correctly 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand Down Expand Up @@ -401,6 +403,7 @@ exports[`<Select/> component <Select/> renders with clearable 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand Down Expand Up @@ -568,6 +571,7 @@ exports[`<Select/> component <Select/> renders with loading 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand Down Expand Up @@ -710,6 +714,7 @@ exports[`<Select/> component <Select/> renders with name 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand All @@ -730,6 +735,7 @@ exports[`<Select/> component <Select/> renders with name 1`] = `
"width": 0,
}
}
tabIndex={-1}
value={Array []}
/>
<div
Expand Down Expand Up @@ -869,6 +875,7 @@ exports[`<Select/> component <Select/> renders with separator 1`] = `
onBlur={[Function]}
onChange={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder="Select..."
readOnly={false}
Expand Down
33 changes: 24 additions & 9 deletions __tests__/components/Content.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react';
import TestRenderer from 'react-test-renderer';
import { unmountComponentAtNode, render } from 'react-dom';
import { act } from 'react-dom/test-utils';

import Content from '../../src/components/Content';
import { options } from '../options';

let spy;
let container = null;

const props = (props = {}) => ({
props: {
Expand All @@ -17,19 +19,22 @@ const props = (props = {}) => ({
values: [options[0]]
},
methods: {
dropDown: () => undefined,
getInputSize: () => undefined,
dropDown: jest.fn(),
getInputSize: () => undefined
},
...props
});

describe('<Clear/> component', () => {
beforeEach(() => {
spy = jest.fn();
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
spy = null;
unmountComponentAtNode(container);
container.remove();
container = null;
});

it('<Content/> renders correctly', () => {
Expand All @@ -39,10 +44,20 @@ describe('<Clear/> component', () => {
});

it('onClick opens dropdown', () => {
TestRenderer.create(
<Content {...props()} onClick={spy}/>)
.root.findByProps({ className: 'react-dropdown-select-content react-dropdown-select-type-multi' }).props.onClick();
const componentProps = props();

expect(spy).toHaveBeenCalled;
act(() => {
render(<Content {...componentProps}/>, container);
});

const content = document.querySelector('.react-dropdown-select-content');

expect(componentProps.methods.dropDown).toHaveBeenCalledTimes(0);

act(() => {
content.dispatchEvent(new MouseEvent('click', { bubbles: true }));
});

expect(componentProps.methods.dropDown).toHaveBeenCalledTimes(1);
});
});
1 change: 1 addition & 0 deletions __tests__/components/__snapshots__/Content.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ exports[`<Clear/> component <Content/> renders correctly 1`] = `
className="react-dropdown-select-input emotion-2 emotion-3"
onBlur={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder=""
readOnly={true}
Expand Down
2 changes: 2 additions & 0 deletions __tests__/components/__snapshots__/Input.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports[`<Input/> is disabled 1`] = `
className="react-dropdown-select-input emotion-0 emotion-1"
onBlur={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder=""
readOnly={false}
Expand All @@ -43,6 +44,7 @@ exports[`<Input/> renders correctly 1`] = `
className="react-dropdown-select-input emotion-0 emotion-1"
onBlur={[Function]}
onClick={[Function]}
onFocus={[Function]}
onKeyPress={[Function]}
placeholder=""
readOnly={false}
Expand Down
5 changes: 4 additions & 1 deletion src/components/Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const Content = ({ props, state, methods }) => {
className={`${LIB_NAME}-content ${
props.multi ? `${LIB_NAME}-type-multi` : `${LIB_NAME}-type-single`
}`}
onClick={() => methods.dropDown('open')}>
onClick={(event) => {
event.stopPropagation();
methods.dropDown('open');
}}>
{props.contentRenderer ? (
props.contentRenderer({ props, state, methods })
) : (
Expand Down
6 changes: 4 additions & 2 deletions src/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ class Input extends Component {
}

if (prevProps.state.dropdown !== this.props.state.dropdown && !this.props.state.dropdown) {
this.input.current.blur();
this.input.current.blur();
}
}

onBlur = () => {
onBlur = (event) => {
event.stopPropagation();
if (!this.props.state.dropdown) {
return this.input.current.blur();
}
Expand Down Expand Up @@ -72,6 +73,7 @@ class Input extends Component {
<InputComponent
ref={this.input}
tabIndex="-1"
onFocus={(event) => event.stopPropagation()}
className={`${LIB_NAME}-input`}
size={methods.getInputSize()}
value={state.search}
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ export class Select extends Component {

{(this.props.name || this.props.required) && (
<input
tabIndex={-1}
style={{ opacity: 0, width: 0, position: 'absolute' }}
name={this.props.name}
required={this.props.required}
Expand Down

0 comments on commit 819bb5a

Please sign in to comment.