|
@@ -7,13 +7,12 @@ import {
|
|
|
} from 'rxjs/operators';
|
|
|
|
|
|
import {
|
|
|
- Container, Wrapper, Rail, Track,
|
|
|
+ OuterWrapper, Wrapper, Rail, Track,
|
|
|
} from './styled';
|
|
|
|
|
|
type Props = {
|
|
|
color?: 'primary' | 'secondary';
|
|
|
- max?: number;
|
|
|
- min?: number;
|
|
|
+ maximum?: number;
|
|
|
defaultValue?: number;
|
|
|
disabled?: boolean;
|
|
|
onChange?: (value: number) => void;
|
|
@@ -22,6 +21,7 @@ type Props = {
|
|
|
const Sliders: React.FunctionComponent<Props> = ({
|
|
|
defaultValue = 0,
|
|
|
onChange,
|
|
|
+ maximum = 100,
|
|
|
}: Props) => {
|
|
|
const sliderRef = useRef<HTMLDivElement>(null);
|
|
|
const [valueState, setValueState] = useState(defaultValue);
|
|
@@ -35,8 +35,9 @@ const Sliders: React.FunctionComponent<Props> = ({
|
|
|
|
|
|
if (slider) {
|
|
|
const { width, left } = slider.getBoundingClientRect();
|
|
|
- const value = (event.clientX - left) / width;
|
|
|
- let percent = parseValueToPercent(value);
|
|
|
+ const moveDistance = event.clientX - left;
|
|
|
+ const moveRate = moveDistance / width;
|
|
|
+ let percent = parseValueToPercent(moveRate);
|
|
|
|
|
|
if (percent <= 0) {
|
|
|
percent = 0;
|
|
@@ -45,7 +46,10 @@ const Sliders: React.FunctionComponent<Props> = ({
|
|
|
}
|
|
|
|
|
|
setValueState(percent);
|
|
|
- if (onChange) onChange(percent);
|
|
|
+ if (onChange) {
|
|
|
+ const value = Math.floor(percent * (maximum * 0.01));
|
|
|
+ onChange(value);
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -72,11 +76,12 @@ const Sliders: React.FunctionComponent<Props> = ({
|
|
|
}, []);
|
|
|
|
|
|
useEffect(() => {
|
|
|
- setValueState(defaultValue);
|
|
|
+ const percent = parseValueToPercent(defaultValue / maximum);
|
|
|
+ setValueState(percent);
|
|
|
}, []);
|
|
|
|
|
|
return (
|
|
|
- <Container>
|
|
|
+ <OuterWrapper>
|
|
|
<Wrapper
|
|
|
ref={sliderRef}
|
|
|
onMouseDown={handleMouseDown}
|
|
@@ -87,7 +92,7 @@ const Sliders: React.FunctionComponent<Props> = ({
|
|
|
isActive={isActive}
|
|
|
/>
|
|
|
</Wrapper>
|
|
|
- </Container>
|
|
|
+ </OuterWrapper>
|
|
|
);
|
|
|
};
|
|
|
|