images_viewer/src/components/JumpControls.tsx

64 lines
1.7 KiB
TypeScript
Raw Normal View History

2020-05-08 17:02:03 +07:00
import React, {useState, useCallback, useEffect} from 'react'
2020-05-09 12:32:54 +07:00
import {Slider, InputNumber} from 'antd'
import styled from 'styled-components'
const Container = styled.div`
width: 100%;
display: inline-flex;
`
2020-05-08 17:02:03 +07:00
const JumpControls = (props: any)=>{
const {src, currIndex, setBookContextState} = props
const [inputValue, setInputValue] = useState(currIndex)
2020-05-09 12:32:54 +07:00
const [sliderValue, setSliderValue] = useState(currIndex)
2020-05-08 17:02:03 +07:00
const onChange = useCallback(
(value) => {
if(value){
const newCurrIndex = value -1
if(newCurrIndex < src.length && newCurrIndex >=0){
setBookContextState({
currIndex: value - 1,
flagToReverse: value < inputValue
})
}
}
},
2020-05-09 11:45:34 +07:00
[inputValue, src.length, setBookContextState])
2020-05-08 17:02:03 +07:00
useEffect(()=>{
setInputValue(currIndex + 1)
}, [currIndex])
2020-05-09 12:32:54 +07:00
useEffect(() => {
setSliderValue(inputValue)
}, [inputValue])
const sliderRestProps = ()=>{
return sliderValue === inputValue? {}: {value: inputValue}
}
return <Container>
2020-05-08 17:02:03 +07:00
<Slider
2020-05-09 12:32:54 +07:00
style={{
flexGrow: 1
}}
2020-05-08 17:02:03 +07:00
min={1}
max={src.length}
2020-05-09 11:51:01 +07:00
onAfterChange={onChange}
2020-05-09 12:32:54 +07:00
defaultValue={typeof inputValue === 'number' ? inputValue : 1}
{...sliderRestProps()}
2020-05-08 17:02:03 +07:00
/>
2020-05-09 12:32:54 +07:00
2020-05-08 17:02:03 +07:00
<InputNumber
min={1}
max={src.length}
style={{ margin: '0 8px' }}
value={inputValue}
onChange={onChange}
/>
2020-05-09 12:32:54 +07:00
</Container>
2020-05-08 17:02:03 +07:00
}
export default JumpControls