Skip to content

Commit

Permalink
Merge pull request #18 from 5rabbits/release/0.5.0
Browse files Browse the repository at this point in the history
Release/0.5.0
  • Loading branch information
Vsanchezr authored Aug 10, 2017
2 parents 2095335 + 0c64bc0 commit a7ad46a
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 5 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
cache: yarn
node_js:
- "6"
- "node"
notifications:
email: false
install:
- yarn
- yarn global add codecov
script:
- yarn run lint
- yarn test
after_success:
- codecov
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "portrait",
"version": "0.5.0-beta.4",
"version": "0.5.0",
"description": "Base design for all 5Rabbits products",
"keywords": [
"commonjs",
Expand Down Expand Up @@ -36,7 +36,7 @@
"dev": "NODE_ENV=development webpack-dev-server --config webpack/webpack.config.js --hot --inline --content-base lib/docs",
"lint": "eslint --color --ext .js src docs",
"prepublish": "npm run build",
"test": "NODE_ENV=test jest",
"test": "NODE_ENV=test jest --ci",
"test-watch": "NODE_ENV=test jest --watch",
"preversion": "npm run lint && SINGLE_RUN=true npm test"
},
Expand Down Expand Up @@ -118,7 +118,8 @@
"coverageDirectory": "coverage",
"coverageReporters": [
"html",
"text-summary"
"text-summary",
"lcov"
]
}
}
42 changes: 42 additions & 0 deletions src/components/controls/SearchInput/SearchInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ describe('SearchInput', () => {
expect(toJSON(component)).toMatchSnapshot()
})

describe('componentWillReceiveProps', () => {
it('should update state with new values', () => {
const component = mount(<SearchInput value="test" />)
const instance = component.instance()

component.setProps({
value: 'search',
})

expect(instance.state.value).toBe('search')
})

it('should not update state when props have the same value', () => {
const component = mount(<SearchInput value="test" />)
const instance = component.instance()

component.setProps({
value: 'test',
})

expect(instance.state.value).toBe('test')
})
})

describe('setValue(value)', () => {
describe('if props.onChange is defined', () => {
it('should call props.onChange passing the new value', () => {
Expand All @@ -32,6 +56,24 @@ describe('SearchInput', () => {
}).not.toThrow()
})
})

describe('if props.value is != ""', () => {
it('should not call to onChange', () => {
const onClick = jest.fn()
const component = mount(
<SearchInput
value="test"
onChange={onClick}
/>,
)
const instance = component.instance()

instance.setValue('aa')

expect(onClick).toHaveBeenCalled()
expect(instance.state.value).toBe('test')
})
})
})

describe('focus()', () => {
Expand Down
27 changes: 25 additions & 2 deletions src/components/controls/SearchInput/SearchInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export default class SearchInput extends Component {
*/
className: PropTypes.string,

/**
* Initial search value for an uncontrolled input.
*/
defaultValue: PropTypes.string,

/**
* Invoked when the input text changes, either by writing or by clicking the clear button.
* The first argument is the current text.
Expand All @@ -35,6 +40,12 @@ export default class SearchInput extends Component {
* `alternate` for popovers.
*/
theme: PropTypes.oneOf(['default', 'alternate']).isRequired,

/**
* Controlled value for the input. Use the `onChange` callback to update
* the value from the parent component.
*/
value: PropTypes.string,
}

static defaultProps = {
Expand All @@ -43,14 +54,26 @@ export default class SearchInput extends Component {
onClear: null,
onSubmit: null,
theme: 'default',
defaultValue: '',
value: undefined,
}

state = {
value: '',
value: this.props.value === undefined
? this.props.defaultValue
: this.props.value,
}

componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value) {
this.setState({ value: nextProps.value })
}
}

setValue = value => {
this.setState({ value })
if (this.props.value === undefined) {
this.setState({ value })
}

if (this.props.onChange) {
this.props.onChange(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`SearchInput should render with minimum props 1`] = `
<SearchInput
className={null}
defaultValue=""
onChange={null}
onClear={null}
onSubmit={null}
Expand All @@ -16,6 +17,7 @@ exports[`SearchInput should render with minimum props 1`] = `
/>
<input
className="form-control SearchInput__input"
defaultValue=""
onChange={[Function]}
onKeyDown={[Function]}
type="text"
Expand Down
42 changes: 42 additions & 0 deletions src/components/controls/SearchInput/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,45 @@
theme="alternate"
/>
</div>
-
description: With default value.
code: >
<div style={{ padding: 16, background: '#fff' }}>
<SearchInput
placeholder="Write to search"
style={{ width: '400px' }}
defaultValue="Some default value"
/>
</div>
-
description: Controlled input.
inline: false
code: >
class Example extends React.Component {
state = {
search: '',
}
handleSearchChange = search => {
this.setState({ search })
}
render() {
return (
<div style={{ padding: 16, background: '#fff' }}>
<SearchInput
placeholder="Write to search"
onChange={this.handleSearchChange}
style={{ width: '400px' }}
value={this.state.search}
/>
<br />
The current search is: {this.state.search}
</div>
)
}
}
ReactDOM.render(<Example />, mountNode)

0 comments on commit a7ad46a

Please sign in to comment.