Skip to content

Commit 232fee6

Browse files
committed
Merge branch '0.3.0'
2 parents 802ad3b + 22b6596 commit 232fee6

14 files changed

+312
-187
lines changed

.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"es2021": true,
55
"node": true
66
},
7+
"globals": {
8+
"JSX": true
9+
},
710
"extends": [
811
"standard",
912
"standard-jsdoc/ts",

.storybook/preview.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export const parameters = {
77
}
88
},
99
backgrounds: {
10-
default: 'dark'
10+
default: 'light'
1111
}
1212
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
A modern single a multi React Slider to use with [Semantic-UI][semantic-ui] or [Fomantic-UI][fomantic-ui].
66

7+
**IMPORTANT**: although the library mentions Semantic UI/Fomantic UI. It is not necessary to install this dependency. This library contains the needed styles to fit as well as possible with that framework, but you can use it in any desired environment.
8+
79

810
![Slider example with multiple colors](./assets/colors.png)
911

assets/colors.png

-6.29 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "neo-react-semantic-ui-range",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "A modern Semantic/Fomantic UI Slider component for React",
55
"scripts": {
66
"build": "del 'dist/*' && npx tsc",

src/components/MultiRangeSlider.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Components
2+
import { SliderTracks } from './SliderTracks'
3+
14
// Utils
25
import { decimalCount } from '../utils/utils'
36

@@ -35,7 +38,9 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
3538
onChange,
3639
margin = 0,
3740
showLabels = false,
38-
color = 'Green'
41+
disabled = false,
42+
inverted = false,
43+
color = 'green'
3944
} = props
4045

