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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | 7x 7x 7x 7x 7x 7x 7x 6x 6x 4x 4x 4x 9x 4x 4x 4x 4x 9x 3x 6x 6x 9x 9x 9x 5x 5x 5x 4x 4x 4x 9x 9x 9x 9x 16x 16x 25x 25x 25x 25x 50x 50x 25x 23x 25x 23x 18x 5x 23x 89x 89x 89x | /** * @category controls * @component slider * @variations collab-ui-react */ import React from 'react'; import PropTypes from 'prop-types'; import SliderPointer from '@collab-ui/react/Slider/SliderPointer'; class Slider extends React.Component { state = { sliderLow: this.props.value.low || this.props.min, sliderHigh: this.props.value.high || this.props.value, scale: [this.props.min, this.props.max], selectionWidth: null, } componentWillMount() { const { min, max, tick } = this.props; tick && this.getScale(min, max, tick); this.getSelectionWidth(); } componentDidMount() { const { min, max } = this.props; const { scale } = this.state; this.setScalePos(scale, min, max); } setScalePos = (scale, min, max) => { this.ticksContainer && this.ticksContainer.childNodes.forEach((child, idx) => { const leftValue = `${(scale[idx] - min) / max * 100}%`; child.style.left = `calc(${leftValue} - ${(child.offsetWidth/2)}px)`; }); }; getScale = (low = 0, high, tick) => { let value = high; let ticksArray = [high]; while (value > 0 && (value - tick) >= low) { value -= tick; ticksArray.unshift(Math.abs(Math.round(value / tick) * tick)); } this.setState({ scale: ticksArray }); }; getSliderWidth = () => { return this.sliderBar.getBoundingClientRect().width; }; getStepWidth = () => { const maxValue = this.props.max - this.props.min; const maxWidth = this.getSliderWidth(); return (this.props.step / maxValue) * maxWidth; }; getSteps = (position) => { if (position.isKeyBoard) return 1; const diff = position.direction === 1 ? position.to - position.from : position.from - position.to; Iif (diff < 0) return 0; const steps = diff / this.getStepWidth(); return steps - Math.floor(steps) >= 0.5 ? Math.ceil(steps) : Math.floor(steps); }; getLimit = (pointerKey, direction) => { if (pointerKey === 'sliderLow') { return this.props.canCross ? direction === 1 ? this.props.max : this.props.min : direction === 1 ? this.state.sliderHigh : this.props.min; } else Eif (pointerKey === 'sliderHigh') { return this.props.canCross ? direction === 1 ? this.props.max : this.props.min : direction === 1 ? this.props.max : this.state.sliderLow; } }; returnCurrentValues = () => { this.getSelectionWidth(); Eif (this.props.onChange) { return this.props.onChange( typeof this.props.value === 'object' ? { low: Math.min(this.state.sliderHigh, this.state.sliderLow), high: Math.max(this.state.sliderHigh, this.state.sliderLow) } : this.state.sliderHigh ); } }; moveForward = (key, pixelMove, limit) => { const newPosition = this.state[key] + pixelMove <= limit ? this.state[key] + pixelMove : limit; this.setState({ [key]: newPosition }, () => this.returnCurrentValues()); }; moveBack = (key, pixelMove, limit) => { const newPosition = this.state[key] - pixelMove >= limit ? this.state[key] - pixelMove : limit; this.setState({ [key]: newPosition }, () => this.returnCurrentValues()); }; onSliderMove = (key, position) => { Iif (this.props.disabled) return; const limit = this.getLimit(key, position.direction); const pixelMove = this.getSteps(position) * this.props.step; position.direction === 1 ? this.moveForward(key, pixelMove, limit) : this.moveBack(key, pixelMove, limit); }; getSelectionWidth = () => { const baseValue = Number.isInteger(this.state.sliderLow) ? this.state.sliderLow : this.props.min; this.setState({ selectionWidth: { width: `${(Math.abs(this.state.sliderHigh - baseValue) / this.props.max) * 100}%`, left: `${((Math.min(baseValue, this.state.sliderHigh) - this.props.min) / this.props.max) * 100}%` } }); }; render() { const { value, disabled, className, max, min, translateFn } = this.props; const { sliderHigh, sliderLow, scale, selectionWidth } = this.state; const renderTicks = () => { const ticks = scale.map((tickValue, idx) => { const tickLabel = translateFn ? translateFn(tickValue) : tickValue; return ( <span key={`tick-${idx}`} className='cui-slider__hashlabel'> {tickLabel} </span> ); }); return ( <div ref={ref => this.ticksContainer = ref}> {ticks} </div> ); }; return ( <div className={ `cui-slider ${className}` + `${(disabled && ` cui-slider--disabled`) || ''}` + `${(className && ` ${className}`) || ''}` } > <span className='cui-slider__bar' ref={ref => this.sliderBar = ref} /> <span className='cui-slider__selection' style={selectionWidth} /> { Number.isInteger(value.low) && <SliderPointer position={((sliderLow - min) / (max - min)) * 100} onMove={(b) => this.onSliderMove('sliderLow', b)} ref={ref => this.sliderLow = ref} /> } <SliderPointer position={((sliderHigh - min) / (max - min)) * 100} onMove={(b) => this.onSliderMove('sliderHigh', b)} ref={ref => this.sliderHigh = ref} /> {renderTicks()} </div> ); } } Slider.propTypes = { /** @prop Determines if minimum pointer can cross over maximum pointer | false */ canCross: PropTypes.bool, /** @prop Optional CSS class string | '' */ className: PropTypes.string, /** @prop Set the attribute disabled to Slider | false */ disabled: PropTypes.bool, /** @prop Set the initial maximum value */ max: PropTypes.number.isRequired, /** @prop Set the initial minimum value | 0 */ min: PropTypes.number, /** @prop Callback function invoked by user when change a pointer position | null */ onChange: PropTypes.func, /** @prop Set visual step measurement | 1 */ step: PropTypes.number, /** @prop Set increment of x-axis labels | 0 */ tick: PropTypes.number, /** @prop Function to compute layout of Slider | null */ translateFn: PropTypes.func, /** @prop Set either maximum pointer value or a combination of high and low pointer values | 0 */ value: PropTypes.oneOfType([ PropTypes.shape({ high: PropTypes.number.isRequired, low: PropTypes.number.isRequired, }), PropTypes.number ]) }; Slider.defaultProps = { canCross: false, className: '', disabled: false, min: 0, onChange: null, step: 1, tick: 0, translateFn: null, value: 0 }; Slider.displayName = 'Slider'; export default Slider; /** * @component slider * @section default * @react import { Slider } from '@collab-ui/react'; export default class DefaultSlider extends React.Component { render() { return ( <Slider min={0} max={500} tick={100} value={200} step={1} /> ); } } **/ /** * @component slider * @section two-handles * @react import { Slider } from '@collab-ui/react'; export default class SliderTwoHandles extends React.Component { state = { slider1: {low: 100, high: 200}, } render() { return ( <div className="row" key="child-0"> <div> Low: {this.state.slider1.low} High: {this.state.slider1.high} </div> <Slider min={0} max={500} tick={100} value={this.state.slider1} step={1} onChange={value => this.setState({ slider1: value })} /> </div> ); } } **/ /** * @component slider * @section cross * @react import { Slider } from '@collab-ui/react'; export default class SliderCross extends React.Component { state = { slider1: {low: 100, high: 200}, } render() { return ( <div className="row" key="child-2"> <div> Low: {this.state.slider1.low} High: {this.state.slider1.high} </div> <Slider min={0} max={500} tick={100} value={this.state.slider1} step={1} canCross onChange={value => this.setState({ slider1: value })} /> </div> ); } } **/ /** * @component slider * @section step * @react import { Slider } from '@collab-ui/react'; export default class SliderStep extends React.Component { state = { slider1: { low: 100, high: 200 }, slider2: { low: 100, high: 200 }, } render() { return ( <span> <div className="row" key='child-0'> <div>Low: {this.state.slider1.low} High: {this.state.slider1.high}</div> <Slider min={0} max={500} tick={100} value={this.state.slider1} step={20} canCross onChange={value => this.setState({ slider1: value })} /> </div> <br key='child-1' /> <div className="row" key='child-2'> <div>Low: {this.state.slider2.low} High: {this.state.slider2.high}</div> <Slider min={0} max={500} tick={100} value={this.state.slider2} step={50} canCross onChange={value => this.setState({ slider2: value })} /> </div> </span> ); } } **/ |