forked from hypeserver/react-date-range
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'input-ranges' of https://github.com/vladaspasic/react-d…
…ate-range into test-branch
- Loading branch information
Showing
6 changed files
with
5,601 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
const MIN = 0; | ||
const MAX = 99999; | ||
|
||
class InputRangeField extends Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.onChange = this.onChange.bind(this); | ||
} | ||
|
||
shouldComponentUpdate(nextProps) { | ||
const { value, label, placeholder } = this.props; | ||
|
||
return ( | ||
value !== nextProps.value || | ||
label !== nextProps.label || | ||
placeholder !== nextProps.placeholder | ||
); | ||
} | ||
|
||
onChange(e) { | ||
const { onChange } = this.props; | ||
|
||
let value = parseInt(e.target.value, 10); | ||
value = isNaN(value) ? 0 : Math.max(Math.min(MAX, value), MIN); | ||
|
||
onChange(value); | ||
} | ||
|
||
render() { | ||
const { label, placeholder, value, styles, onBlur, onFocus } = this.props; | ||
|
||
return ( | ||
<div className={styles.inputRange}> | ||
<input | ||
className={styles.inputRangeInput} | ||
placeholder={placeholder} | ||
value={value} | ||
min={MIN} | ||
max={MAX} | ||
onChange={this.onChange} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
/> | ||
<span className={styles.inputRangeLabel}>{label}</span> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
InputRangeField.propTypes = { | ||
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
label: PropTypes.string.isRequired, | ||
placeholder: PropTypes.string, | ||
styles: PropTypes.shape({ | ||
inputRange: PropTypes.string, | ||
inputRangeInput: PropTypes.string, | ||
inputRangeLabel: PropTypes.string, | ||
}).isRequired, | ||
onBlur: PropTypes.func.isRequired, | ||
onFocus: PropTypes.func.isRequired, | ||
onChange: PropTypes.func.isRequired, | ||
}; | ||
|
||
InputRangeField.defaultProps = { | ||
value: '', | ||
placeholder: '-', | ||
}; | ||
|
||
export default InputRangeField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import InputRangeField from './InputRangeField'; | ||
|
||
const styles = { | ||
inputRange: 'range', | ||
inputRangeInput: 'input', | ||
inputRangeLabel: 'label', | ||
}; | ||
const toChangeEvent = value => ({ target: { value } }); | ||
|
||
describe('InputRangeField tests', () => { | ||
test('Should parse input value to number', () => { | ||
const onChange = jest.fn(); | ||
const wrapper = mount( | ||
<InputRangeField | ||
label="Input label" | ||
styles={styles} | ||
onChange={onChange} | ||
onFocus={jest.fn()} | ||
onBlur={jest.fn()} | ||
/> | ||
); | ||
|
||
wrapper.find('input').simulate('change', toChangeEvent('3')); | ||
expect(onChange).lastCalledWith(3); | ||
wrapper.find('input').simulate('change', toChangeEvent(12)); | ||
expect(onChange).lastCalledWith(12); | ||
wrapper.find('input').simulate('change', toChangeEvent('')); | ||
expect(onChange).lastCalledWith(0); | ||
wrapper.find('input').simulate('change', toChangeEvent('invalid number')); | ||
expect(onChange).lastCalledWith(0); | ||
wrapper.find('input').simulate('change', toChangeEvent(-12)); | ||
expect(onChange).lastCalledWith(0); | ||
wrapper.find('input').simulate('change', toChangeEvent(99999999)); | ||
expect(onChange).lastCalledWith(99999); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(6); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
|
||
test('Should rerender when props change', () => { | ||
const wrapper = mount( | ||
<InputRangeField | ||
value={12} | ||
placeholder="Placeholder" | ||
label="Input label" | ||
styles={styles} | ||
onChange={jest.fn()} | ||
onFocus={jest.fn()} | ||
onBlur={jest.fn()} | ||
/> | ||
); | ||
|
||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('value')).toEqual(12); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('placeholder')).toEqual('Placeholder'); | ||
expect(wrapper.find(`.${styles.inputRangeLabel}`).text()).toEqual('Input label'); | ||
|
||
wrapper.setProps({ value: '32' }); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('value')).toEqual('32'); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('placeholder')).toEqual('Placeholder'); | ||
expect(wrapper.find(`.${styles.inputRangeLabel}`).text()).toEqual('Input label'); | ||
|
||
wrapper.setProps({ placeholder: '-' }); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('value')).toEqual('32'); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('placeholder')).toEqual('-'); | ||
expect(wrapper.find(`.${styles.inputRangeLabel}`).text()).toEqual('Input label'); | ||
|
||
wrapper.setProps({ label: 'Label' }); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('value')).toEqual('32'); | ||
expect(wrapper.find(`.${styles.inputRangeInput}`).prop('placeholder')).toEqual('-'); | ||
expect(wrapper.find(`.${styles.inputRangeLabel}`).text()).toEqual('Label'); | ||
}); | ||
}); |
Oops, something went wrong.