Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rangeinput): creates form RangeInput component, test, and stories #194

Merged
merged 5 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"Sara Bocciardi <sara@truss.works>",
"Jeri Sommers <sojeri@truss.works>",
"Emily Mahanna <emahanna@truss.works>",
"Hana Worku <hana@truss.works>"
"Hana Worku <hana@truss.works>",
"Duncan Spencer <duncan@truss.works>"
],
"license": "Apache-2.0",
"bugs": {
Expand Down
69 changes: 69 additions & 0 deletions src/components/forms/RangeInput/RangeInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react'
import { RangeInput } from './RangeInput'
import { Label } from '../Label/Label'

export default {
title: 'Forms/RangeInput',
parameters: {
info: `
USWDS 2.0 RangeInput component

Source: https://designsystem.digital.gov/components/form-controls/#range
`,
},
}

const labelChildren = (
<>
<div>
<span>Start:</span> <span>0</span>
</div>
<div>
<span>End:</span> <span>100</span>
</div>
</>
)

const labelHint = <>(drag to adjust or use arrow keys)</>

export const defaultRange = (): React.ReactElement => (
<>
<Label htmlFor="range-slider" hint={labelHint}>
{labelChildren}
</Label>
<RangeInput id="range-slider" name="range" />
</>
)

export const customRange = (): React.ReactElement => (
<RangeInput
id="custom-range-slider"
name="rangeValue"
className="dark-theme"
min={1}
max={11}
step={2}
defaultValue={3}
/>
)

// Only tick marks are shown in Chrome but not with usa-range class currently because the appearance property is set to none
export const dataListRange = (): React.ReactElement => (
<>
<RangeInput
id="range-slider"
name="range"
min={0}
max={4}
defaultValue={2}
list="range-list-id"
/>
<datalist id="range-list-id">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</datalist>
</>
)
67 changes: 67 additions & 0 deletions src/components/forms/RangeInput/RangeInput.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react'
import { render, queryByText } from '@testing-library/react'

import { RangeInput } from './RangeInput'

describe('RangeInput component', () => {
it('renders without errors', () => {
const { queryByTestId } = render(
<RangeInput
id="range-slider-id"
name="rangeName"
className="additional-class"
/>
)
const rangeElement = queryByTestId('range')

expect(rangeElement).toBeInTheDocument()
expect(rangeElement).toHaveAttribute('id', 'range-slider-id')
expect(rangeElement).toHaveAttribute('name', 'rangeName')
expect(rangeElement).toHaveClass('usa-range')
expect(rangeElement).toHaveClass('additional-class')
})

it('renders with custom range values', () => {
const { queryByTestId } = render(
<RangeInput
id="range-slider-id"
name="rangeName"
min={0}
max={60}
step={15}
defaultValue={45}
/>
)
expect(queryByTestId('range')).toHaveAttribute('min', '0')
expect(queryByTestId('range')).toHaveAttribute('max', '60')
expect(queryByTestId('range')).toHaveAttribute('step', '15')
expect(queryByTestId('range')).toHaveAttribute('value', '45')
})

it('renders with step attribute set to value any', () => {
const { queryByTestId } = render(
<RangeInput id="range-slider-id" name="rangeName" step="any" />
)
expect(queryByTestId('range')).toHaveAttribute('step', 'any')
})

it('renders with specified datalist attribute', () => {
const { queryByTestId } = render(
<RangeInput
id="range-slider-id"
name="rangeName"
list="some-datalist-id"
/>
)
expect(queryByTestId('range')).toHaveAttribute('list', 'some-datalist-id')
})

it('renders with attached ref', () => {
const rangeRef: React.RefObject<HTMLInputElement> = React.createRef()

const { queryByTestId } = render(
<RangeInput id="range-slider-id" name="rangeName" inputRef={rangeRef} />
)
expect(queryByTestId('range')).toEqual(rangeRef.current)
})
})
34 changes: 34 additions & 0 deletions src/components/forms/RangeInput/RangeInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react'
import classnames from 'classnames'

interface RangeInputProps {
id: string
name: string
inputRef?:
| string
| ((instance: HTMLInputElement | null) => void)
| React.RefObject<HTMLInputElement>
| null
| undefined
}

export const RangeInput = (
props: RangeInputProps & React.InputHTMLAttributes<HTMLInputElement>
): React.ReactElement => {
// Range defaults to min = 0, max = 100, step = 1, and value = (max/2) if not specified.
const { className, inputRef, ...inputProps } = props

const classes = classnames('usa-range', className)

return (
<input
data-testid="range"
className={classes}
ref={inputRef}
type="range"
{...inputProps}
/>
)
}

export default RangeInput
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export { Form } from './components/forms/Form/Form'
export { FormGroup } from './components/forms/FormGroup/FormGroup'
export { Label } from './components/forms/Label/Label'
export { Radio } from './components/forms/Radio/Radio'
export { RangeInput } from './components/forms/RangeInput/RangeInput'
export { Textarea } from './components/forms/Textarea/Textarea'
export { TextInput } from './components/forms/TextInput/TextInput'

Expand Down