Skip to content

Commit

Permalink
Add disabled prop
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchin committed Nov 22, 2015
1 parent 0df907a commit ea39abd
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ ariaLabelledby |string |`aria-labelledby` attribute
classNames |Object.<string> |CSS class names
defaultValue |number |Default value
defaultValues |Object |Default values
disabled |boolean |Disabled or not
maxValue |number |Maximum value it can accept
minValue |number |Minimum value it can accept
name |string |Name of `form` input
Expand Down
7 changes: 7 additions & 0 deletions example/js/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class App extends React.Component {
minValue={0}
defaultValue={defaultValue}
/>

<InputRange
maxValue={20}
minValue={0}
disabled={true}
defaultValue={defaultValue}
/>
</form>
);
}
Expand Down
11 changes: 9 additions & 2 deletions scss/_InputRangeSlider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
width: $InputRange-slider-width;

&:active {
transform: $InputRange-slider-transform--active;
transform: $InputRange-slider--active-transform;
}

&:focus {
box-shadow: 0 0 0 5px transparentize($InputRange-primaryColor, 0.8);
box-shadow: 0 0 0 5px transparentize($InputRange-slider-background, 0.8);
}

.InputRange.is-disabled & {
background: $InputRange-slider--is-disabled-background;
border: $InputRange-slider--is-disabled-border;
box-shadow: none;
transform: none;
}
}

Expand Down
4 changes: 4 additions & 0 deletions scss/_InputRangeTrack.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
height: $InputRange-track-height;
position: relative;
transition: $inputRange-track-transition;

.InputRange.is-disabled & {
background: $InputRange-track--is-disabled-background;
}
}

