Skip to content

Commit

Permalink
FIX (renderComponents): use props as object and not as args
Browse files Browse the repository at this point in the history
  • Loading branch information
sanusart committed Feb 13, 2019
1 parent 94f559b commit e292262
Show file tree
Hide file tree
Showing 24 changed files with 42 additions and 48 deletions.
4 changes: 2 additions & 2 deletions __tests__/components/Option.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ describe('<Option/> component', () => {

it('renders correctly', () => {
const tree = TestRenderer.create(
<Option {...props({ parentItem: options[0] })}/>
<Option {...props({ item: options[0] })}/>
).toJSON();

expect(tree).toMatchSnapshot();
});

it('onClick remove item', () => {
TestRenderer.create(
<Option {...props({ parentItem: options[0] })} onClick={spy}/>)
<Option {...props({ item: options[0] })} onClick={spy}/>)
.root.findByProps({ className: 'react-dropdown-select-option-remove' }).props.onClick();

expect(spy).toHaveBeenCalled;
Expand Down
6 changes: 3 additions & 3 deletions docs/src/examples/CustomContentAndDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export class CustomContentAndDropdown extends React.Component {
value: user.email
}));

customContentRenderer = (props, state) => (
customContentRenderer = ({ props, state }) => (
<div style={{ cursor: 'pointer' }}>
{state.values.length} of {props.options.length} selected
</div>
);

customDropdownRenderer = (props, state, methods) => {
customDropdownRenderer = ({ props, state, methods }) => {
const regexp = new RegExp(state.search, 'i');

return (
Expand Down Expand Up @@ -54,7 +54,7 @@ export class CustomContentAndDropdown extends React.Component {
onClick={option.disabled ? null : () => methods.addItem(option)}>
<input
type="checkbox"
onChange={() => option.disabled ? undefined : methods.addItem(option)}
onChange={() => (option.disabled ? undefined : methods.addItem(option))}
checked={state.values.indexOf(option) !== -1}
/>
<ItemLabel>{option[props.labelField]}</ItemLabel>
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/ItemRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ItemRenderer = ({ options, title }) => (
multi
options={options}
values={[]}
itemRenderer={(item, itemIndex, props, state, methods) => (
itemRenderer={({ item, itemIndex, props, state, methods }) => (
<StyledItem>
{item.disabled ? (
<div aria-disabled>{item.label}</div>
Expand Down
12 changes: 6 additions & 6 deletions docs/src/pages/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export class Demo extends React.Component {

setValues = (selectValues) => this.setState({ selectValues });

contentRenderer = (innerProps, innerState) => {
contentRenderer = ({props, state}) => {
return (
<div>
{innerState.values.length} of {innerProps.options.length} Selected
{state.values.length} of {props.options.length} Selected
</div>
);
};
Expand All @@ -59,7 +59,7 @@ export class Demo extends React.Component {
);
};

itemRenderer = (item, itemIndex, props, state, methods) => (
itemRenderer = ({item, itemIndex, props, state, methods}) => (
<div key={item[props.valueField]} onClick={() => methods.addItem(item)}>
<div style={{ margin: '10px' }}>
<input type="checkbox" checked={methods.isSelected(item)} />
Expand All @@ -68,7 +68,7 @@ export class Demo extends React.Component {
</div>
);

dropdownRenderer = (props, state, methods) => {
dropdownRenderer = ({props, state, methods}) => {
const regexp = new RegExp(state.search, 'i');

return (
Expand Down Expand Up @@ -118,13 +118,13 @@ export class Demo extends React.Component {
);
};

optionRenderer = (option, props, state, methods) => (
optionRenderer = ({option, props, state, methods}) => (
<React.Fragment>
<div onClick={(event) => methods.removeItem(event, option, true)}>{option.label}</div>
</React.Fragment>
);

inputRenderer = (props, state, methods) => (
inputRenderer = ({props, state, methods}) => (
<input
tabIndex="1"
className="react-dropdown-select-input"
Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/demos/clear-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default clear all button

```
clearRenderer={
(parentProps, parentState, parentMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/content-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default element content

```
contentRenderer={
(innerProps, innerState, innerMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/dropdown-hanle-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default handle for opening dropdown

```
dropdownHandleRenderer={
(parentProps, parentState, parentMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/dropdown-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default dropdown

```jsx
dropdownRenderer={
(parentProps, parentState, parentMethods) => func(parentProps, parentState, parentMethods)
({ props, state, methods }) => func(parentProps, parentState, parentMethods)
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/input-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default "input" component, the search in the content

```
inputRenderer={
(parentProps, parentState, parentMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/item-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default list item component in a dropdown

```
itemRenderer={
(item, itemIndex, innerProps, innerState, innerMethods) => <component/>
({ item, itemIndex, props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/loading-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default loading indicator

```
loadingRenderer={
(innerProps) => <component/>
({ props }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/no-data-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default "no data" content

```
noDataRenderer={
(innerProps, innerState, innerMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/option-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default "option" component (the pillow with label and &times;) in the c

```
optionRenderer={
(innerItem, innerProps, innerState, innerMethods) => <component/>
({ item, props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion docs/src/pages/demos/separator-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Override default separator

```
separatorRenderer={
(parentProps, parentState, parentMethods) => <component/>
({ props, state, methods }) => <component/>
}
```
2 changes: 1 addition & 1 deletion src/components/Clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from '@emotion/styled';

const Clear = ({ props, state, methods }) =>
props.clearRenderer ? (
props.clearRenderer(props, state, methods)
props.clearRenderer({ props, state, methods })
) : (
<ClearComponent
className="react-dropdown-select-clear"
Expand Down
14 changes: 4 additions & 10 deletions src/components/Content.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,23 @@ const Content = ({ props, state, methods }) => {
}`}
onClick={() => methods.dropDown('open')}>
{props.contentRenderer ? (
props.contentRenderer(props, state, methods)
props.contentRenderer({ props, state, methods })
) : (
<React.Fragment>
{props.multi
? state.values &&
state.values.map((item) => (
<Option
key={`${item[props.valueField]}${item[props.labelField]}`}
parentItem={item}
item={item}
state={state}
props={props}
methods={methods}
/>
))
: state.values &&
state.values.length > 0 && (
<span>{state.values[0][props.labelField]}</span>
)}
<Input
props={props}
methods={methods}
state={state}
/>
state.values.length > 0 && <span>{state.values[0][props.labelField]}</span>}
<Input props={props} methods={methods} state={state} />
</React.Fragment>
)}
</ContentComponent>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Dropdown = ({ props, state, methods }) => (
methods
)}`}>
{props.dropdownRenderer ? (
props.dropdownRenderer(props, state, methods)
props.dropdownRenderer({ props, state, methods })
) : (
<React.Fragment>
{props.create && state.search && !valueExistInSelected(state.search, state.values) && (
Expand Down
2 changes: 1 addition & 1 deletion src/components/DropdownHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from '@emotion/styled';

const DropdownHandle = ({ props, state, methods }) =>
props.dropdownHandleRenderer ? (
props.dropdownHandleRenderer(props, state, methods)
props.dropdownHandleRenderer({ props, state, methods })
) : (
<DropdownHandleComponent
tabIndex="-1"
Expand Down
2 changes: 1 addition & 1 deletion src/components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Input = ({ props, state, methods }) => {
methods.createNew(state.search);

return props.inputRenderer ? (
props.inputRenderer(props, state, methods)
props.inputRenderer({ props, state, methods })
) : (
<InputComponent
tabIndex="-1"
Expand Down
6 changes: 4 additions & 2 deletions src/components/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hexToRGBA } from '../util';

const Item = ({ props, state, methods, item, itemIndex }) => {
if (props.itemRenderer) {
return props.itemRenderer(item, itemIndex, props, state, methods);
return props.itemRenderer({ item, itemIndex, props, state, methods });
}

if (!props.keepSelectedInList && methods.isSelected(item)) {
Expand All @@ -22,7 +22,9 @@ const Item = ({ props, state, methods, item, itemIndex }) => {
tabIndex="-1"
className={`react-dropdown-select-item ${
methods.isSelected(item) ? 'react-dropdown-select-item-selected' : ''
} ${state.cursor === itemIndex ? 'react-dropdown-select-item-active' : ''} ${item.disabled ? 'react-dropdown-select-item-disabled' : ''}`}
} ${state.cursor === itemIndex ? 'react-dropdown-select-item-active' : ''} ${
item.disabled ? 'react-dropdown-select-item-disabled' : ''
}`}
onClick={item.disabled ? undefined : () => methods.addItem(item)}
onKeyPress={item.disabled ? undefined : () => methods.addItem(item)}
color={props.color}>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from '@emotion/styled';

const Loading = ({ props }) =>
props.loadingRenderer ? (
props.loadingRenderer()
props.loadingRenderer({ props })
) : (
<LoadingComponent className="react-dropdown-select-loading" color={props.color} />
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/NoData.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from '@emotion/styled';

const NoData = ({ props, state, methods }) =>
props.noDataRenderer ? (
props.noDataRenderer(props, state, methods)
props.noDataRenderer({ props, state, methods })
) : (
<NoDataComponent color={props.color}>{props.noDataLabel}</NoDataComponent>
);
Expand Down
12 changes: 5 additions & 7 deletions src/components/Option.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import React from 'react';
import styled from '@emotion/styled';

const Option = ({ parentItem, props, state, methods }) =>
parentItem && props.optionRenderer ? (
props.optionRenderer(parentItem, props, state, methods)
const Option = ({ item, props, state, methods }) =>
item && props.optionRenderer ? (
props.optionRenderer({ item, props, state, methods })
) : (
<OptionComponent
role="listitem"
disabled={props.disabled}
className="react-dropdown-select-option"
color={props.color}>
<span className="react-dropdown-select-option-label">
{parentItem[props.labelField]}
</span>
<span className="react-dropdown-select-option-label">{item[props.labelField]}</span>
<span
className="react-dropdown-select-option-remove"
onClick={(event) => methods.removeItem(event, parentItem, props.closeOnSelect)}>
onClick={(event) => methods.removeItem(event, item, props.closeOnSelect)}>
&times;
</span>
</OptionComponent>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Separator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import styled from '@emotion/styled';

const Separator = ({ props, state, methods }) =>
props.separatorRenderer ? (
props.separatorRenderer(props, state, methods)
props.separatorRenderer({ props, state, methods })
) : (
<SeparatorComponent className="react-dropdown-select-separator" />
);
Expand Down

0 comments on commit e292262

Please sign in to comment.