Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangboya3 committed Jun 21, 2021
1 parent 953cb6a commit df4ca4c
Show file tree
Hide file tree
Showing 4 changed files with 14,906 additions and 49 deletions.
76 changes: 40 additions & 36 deletions components/slider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CSSTransition } from 'react-transition-group'
import Provider from '../context'

const prefixCls = 'hi-slider'
const noop = () => {}
const noop = () => { }
const Slider = memo(
({
defaultValue = 0,
Expand All @@ -22,18 +22,6 @@ const Slider = memo(
marks = {},
theme
}) => {
const getValue = useCallback((value) => {
if (value === undefined) {
return value
}
if (value > (initMax || 100)) {
value = initMax || 100
} else if (value < (initMin || 0)) {
value = initMin || 0
}
return value
}, [])
const [value, setValue] = useState(initValue !== undefined ? getValue(initValue) : getValue(defaultValue))
// 是否可拖动
const [canMove, setCanMove] = useState(false)

Expand All @@ -52,7 +40,21 @@ const Slider = memo(
const [isInitPage, setIsInitPage] = useState(true)
const sliderRef = useRef()
const tooltipRef = useRef()

const getValue = useCallback(
(value) => {
if (value === undefined) {
return value
}
if (value > (initMax || 100)) {
value = initMax || 100
} else if (value < (initMin || 0)) {
value = initMin || 0
}
return value
},
[max, min]
)
const [value, setValue] = useState(initValue !== undefined ? getValue(initValue) : getValue(defaultValue))
useClickOutside((e) => {
setShowTooltip(false)
}, document.querySelector(`#${prefixCls}`))
Expand Down Expand Up @@ -94,7 +96,7 @@ const Slider = memo(
setPositionStep((step / ((max || 100) - (min || 0))) * 100)
// 设置初始位置
setStartPosition(((value - (min || 0)) / ((max || 100) - (min || 0))) * 100)
}, [])
}, [max, min])

// <- -> 键盘事件
const onKeyDown = useCallback(
Expand Down Expand Up @@ -128,7 +130,7 @@ const Slider = memo(
onChange(_value)
}
},
[value]
[value, max, min]
)

useEffect(() => {
Expand Down Expand Up @@ -199,7 +201,7 @@ const Slider = memo(
onChange(changeValue)
}
},
[canMove, positionStep, startPosition]
[canMove, positionStep, startPosition, max, min]
)
const onMouseUp = useCallback(
(e) => {
Expand All @@ -216,11 +218,11 @@ const Slider = memo(
return
}
if (canMove) {
window.onmouseup = onMouseUp
window.onmousemove = onMouseMove
document.body.onmouseup = onMouseUp
document.body.onmousemove = onMouseMove
} else {
window.onmouseup = null
window.onmousemove = null
document.body.onmouseup = null
document.body.onmousemove = null
}
}, [canMove, disabled, onMouseUp, onMouseMove])

Expand All @@ -229,7 +231,7 @@ const Slider = memo(
(value) => {
return ((value - (min || 0)) / ((max || 100) - (min || 0))) * 100
},
[value]
[value, max, min]
)

// 鼠标落下
Expand Down Expand Up @@ -302,17 +304,20 @@ const Slider = memo(
setStartPosition(position)
onChange(value)
},
[positionStep, newRightPosition, vertical, disabled]
[positionStep, newRightPosition, vertical, disabled, max, min]
)
// 点击marks上的点
const onMarksClick = useCallback((e, value) => {
e.stopPropagation()
if (initValue === undefined) {
setValue(value)
setStartPosition(((value - (min || 0)) / ((max || 100) - (min || 0))) * 100)
}
onChange(value)
}, [])
const onMarksClick = useCallback(
(e, value) => {
e.stopPropagation()
if (initValue === undefined) {
setValue(value)
setStartPosition(((value - (min || 0)) / ((max || 100) - (min || 0))) * 100)
}
onChange(value)
},
[max, min]
)
const sliderClasses = classNames(prefixCls, `theme__${theme}`, {
[`${prefixCls}--disabled`]: disabled,
[`${prefixCls}--vertical`]: vertical,
Expand Down Expand Up @@ -342,9 +347,8 @@ const Slider = memo(
ref={tooltipRef}
onClick={onHandleClick}
style={{
[!vertical ? 'left' : 'top']: `${
!vertical ? newRightPosition.toFixed(4) : (100 - newRightPosition).toFixed(4)
}%`
[!vertical ? 'left' : 'top']: `${!vertical ? newRightPosition.toFixed(4) : (100 - newRightPosition).toFixed(4)
}%`
}}
tabIndex="0"
onKeyDown={onKeyDown}
Expand Down Expand Up @@ -401,8 +405,8 @@ const Slider = memo(
</div>
{((min && max) || Object.entries(marks).length !== 0) && (
<div className={`${prefixCls}__stepText`}>
{min && <span className={`${prefixCls}__min ${prefixCls}__stepText-dot`}>{min}</span>}
{max && <span className={`${prefixCls}__max ${prefixCls}__stepText-dot`}>{max}</span>}
{min !== undefined && <span className={`${prefixCls}__min ${prefixCls}__stepText-dot`}>{min}</span>}
{max !== undefined && <span className={`${prefixCls}__max ${prefixCls}__stepText-dot`}>{max}</span>}
{Object.entries(marks).map(([key, item], index) => (
<span
className={`${prefixCls}__stepText-dot`}
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-standard": "^4.0.1",
"file-loader": "^1.1.11",
"fork-ts-checker-webpack-plugin": "4.0.3",
"fork-ts-checker-webpack-plugin": "^4.0.3",
"gh-pages": "^2.0.1",
"github-markdown-css": "^3.0.1",
"gulp": "^3.9.1",
Expand Down
Loading

0 comments on commit df4ca4c

Please sign in to comment.