This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
/
Slider.tsx
239 lines (206 loc) · 7.63 KB
/
Slider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { Accessibility, sliderBehavior } from '@fluentui/accessibility'
import * as React from 'react'
import * as _ from 'lodash'
import * as PropTypes from 'prop-types'
import * as customPropTypes from '@fluentui/react-proptypes'
import { handleRef, Ref } from '@fluentui/react-component-ref'
import cx from 'classnames'
import {
applyAccessibilityKeyHandlers,
AutoControlledComponent,
ChildrenComponentProps,
commonPropTypes,
partitionHTMLProps,
UIComponentProps,
RenderResultConfig,
setWhatInputSource,
} from '../../utils'
import {
ComponentEventHandler,
ShorthandValue,
WithAsProp,
withSafeTypeForAs,
Omit,
} from '../../types'
import { SupportedIntrinsicInputProps } from '../../utils/htmlPropsUtils'
import Box, { BoxProps } from '../Box/Box'
const processInputValues = (
p: Pick<SliderProps, 'min' | 'max'> & Pick<SliderState, 'value'>,
): { min: number; max: number; value: number; valueAsPercentage: string } => {
let min = _.toNumber(p.min)
let max = _.toNumber(p.max)
let value = _.toNumber(p.value)
if (isNaN(min)) min = Number(Slider.defaultProps.min)
if (isNaN(max)) max = Number(Slider.defaultProps.max)
value = isNaN(value) ? min + (max - min) / 2 : Math.min(max, Math.max(min, value))
const valueAsPercentage = `${(100 * (value - min)) / (max - min)}%`
return { min, max, value, valueAsPercentage }
}
export interface SliderSlotClassNames {
input: string
inputWrapper: string
rail: string
thumb: string
track: string
}
export interface SliderProps
extends UIComponentProps,
ChildrenComponentProps,
Omit<SupportedIntrinsicInputProps, 'defaultValue'> {
/** Accessibility behavior if overridden by the user. */
accessibility?: Accessibility
/** The default value of the slider. */
defaultValue?: string | number
/** A slider can be read-only and unable to change states. */
disabled?: SupportedIntrinsicInputProps['disabled']
/** A slider can take the width of its container. */
fluid?: boolean
/**
* Callback that creates custom accessibility message a screen reader narrates when the value changes.
* @param props - Slider props.
*/
getA11yValueMessageOnChange?: (props: SliderProps) => string
/** Shorthand for the input component. */
input?: ShorthandValue<BoxProps>
/** Ref for input DOM node. */
inputRef?: React.Ref<HTMLElement>
/** The maximum value of the slider. */
max?: SupportedIntrinsicInputProps['max']
/** The minimum value of the slider. */
min?: SupportedIntrinsicInputProps['min']
/**
* Called after item checked state is changed.
* @param event - React's original SyntheticEvent.
* @param data - All props.
*/
onChange?: ComponentEventHandler<SliderProps & { value: string }>
/**
* A number that specifies the granularity that the value must adhere to, or the special value 'any'.
* A string value of any means that no stepping is implied, and any value is allowed
* (barring other constraints, such as min and max).
*/
step?: SupportedIntrinsicInputProps['step']
/** The value of the slider. */
value?: string | number
/** A slider can be displayed vertically. */
vertical?: boolean
}
export interface SliderState {
value: SliderProps['value']
}
class Slider extends AutoControlledComponent<WithAsProp<SliderProps>, SliderState> {
inputRef = React.createRef<HTMLElement>()
static displayName = 'Slider'
static className = 'ui-slider'
static slotClassNames: SliderSlotClassNames
static propTypes = {
...commonPropTypes.createCommon({ content: false }),
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
fluid: PropTypes.bool,
getA11yValueMessageOnChange: PropTypes.func,
input: customPropTypes.itemShorthand,
inputRef: customPropTypes.ref,
max: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
min: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
onChange: PropTypes.func,
step: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
vertical: PropTypes.bool,
}
static defaultProps: SliderProps = {
accessibility: sliderBehavior,
getA11yValueMessageOnChange: ({ value }) => String(value),
max: 100,
min: 0,
step: 1,
}
static autoControlledProps = ['value']
getInitialAutoControlledState(): Partial<SliderState> {
return { value: 50 }
}
handleInputOverrides = () => ({
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
const value = _.get(e, 'target.value')
_.invoke(this.props, 'onChange', e, { ...this.props, value })
this.setState({ value })
},
onMouseDown: (e: React.MouseEvent<HTMLInputElement>) => {
setWhatInputSource('mouse')
_.invoke(this.props, 'onMouseDown', e, this.props)
},
})
renderComponent({
ElementType,
classes,
accessibility,
rtl,
styles,
unhandledProps,
}: RenderResultConfig<SliderProps>) {
const { input, inputRef, step } = this.props
const [htmlInputProps, restProps] = partitionHTMLProps(unhandledProps)
const type = 'range'
const { min, max, value, valueAsPercentage } = processInputValues({
min: this.props.min,
max: this.props.max,
value: this.state.value || '',
})
// we need 2 wrappers around the slider rail, track, input and thumb slots to achieve correct component sizes
return (
<ElementType className={classes.root} {...accessibility.attributes.root} {...restProps}>
<div className={cx(Slider.slotClassNames.inputWrapper, classes.inputWrapper)}>
<span className={cx(Slider.slotClassNames.rail, classes.rail)} />
<span
className={cx(Slider.slotClassNames.track, classes.track)}
style={{ width: valueAsPercentage }}
/>
<Ref
innerRef={(inputElement: HTMLElement) => {
handleRef(this.inputRef, inputElement)
handleRef(inputRef, inputElement)
}}
>
{Box.create(input || type, {
defaultProps: () => ({
...htmlInputProps,
...accessibility.attributes.input,
className: Slider.slotClassNames.input,
as: 'input',
min,
max,
step,
type,
value,
styles: styles.input,
...applyAccessibilityKeyHandlers(accessibility.keyHandlers.input, htmlInputProps),
}),
overrideProps: this.handleInputOverrides,
})}
</Ref>
{/* the thumb slot needs to appear after the input slot */}
<span
className={cx(Slider.slotClassNames.thumb, classes.thumb)}
style={{ [rtl ? 'right' : 'left']: valueAsPercentage }}
/>
</div>
</ElementType>
)
}
}
Slider.slotClassNames = {
input: `${Slider.className}__input`,
inputWrapper: `${Slider.className}__input-wrapper`,
rail: `${Slider.className}__rail`,
thumb: `${Slider.className}__thumb`,
track: `${Slider.className}__track`,
}
/**
* A Slider represents an input that allows user to choose a value from within a specific range.
*
* @accessibility
* Implements [ARIA Slider](https://www.w3.org/TR/wai-aria-practices-1.1/#slider) design pattern.
* @accessibilityIssues
* [Slider - JAWS narrates slider value twice when using PageUp / PageDown](https://github.com/FreedomScientific/VFO-standards-support/issues/220)
* [Slider - JAWS narrates current and new value in vertical slider](https://github.com/FreedomScientific/VFO-standards-support/issues/219)
*/
export default withSafeTypeForAs<typeof Slider, SliderProps, 'input'>(Slider)