Skip to content
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
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ when possible and accept [props](https://facebook.github.io/react/docs/component
* If you need a constant file, it should be called `{Component_Name}Constants.js` (Component_Name with PascalCase)
* Each component should treat as a standalone package and live under its own folder
* Single file per component with **default export**
* Avoid using the bindMethods syntax for attaching methods to a class. Instead use class properties for example ```testMethod = () => { return 'test'} ```
* When component is a set of components (e.g., ListGroup and ListGroupItem),
they should live in the same folder named on the parent component (e.g., ListGroup)
* Each component folder should have an `index.js` file with **named exports** of all the relevant components in the folder
Expand Down
29 changes: 9 additions & 20 deletions src/common/controlled.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { nullValues, bindMethods, selectKeys, filterKeys } from './helpers';
import { nullValues, selectKeys, filterKeys } from './helpers';

/*
controlled(stateTypes, defaults)(WrappedComponent)
Expand Down Expand Up @@ -35,16 +35,7 @@ import { nullValues, bindMethods, selectKeys, filterKeys } from './helpers';
*/
const controlled = ({ types, defaults = {}, persist }) => WrappedComponent => {
class ControlledComponent extends React.Component {
constructor() {
super();
this.state = { ...nullValues(types), ...defaults };
bindMethods(this, [
'sessionKey',
'savePersistent',
'loadPersistent',
'setControlledState'
]);
}
state = { ...nullValues(types), ...defaults };

componentDidMount() {
this.loadPersistent();
Expand All @@ -60,21 +51,21 @@ const controlled = ({ types, defaults = {}, persist }) => WrappedComponent => {
window.removeEventListener('beforeunload', this.savePersistent);
}

setControlledState(updater) {
setControlledState = updater => {
this.setState(updater);
}
};

loadPersistent() {
loadPersistent = () => {
if (persist && persist.length > 0) {
const fromPersisted =
window &&
window.sessionStorage &&
window.sessionStorage.getItem(this.sessionKey());
fromPersisted && this.setState(JSON.parse(fromPersisted));
}
}
};

savePersistent() {
savePersistent = () => {
if (persist && persist.length > 0) {
const toPersist = selectKeys(this.state, persist);
window &&
Expand All @@ -84,11 +75,9 @@ const controlled = ({ types, defaults = {}, persist }) => WrappedComponent => {
JSON.stringify(toPersist)
);
}
}
};

sessionKey() {
return this.props.sessionKey || JSON.stringify(persist);
}
sessionKey = () => this.props.sessionKey || JSON.stringify(persist);

render() {
const controlledStateProps = filterKeys(
Expand Down
5 changes: 5 additions & 0 deletions src/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import React from 'react';

/** Equivalent to calling `this.someMethod = this.someMethod.bind(this)` for every method name in the methods array. */
export const bindMethods = (context, methods) => {
// eslint-disable-next-line no-console
console.warn(`
bindMethods usage is deprecated in favor of class methods.
bindMethods will be removed in the next major release
`);
methods.forEach(method => {
context[method] = context[method].bind(context);
});
Expand Down
93 changes: 31 additions & 62 deletions src/components/Filter/__mocks__/mockFilterExample.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Filter, FormControl, Toolbar } from '../../../index';
import { bindMethods } from '../../../common/helpers';

export const mockFilterExampleFields = [
{
Expand Down Expand Up @@ -72,28 +71,13 @@ export const mockFilterExampleFields = [
];

export class MockFilterExample extends React.Component {
constructor() {
super();

bindMethods(this, [
'updateCurrentValue',
'onValueKeyPress',
'selectFilterType',
'filterValueSelected',
'filterCategorySelected',
'categoryValueSelected',
'removeFilter',
'clearFilters'
]);

this.state = {
currentFilterType: mockFilterExampleFields[0],
activeFilters: [],
currentValue: ''
};
}
state = {
currentFilterType: mockFilterExampleFields[0],
activeFilters: [],
currentValue: ''
};

onValueKeyPress(keyEvent) {
onValueKeyPress = keyEvent => {
const { currentValue, currentFilterType } = this.state;

if (keyEvent.key === 'Enter' && currentValue && currentValue.length > 0) {
Expand All @@ -102,9 +86,9 @@ export class MockFilterExample extends React.Component {
keyEvent.stopPropagation();
keyEvent.preventDefault();
}
}
};

categoryValueSelected(value) {
categoryValueSelected = value => {
const { currentValue, currentFilterType, filterCategory } = this.state;

if (filterCategory && currentValue !== value) {
Expand All @@ -117,11 +101,11 @@ export class MockFilterExample extends React.Component {
this.filterAdded(currentFilterType, filterValue);
}
}
}
};

clearFilters() {
clearFilters = () => {
this.setState({ activeFilters: [] });
}
};

filterAdded = (field, value) => {
let filterText = '';
Expand All @@ -145,14 +129,14 @@ export class MockFilterExample extends React.Component {
this.setState({ activeFilters });
};

filterCategorySelected(category) {
filterCategorySelected = category => {
const { filterCategory } = this.state;
if (filterCategory !== category) {
this.setState({ filterCategory: category, currentValue: '' });
}
}
};

filterValueSelected(filterValue) {
filterValueSelected = filterValue => {
const { currentFilterType, currentValue } = this.state;

if (filterValue !== currentValue) {
Expand All @@ -161,9 +145,9 @@ export class MockFilterExample extends React.Component {
this.filterAdded(currentFilterType, filterValue);
}
}
}
};

removeFilter(filter) {
removeFilter = filter => {
const { activeFilters } = this.state;

const index = activeFilters.indexOf(filter);
Expand All @@ -174,9 +158,9 @@ export class MockFilterExample extends React.Component {
];
this.setState({ activeFilters: updated });
}
}
};

selectFilterType(filterType) {
selectFilterType = filterType => {
const { currentFilterType } = this.state;
if (currentFilterType !== filterType) {
this.setState(prevState => ({
Expand All @@ -192,11 +176,11 @@ export class MockFilterExample extends React.Component {
: prevState.categoryValue
}));
}
}
};

updateCurrentValue(event) {
updateCurrentValue = event => {
this.setState({ currentValue: event.target.value });
}
};

renderInput() {
const { currentFilterType, currentValue, filterCategory } = this.state;
Expand Down Expand Up @@ -290,7 +274,6 @@ export class MockFilterExample extends React.Component {
export const mockFilterExampleSource = `
import React from 'react';
import { Filter, FormControl, Toolbar } from '../../../index';
import { bindMethods } from '../../../common/helpers';

export const mockFilterExampleFields = [
{
Expand Down Expand Up @@ -362,26 +345,12 @@ export const mockFilterExampleFields = [
];

export class MockFilterExample extends React.Component {
constructor() {
super();

bindMethods(this, [
'updateCurrentValue',
'onValueKeyPress',
'selectFilterType',
'filterValueSelected',
'filterCategorySelected',
'categoryValueSelected',
'removeFilter',
'clearFilters'
]);

this.state = {

state = {
currentFilterType: mockFilterExampleFields[0],
activeFilters: [],
currentValue: ''
};
}

filterAdded = (field, value) => {
let filterText = '';
Expand All @@ -407,7 +376,7 @@ export class MockFilterExample extends React.Component {
this.setState({ activeFilters: activeFilters });
};

selectFilterType(filterType) {
selectFilterType = filterType => {
const { currentFilterType } = this.state;
if (currentFilterType !== filterType) {
this.setState(prevState => {
Expand All @@ -427,7 +396,7 @@ export class MockFilterExample extends React.Component {
}
}

filterValueSelected(filterValue) {
filterValueSelected = filterValue => {
const { currentFilterType, currentValue } = this.state;

if (filterValue !== currentValue) {
Expand All @@ -438,14 +407,14 @@ export class MockFilterExample extends React.Component {
}
}

filterCategorySelected(category) {
filterCategorySelected = category => {
const { filterCategory } = this.state;
if (filterCategory !== category) {
this.setState({ filterCategory: category, currentValue: '' });
}
}

categoryValueSelected(value) {
categoryValueSelected = value => {
const { currentValue, currentFilterType, filterCategory } = this.state;

if (filterCategory && currentValue !== value) {
Expand All @@ -460,11 +429,11 @@ export class MockFilterExample extends React.Component {
}
}

updateCurrentValue(event) {
updateCurrentValue = event => {
this.setState({ currentValue: event.target.value });
}

onValueKeyPress(keyEvent) {
onValueKeyPress = keyEvent => {
const { currentValue, currentFilterType } = this.state;

if (keyEvent.key === 'Enter' && currentValue && currentValue.length > 0) {
Expand All @@ -475,7 +444,7 @@ export class MockFilterExample extends React.Component {
}
}

removeFilter(filter) {
removeFilter = filter => {
const { activeFilters } = this.state;

let index = activeFilters.indexOf(filter);
Expand All @@ -488,7 +457,7 @@ export class MockFilterExample extends React.Component {
}
}

clearFilters() {
clearFilters = () => {
this.setState({ activeFilters: [] });
}

Expand Down
37 changes: 14 additions & 23 deletions src/components/InfoTip/InfoTip.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Dropdown } from '../Dropdown';
import { bindMethods, KEY_CODES } from '../../common/helpers';
import { KEY_CODES } from '../../common/helpers';

class InfoTip extends React.Component {
constructor(props) {
super(props);
bindMethods(this, [
'handleKeyDown',
'handleClick',
'handleBackFocus',
'handleBlur'
]);
this.state = { open: false, footerFocused: false };
}
state = { open: false, footerFocused: false };

handleEnterKeyDown(event) {
handleEnterKeyDown = event => {
this.setState({ open: !this.state.open });
event.preventDefault();
}
};

handleTabKeyDown(event) {
handleTabKeyDown = event => {
if (this.state.footerFocused) {
this.setState({ open: false, footerFocused: false });
} else {
this.setState({ footerFocused: true });
}
event.stopPropagation();
event.nativeEvent.stopImmediatePropagation();
}
};

handleKeyDown(event) {
handleKeyDown = event => {
if (event.shiftKey && event.keyCode) {
return this.handleBackFocus();
}
Expand All @@ -44,24 +35,24 @@ class InfoTip extends React.Component {
default:
return null;
}
}
};

handleBackFocus() {
handleBackFocus = () => {
if (this.state.open) {
this.setState({ open: false });
}
}
};

handleClick(event) {
handleClick = event => {
event.preventDefault();
this.setState({ open: !this.state.open });
}
handleBlur(event) {
};
handleBlur = event => {
if (event && event.relatedTarget) {
event.relatedTarget.click();
}
this.setState({ open: false });
}
};

render() {
const { children, onToggle, ...props } = this.props;
Expand Down
Loading