4146
/**
@@ -79,34 +84,43 @@ const MultiRangeSlider = function (props: MultiRangeSliderProps) {
7984
const maxPercent = getPercent(maxValue)
8085
const left = `${minPercent}%`
8186
const width = `${maxPercent - minPercent}%`
87+
const disabledClass = disabled ? 'disabled' : ''
88+
8289
return (
8390
<>
8491
<input
8592
type="range"
8693
min={defaultMinValue}
8794
max={defaultMaxValue}
8895
value={minValue}
96+
disabled={disabled}
8997
onChange={(event) => {
9098
handleChange(parseFloat(event.target.value), maxValue)
9199
}}
92-
className="thumb thumb--left"
100+
className={`thumb thumb--left ${disabledClass}`}
93101
/>
94102
<input
95103
type="range"
96104
step={step}
97105
min={defaultMinValue}
98106
max={defaultMaxValue}
99107
value={maxValue}
108+
disabled={disabled}
100109
onChange={(event) => {
101110
handleChange(minValue, parseFloat(event.target.value))
102111
}}
103-
className="thumb thumb--right"
112+
className={`thumb thumb--right ${disabledClass}`}
104113
/>
105-
<div className="slider">
106-
<div className="slider__track" />
114+
115+
<SliderTracks
116+
left={left}
117+
width={width}
118+
color={color}
119+
disabled={disabled}
120+
inverted={inverted}
121+
>
107122
{renderLabels()}
108-
<div style={{ left: left, width: width, backgroundColor: color }} className={'slider__range'} />
109-
</div>
123+
</SliderTracks>
110124
</>
111125
)
112126
}

src/components/SingleRangeSlider.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Components
2+
import { SliderTracks } from './SliderTracks'
3+
14
// Utils
25
import { decimalCount } from '../utils/utils'
36

@@ -22,7 +25,17 @@ interface SingleRangeSliderProps extends SliderProps {
2225
* @returns Component.
2326
*/
2427
const SingleRangeSlider = function (props: SingleRangeSliderProps) {
25-
const { defaultMinValue, defaultMaxValue, value, color = 'Green', showLabels = false, step = 1, onChange } = props
28+
const {
29+
defaultMinValue,
30+
defaultMaxValue,
31+
value,
32+
color = 'green',
33+
showLabels = false,
34+
step = 1,
35+
disabled = false,
36+
inverted = false,
37+
onChange
38+
} = props
2639

2740
/**
2841
* Invokes callback function if slider value is updated.
@@ -55,6 +68,7 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
5568
}
5669

5770
const selectedPercent = `${getSelectedSliderPercent(value)}%`
71+
const disabledClass = disabled ? 'disabled' : ''
5872

5973
return (
6074
<>
@@ -64,16 +78,16 @@ const SingleRangeSlider = function (props: SingleRangeSliderProps) {
6478
max={defaultMaxValue}
6579
value={value}
6680
step={step}
81+
disabled={disabled}
6782
onChange={(event) => {
6883
handleValueChange(parseFloat(event.target.value))
6984
}}
70-
className="thumb thumb--left"
85+
className={`thumb thumb--left ${disabledClass}`}
7186
/>
72-
<div className="slider">
73-
<div className="slider__track" />
87+
88+
<SliderTracks width={selectedPercent} color={color} disabled={disabled} inverted={inverted}>
7489
{renderLabels()}
75-
<div style={{ width: selectedPercent, backgroundColor: color }} className={'slider__range'} />
76-
</div>
90+
</SliderTracks>
7791
</>
7892
)
7993
}

src/components/SliderTracks.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { RangeColor } from '../types'
2+
3+
/** Component props. */
4+
interface SliderTracksProps {
5+
/** Labels component. */
6+
children: JSX.Element;
7+
/** Left offset. */
8+
left?: string;
9+
/** Track width. */
10+
width: string;
11+
/** Track color. */
12+
color: RangeColor;
13+
/** Disabled prop. */
14+
disabled: boolean;
15+
/** Inverted prop. */
16+
inverted: boolean;
17+
}
18+
19+
/**
20+
* Render Slider track with custom color, left offset and width.
21+
*
22+
* @param props - Component props.
23+
* @returns Component.
24+
*/
25+
const SliderTracks = (props: SliderTracksProps) => {
26+
const disabledClass = props.disabled ? 'disabled' : ''
27+
const invertedClass = props.inverted ? 'inverted' : ''
28+
const colorClass = props.inverted ? `${invertedClass}-${props.color}` : props.color
29+
30+
return (
31+
<div className={`slider ${invertedClass}`}>
32+
<div className={`slider__track ${disabledClass} ${invertedClass}`} />
33+
{props.children}
34+
<div
35+
style={{ left: props.left, width: props.width }}
36+
className={`color_track slider__range ${disabledClass} ${colorClass}`}
37+
/>
38+
</div>
39+
)
40+
}
41+
42+
export { SliderTracks }

src/stories/Colors.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const Colors = () => {
2727
setValue(newValue)
2828
}
2929

30-
const colors: RangeColor[] = ['RoyalBlue', 'Green', 'BlueViolet', 'Brown', 'Orange', 'Violet', 'Tomato', 'DarkMagenta']
30+
const colors: RangeColor[] = ['blue', 'green', 'red', 'brown', 'orange', 'violet', 'yellow', 'teal']
3131
return colors.map((color) => (
3232
<h1 key={color}>
3333
<SingleRangeSlider

src/stories/Inverted.stories.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ComponentMeta } from '@storybook/react'
2+
import { SingleRangeSlider } from '../components/SingleRangeSlider'
3+
import { useState } from 'react'
4+
import { RangeColor } from '../types'
5+
6+
// Styles
7+
import './colors.css'
8+
9+
export default {
10+
title: 'Example/SingleSlider/Inverted',
11+
component: SingleRangeSlider,
12+
subcomponents: { SingleRangeSlider },
13+
parameters: {
14+
layout: 'centered',
15+
backgrounds: {
16+
default: 'dark'
17+
}
18+
}
19+
} as ComponentMeta<typeof SingleRangeSlider >
20+
21+
export const Inverted = () => {
22+
const [value, setValue] = useState<number>(40)
23+
24+
/**
25+
* Handles changes.
26+
*
27+
* @param newValue - New value.
28+
*/
29+
function handleChange (newValue: number) {
30+
setValue(newValue)
31+
}
32+
33+
const colors: RangeColor[] = ['blue', 'green', 'red', 'brown', 'orange', 'violet', 'yellow', 'teal']
34+
return colors.map((color) => (
35+
<h1 key={color}>
36+
<SingleRangeSlider
37+
key={color}
38+
defaultMinValue={0}
39+
defaultMaxValue={100}
40+
value={value}
41+
color={color}
42+
inverted={true}
43+
onChange={handleChange}
44+
/>
45+
</h1>
46+
))
47+
}

0 commit comments

Comments
 (0)