Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 3x 3x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x | import {useEffect, useRef, useState} from 'react' import cx from 'classnames' import PropTypes from 'prop-types' import Handler from './Handler.js' import markerFactory from './markerFactory.js' import {BASE_CLASS, CLASS_DISABLED, CLASS_FULLWIDTH, CLASS_INVERSE, Label, Range, Slider} from './settings.js' const getHandle = ({refAtomSlider, hideTooltip}) => props => <Handler refAtomSlider={refAtomSlider} hideTooltip={hideTooltip} {...props} /> const AtomSlider = ({ onChange, onAfterChange, value, min = 0, max = 100, step = 1, range, disabled, valueLabel, marks, valueLabelFormatter, hideMarks = false, hideTooltip = false, defaultValue, invertColors, isFullWidth }) => { let initialStateValue const refAtomSlider = useRef() const numMax = Number(max) const numMin = Number(min) Iif (value !== undefined) { initialStateValue = range ? value.map(Number) : Number(value) } else Iif (defaultValue !== undefined) { initialStateValue = range ? defaultValue.map(Number) : Number(defaultValue) } else { initialStateValue = range ? [numMin, numMax] : Math.trunc((numMin + numMax - numMin) / 2) } const [internalValue, setInternalValue] = useState(initialStateValue) useEffect(() => { Iif (value !== undefined) { setInternalValue(range ? value.map(Number) : Number(value)) } }, [value, range]) const handleChange = newValue => { const e = {} if (value === undefined) { setInternalValue(newValue) } if (typeof onChange === 'function') { onChange(e, {value: newValue}) } } const handleAfterChange = value => { if (typeof onAfterChange === 'function') { onAfterChange({}, {value}) } } const customProps = { handle: getHandle({refAtomSlider, hideTooltip}), onChange: handleChange, onAfterChange: handleAfterChange, disabled, marks: hideMarks ? {} : markerFactory({step, min, max, marks, isFullWidth}), max, min, step, value: internalValue } const computedClassName = cx( BASE_CLASS, {[CLASS_DISABLED]: disabled}, {[CLASS_INVERSE]: invertColors}, {[CLASS_FULLWIDTH]: isFullWidth} ) // Determine the type of the slider according to the range prop const Type = range ? Range : Slider return ( <div ref={refAtomSlider} className={computedClassName}> {valueLabel && customProps.handle ? ( <> <Label value={internalValue.toString()} formatter={valueLabelFormatter} percentage={((internalValue - min) / (max - min)) * 100} /> <Type {...customProps} /> </> ) : ( <Type {...customProps} /> )} </div> ) } AtomSlider.displayName = 'AtomSlider' AtomSlider.propTypes = { /** minimal value in range */ min: PropTypes.number, /** minimal value in range */ max: PropTypes.number, /** steps for range */ step: PropTypes.number, /** range mode */ range: PropTypes.bool, /** slider disabled */ disabled: PropTypes.bool, /** value */ value: PropTypes.oneOfType([PropTypes.number, PropTypes.arrayOf(PropTypes.number)]), /** defaultValue prop that set initial positions of handles */ defaultValue: PropTypes.oneOfType([PropTypes.number, PropTypes.arrayOf(PropTypes.number)]), /* callback to be called with every update of the input value */ onChange: PropTypes.func, /* callback to be called with when the ontouchend or onmouseup event is triggered */ onAfterChange: PropTypes.func, /* only if range=false, shows a position fixed label with the current value instead of a tooltip */ valueLabel: PropTypes.bool, /* Set your own mark labels, usually first and last positions */ marks: PropTypes.array, /* callback to format the value shown as label */ valueLabelFormatter: PropTypes.func, /* flag to hide marks if wanted */ hideMarks: PropTypes.bool, /* flag to hide tooltip if wanted */ hideTooltip: PropTypes.bool, /* If true it will invert the colors for selected track and rail */ invertColors: PropTypes.bool, /* If true it will render in a full width design */ isFullWidth: PropTypes.bool } export default AtomSlider |