Skip to content

Commit 01d7bd9

Browse files
burbuli8raPanos Christophides
andauthored
feat(input): add input range component (#388)
* feat(input): add input range component * test(input): add basic test for InputRange component * chore(input): add story for InputRange component * update slider colors * chore(input): PR requested changes * Remove unused useState * Remove any prop key from TS declaration file * Move background size to styled component * Rename TextRange story to InputRange * Use getColor with theme colors instead of hex values * chore(npm): version patch (dev) * v2.3.25 Co-authored-by: Panos Christophides <panos@netdata.cloud>
1 parent 6c83de4 commit 01d7bd9

7 files changed

Lines changed: 102 additions & 1 deletion

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netdata/netdata-ui",
3-
"version": "2.3.24",
3+
"version": "2.3.25",
44
"description": "netdata UI kit",
55
"main": "./lib/index.js",
66
"files": [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ChangeEvent, FC, SyntheticEvent } from "react"
2+
3+
export interface RangeInputProps {
4+
"data-testid"?: string
5+
max?: number
6+
min?: number
7+
onChange: (e: ChangeEvent<HTMLInputElement>) => void
8+
onClick?: (e: SyntheticEvent<HTMLButtonElement>) => void
9+
onInput?: (e: SyntheticEvent<HTMLButtonElement>) => void
10+
step?: number
11+
value: number
12+
}
13+
14+
declare const RangeInput: FC<RangeInputProps>
15+
16+
export { RangeInput }
17+
18+
export default RangeInput
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react"
2+
import { InputRange as StyledInputRange } from "./styled"
3+
4+
const InputRange = ({ max = 100, min = 0, step = 1, value = 0, ...rest }) => (
5+
<StyledInputRange
6+
data-testid="rangeInput"
7+
max={max}
8+
min={min}
9+
step={step}
10+
type="range"
11+
value={value}
12+
{...rest}
13+
/>
14+
)
15+
16+
export default InputRange
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { useState } from "react"
2+
import { storiesOf } from "@storybook/react"
3+
import RangeInput from "./index"
4+
5+
const rangeStory = storiesOf("Components|Controls/InputRange", module)
6+
7+
rangeStory.add("Input Range", () => {
8+
const [value, setValue] = useState(0.1)
9+
10+
return (
11+
<RangeInput
12+
data-testid="metricCorrelation-resultsSlider"
13+
min={0}
14+
max={1}
15+
onChange={event => {
16+
console.log(`You changed ${event.target.value.toString()}`)
17+
}}
18+
onClick={() => {
19+
console.log("You clicked input range")
20+
}}
21+
onInput={event => setValue(event.target.value)}
22+
value={value}
23+
step={0.00000000000001}
24+
/>
25+
)
26+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react"
2+
import RangeInput from "./index"
3+
import { renderWithProviders, screen } from "testUtilities"
4+
5+
const setup = props => renderWithProviders(<RangeInput {...props} />)
6+
7+
describe("InputRange component", () => {
8+
test("should render component", () => {
9+
setup()
10+
expect(screen.getByTestId("rangeInput")).toBeInTheDocument()
11+
})
12+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import styled from "styled-components"
2+
import { getColor } from "src/theme"
3+
4+
export const InputRange = styled.input.attrs({ type: "range" })`
5+
-webkit-appearance: none;
6+
height: 2px;
7+
background-color: ${getColor("primary")}40;
8+
background-image: linear-gradient(${getColor("primary")}, ${getColor("primary")});
9+
background-repeat: no-repeat;
10+
background-size: ${({ max, value }) => `${(value * 100) / max}% 100%`};
11+
cursor: pointer;
12+
width: 100%;
13+
14+
&::-webkit-slider-thumb {
15+
-webkit-appearance: none;
16+
height: 10px;
17+
width: 10px;
18+
border-radius: 50%;
19+
background: ${getColor("primary")};
20+
transition: all 0.3s ease-in-out;
21+
22+
&:active {
23+
height: 16px;
24+
width: 16px;
25+
}
26+
}
27+
`

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ export {
6767
FilterBoxAutocompleteHandler,
6868
} from "./components/filter-box"
6969

70+
export { default as InputRange } from "./components/input/range"
71+
7072
export { default as Drop } from "./components/drops/drop"
7173
export { default as DropContainer } from "./components/drops/container"
7274
export { default as Tooltip } from "./components/drops/tooltip"

0 commit comments

Comments
 (0)