Skip to content

Commit

Permalink
Merge pull request #88 from sflpro/feature-slider-input-disabled-func…
Browse files Browse the repository at this point in the history
…tionality

fix: Add disabled functionality for slider input
  • Loading branch information
edgar-poghosyan-sfl authored Aug 6, 2020
2 parents ae7b890 + cae4b01 commit 714c4ba
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 32 deletions.
18 changes: 11 additions & 7 deletions src/Control/control.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.control {
@mixin font-scale-6;
transition: left var(--slider-animation-duration) ease-out;
color: var(--slider-control-color);
top: var(--slider-control-top);
position: absolute;
cursor: pointer;
z-index: var(--slider-control-z-index);
@mixin font-scale-6;
transition: left var(--slider-animation-duration) ease-out;
color: var(--slider-control-color);
top: var(--slider-control-top);
position: absolute;
cursor: pointer;
z-index: var(--slider-control-z-index);
}

.disabled {
color: var(--slider-control-disabled-color);
}
22 changes: 15 additions & 7 deletions src/Control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ export class Control extends React.Component {
};

onMouseDown = (event) => {
const { onDragStart, name } = this.props;
const { onDragStart, name, disabled } = this.props;
if (disabled) {
return;
}

const { currentStep, left } = this.state;

this.addListeners();
Expand All @@ -112,8 +116,10 @@ export class Control extends React.Component {

onMouseUp = (event) => {
const { currentStep, left } = this.state;
const { onDragEnd, name } = this.props;

const { onDragEnd, name, disabled } = this.props;
if (disabled) {
return;
}
this.removeListeners();

if (onDragEnd && typeof onDragEnd === 'function') {
Expand Down Expand Up @@ -172,18 +178,17 @@ export class Control extends React.Component {
style,
min,
max,
disabled,
...props
} = this.props;
const { left } = this.state;

const controlClassNames = {
[styles.control]: true,
[styles.disabled]: disabled,
[className]: className,
};

if (className) {
controlClassNames[className] = true;
}

const sliderStyles = {
...style,
left: `${left}px`,
Expand Down Expand Up @@ -225,6 +230,8 @@ Control.propTypes = {
min: PropTypes.number,
/** Number, max value of control if not passed will get from Slider Component */
max: PropTypes.number,
/** Boolean, whether control is disabled */
disabled: PropTypes.bool,
/** String, classname that will be passed to wrapper span element */
className: PropTypes.string,
/** Object, style that will be added to wrapper span element */
Expand All @@ -236,6 +243,7 @@ Control.defaultProps = {
onDragEnd: undefined,
min: undefined,
max: undefined,
disabled: false,
className: '',
style: undefined,
};
6 changes: 6 additions & 0 deletions src/InputSlider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class InputSlider extends React.Component {
input: InputComponent,
inputProps,
separator,
disabled,
className,
children,
style,
Expand All @@ -218,11 +219,13 @@ export class InputSlider extends React.Component {
onKeyPress={this.onKeyPress}
onChange={this.onChange}
onBlur={this.onBlur}
disabled={disabled}
value={value}
/>
<Slider
{...props}
onControlChange={this.setMinMax}
disabled={disabled}
className={styles.inputSlider}
>
{children}
Expand All @@ -239,6 +242,8 @@ InputSlider.propTypes = {
inputProps: PropTypes.object,
/** String, character that will separate range input values */
separator: PropTypes.string,
/** Boolean, whether slider input is disabled */
disabled: PropTypes.bool,
/** String, className that will be added to wrapper div */
className: PropTypes.string,
/** Number, min difference of range input values */
Expand All @@ -262,6 +267,7 @@ InputSlider.propTypes = {
InputSlider.defaultProps = {
input: TextInput,
hasError: false,
disabled: false,
separator: '-',
inputProps: {},
className: '',
Expand Down
3 changes: 2 additions & 1 deletion src/InputSlider/inputSlider.story.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ storiesOf('Form controls/Input Slider', module)
<InputSlider
onChange={handler}
step={5}
disabled
>
<Control name='test3' value={state.test3} icon={<Icon name='cross' />} />
<Control name='test3' value={state.test3} icon={<Icon name='cross' />} disabled />
</InputSlider>
</Item>
</ItemRow>
Expand Down
11 changes: 10 additions & 1 deletion src/Slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export class Slider extends React.Component {
event.preventDefault();
}

const { onChange, step, min, max } = this.props;
const { onChange, step, min, max, disabled } = this.props;
if (disabled) {
return;
}

const { maxStepsCount, controls } = this.state;

const [minControlName, maxControlName] = Object.keys(controls);
Expand Down Expand Up @@ -174,6 +178,7 @@ export class Slider extends React.Component {
min,
max,
distance,
disabled,
...props
} = this.props;

Expand All @@ -186,6 +191,7 @@ export class Slider extends React.Component {

const sliderClassName = classNames({
[styles.slider]: true,
[styles.disabled]: disabled,
[className]: true,
});

Expand Down Expand Up @@ -234,6 +240,8 @@ Slider.propTypes = {
distance: PropTypes.number,
/** Number, amount of change per step */
step: PropTypes.number,
/** Boolean, whether slider is disabled */
disabled: PropTypes.bool,
/** Number, min value of input */
min: PropTypes.number,
/** Number, max value of input */
Expand All @@ -250,6 +258,7 @@ Slider.defaultProps = {
onControlChange: undefined,
distance: 1,
step: 1,
disabled: false,
min: 0,
max: 100,
className: '',
Expand Down
40 changes: 24 additions & 16 deletions src/Slider/slider.css
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
.wrapper {
display: flex;
align-items: center;
min-width: var(--slider-wrapper-min-width);
display: flex;
align-items: center;
min-width: var(--slider-wrapper-min-width);
}

.slider {
background-color: var(--slider-background-color);
display: inline-block;
position: relative;
border-radius: var(--slider-border-radius);
cursor: pointer;
margin: var(--slider-margin);
width: var(--slider-width);
height: var(--slider-height);
background-color: var(--slider-background-color);
display: inline-block;
position: relative;
border-radius: var(--slider-border-radius);
cursor: pointer;
margin: var(--slider-margin);
width: var(--slider-width);
height: var(--slider-height);
}

.disabled {
cursor: default;
}

.sliderLine {
position: absolute;
height: var(--slider-line-height);
border-radius: var(--slider-line-border-radius);
background-color: var(--slider-line-color);
transition: left var(--slider-line-animation-duration) ease-out, width var(--slider-line-animation-duration) ease-out;
position: absolute;
height: var(--slider-line-height);
border-radius: var(--slider-line-border-radius);
background-color: var(--slider-line-color);
transition: left var(--slider-line-animation-duration) ease-out, width var(--slider-line-animation-duration) ease-out;
}

.disabled .sliderLine {
background-color: var(--slider-background-disabled-color);
}
2 changes: 2 additions & 0 deletions src/defaults.css
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@

--slider-animation-duration: 0.1s;
--slider-background-color: var(--default-color);
--slider-background-disabled-color: var(--disabled-color);
--slider-line-color: var(--primary-color);
--slider-height: 4px;
--slider-width: 100%;
Expand All @@ -329,6 +330,7 @@
--slider-line-animation-duration: var(--slider-animation-duration);
--slider-control-border: 0 0 2px var(--neutral-1000-opacity-30);
--slider-control-color: var(--primary-color);
--slider-control-disabled-color: var(--disabled-color);
--slider-control-size: 16px;
--slider-control-top: 4px;
--slider-wrapper-min-width: 200px;
Expand Down

0 comments on commit 714c4ba

Please sign in to comment.