.InputRange-track--container {
Expand Down
8 changes: 6 additions & 2 deletions scss/_InputRangeVariables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ $InputRange-fontFamily: 'Helvetica Neue', san-serif;
$InputRange-primaryColor: #3f51b5;
$InputRange-neutralColor: #aaaaaa;
$InputRange-neutralLightColor: #eeeeee;
$InputRange-disabledColor: #cccccc;

// InputRange-slider
$InputRange-slider-background: $InputRange-primaryColor !default;
$InputRange-slider-border: 1px solid $InputRange-primaryColor !default;
$InputRange-slider-height: 1rem !default;
$InputRange-slider-width: 1rem !default;
$InputRange-slider-transition: transform 0.3s ease-out, box-shadow 0.3s ease-out;
$InputRange-slider-transform--active: scale(1.3) !default;
$InputRange-sliderContainer-transition: left 0.3s ease-out;
$InputRange-slider--active-transform: scale(1.3) !default;
$InputRange-slider--is-disabled-background: $InputRange-disabledColor !default;
$InputRange-slider--is-disabled-border: 1px solid $InputRange-disabledColor !default;

// InputRange-label
$InputRange-label-color: $InputRange-neutralColor !default;

// InputRange-track
$InputRange-track--active-background: $InputRange-primaryColor !default;
$InputRange-track-background: $InputRange-neutralLightColor !default;
$InputRange-track-height: 0.3rem !default;
$inputRange-track-transition: left 0.3s ease-out, width 0.3s ease-out;
$InputRange-track--active-background: $InputRange-primaryColor !default;
$InputRange-track--is-disabled-background: $InputRange-neutralLightColor !default;
24 changes: 23 additions & 1 deletion src/InputRange/InputRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,20 @@ class InputRange extends React.Component {

// Handlers
handleSliderMouseMove(slider, event) {
if (this.props.disabled) {
return;
}

const position = this.valueTransformer.positionFromEvent(event);

this.setPosition(slider, position);
}

handleSliderKeyDown(slider, event) {
if (this.props.disabled) {
return;
}

switch (event.keyCode) {
case KeyCode.LEFT_ARROW:
this.decrementValue(slider);
Expand All @@ -254,6 +262,10 @@ class InputRange extends React.Component {
}

handleTrackMouseDown(track, position) {
if (this.props.disabled) {
return;
}

this.setPosition(null, position);
}

Expand Down Expand Up @@ -313,9 +325,17 @@ class InputRange extends React.Component {

render() {
const classNames = this.props.classNames;
let componentClassName = classNames.component;

if (this.props.disabled) {
componentClassName = `${componentClassName} is-disabled`;
}

return (
<div ref="inputRange" className={ classNames.component }>
<div
aria-disabled={ this.props.disabled }
ref="inputRange"
className={ componentClassName }>
<span className={ classNames.labelMin }>
<span className={ classNames.labelContainer }>
{ this.props.minValue }
Expand Down Expand Up @@ -348,6 +368,7 @@ InputRange.propTypes = {
classNames: React.PropTypes.objectOf(React.PropTypes.string),
defaultValue: maxMinValuePropType,
defaultValues: maxMinValuePropType,
disabled: React.PropTypes.bool,
maxValue: maxMinValuePropType,
minValue: maxMinValuePropType,
name: React.PropTypes.string,
Expand All @@ -359,6 +380,7 @@ InputRange.propTypes = {

InputRange.defaultProps = {
classNames: defaultClassNames,
disabled: false,
minValue: 0,
maxValue: 10,
step: 1,
Expand Down
70 changes: 54 additions & 16 deletions test/InputRange.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('InputRange', () => {
it('should set the initial position for slider', () => {
const props = {
classNames: jasmine.any(Object),
disabled: false,
maxValue: 20,
minValue: 0,
values: {
Expand Down Expand Up @@ -551,74 +552,111 @@ describe('InputRange', () => {
});

describe('handleSliderMouseMove', () => {
let slider;
let event;

beforeEach(() => {
spyOn(inputRange, 'setPosition');
});

it('should set the position of a slider according to mouse event', () => {
const slider = inputRange.refs.sliderMax;
const event = {
slider = inputRange.refs.sliderMax;
event = {
clientX: 100,
clientY: 200,
};
});

it('should set the position of a slider according to mouse event', () => {
inputRange.handleSliderMouseMove(slider, event);

expect(inputRange.setPosition).toHaveBeenCalledWith(slider, { x: 92, y: 0 });
});

it('should not set the position of a slider if disabled', () => {
inputRange.props.disabled = true;
inputRange.handleSliderMouseMove(slider, event);

expect(inputRange.setPosition).not.toHaveBeenCalled();
});
});

describe('handleSliderKeyDown', () => {
let slider;
let event;

describe('when pressing left arrow key', () => {
beforeEach(() => {
spyOn(inputRange, 'decrementValue');
});

it('should decrement value', () => {
const slider = inputRange.refs.sliderMax;
const event = {
slider = inputRange.refs.sliderMax;
event = {
keyCode: 37,
};
});

it('should decrement value', () => {
inputRange.handleSliderKeyDown(slider, event);

expect(inputRange.decrementValue).toHaveBeenCalledWith(slider);
});

it('should not decrement value if disabled', () => {
inputRange.props.disabled = true;
inputRange.handleSliderKeyDown(slider, event);

expect(inputRange.decrementValue).not.toHaveBeenCalled();
});
});

describe('when pressing right arrow key', () => {
beforeEach(() => {
spyOn(inputRange, 'incrementValue');
});

it('should increment value', () => {
const slider = inputRange.refs.sliderMax;
const event = {
slider = inputRange.refs.sliderMax;
event = {
keyCode: 39,
};
});

it('should increment value', () => {
inputRange.handleSliderKeyDown(slider, event);

expect(inputRange.incrementValue).toHaveBeenCalledWith(slider);
});

it('should not increment value if disabled', () => {
inputRange.props.disabled = true;
inputRange.handleSliderKeyDown(slider, event);

expect(inputRange.incrementValue).not.toHaveBeenCalled();
});
});
});

describe('handleTrackMouseDown', () => {
let track;
let position;

beforeEach(() => {
spyOn(inputRange, 'setPosition');
});

it('should set a new position based on the position of mouse click', () => {
const track = {};
const position = {
track = {};
position = {
x: 100,
y: 0,
};
});

it('should set a new position based on the position of mouse click', () => {
inputRange.handleTrackMouseDown(track, position);

expect(inputRange.setPosition).toHaveBeenCalledWith(null, position);
});

it('should not set a new position if disabled', () => {
inputRange.props.disabled = true;
inputRange.handleTrackMouseDown(track, position);

expect(inputRange.setPosition).not.toHaveBeenCalled();
});
});
});

0 comments on commit ea39abd

Please sign in to comment.