-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
212 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,50 @@ | ||
import React from 'react'; | ||
import { Story, Meta } from '@storybook/react'; | ||
|
||
import { Slider, SliderProps, Range as SlRange, RangeProps, SliderHandle } from './Slider'; | ||
import { Text } from '../Text'; | ||
import { SliderTooltip } from 'rc-slider'; | ||
import { Slider, SliderProps } from './Slider'; | ||
|
||
export default { | ||
title: 'Inputs/Slider', | ||
component: Slider, | ||
subcomponents: { | ||
Range: SlRange, | ||
}, | ||
argTypes: { | ||
// backgroundColor: { control: 'color' }, | ||
}, | ||
} as Meta; | ||
|
||
export const Basic: Story<SliderProps> = (args) => <Slider {...args} />; | ||
|
||
export const Verticle: Story<SliderProps> = (args) => ( | ||
<div style={{ height: '400px' }}> | ||
<Slider vertical {...args} /> | ||
</div> | ||
); | ||
|
||
export const Range: Story<RangeProps> = (args) => <SlRange {...args} />; | ||
|
||
export const Marks = Basic.bind({}); | ||
Marks.args = { | ||
marks: { | ||
0: '0 KM', | ||
10: '10 KM', | ||
50: <Text color="red">50</Text>, | ||
100: '100 KM', | ||
}, | ||
Basic.args = { | ||
value: 50, | ||
}; | ||
|
||
export const WithTooltip: Story<SliderProps> = (args) => { | ||
const handle = ({ value, dragging, ...rest }: { value: number; dragging?: boolean }) => ( | ||
<SliderTooltip visible={dragging} prefixCls="rc-slider-tooltip" overlay={`${value} KM`} placement="top"> | ||
<SliderHandle value={value} {...rest} /> | ||
</SliderTooltip> | ||
); | ||
return <Slider {...args} handle={handle} />; | ||
export const MaxAndMin = Basic.bind({}); | ||
MaxAndMin.args = { | ||
min: 20, | ||
max: 80, | ||
}; | ||
|
||
// export const Verticle: Story<SliderProps> = (args) => ( | ||
// <div style={{ height: '400px' }}> | ||
// <Slider vertical {...args} /> | ||
// </div> | ||
// ); | ||
|
||
// export const Range: Story<RangeProps> = (args) => <SlRange {...args} />; | ||
|
||
// export const Marks = Basic.bind({}); | ||
// Marks.args = { | ||
// marks: { | ||
// 0: '0 KM', | ||
// 10: '10 KM', | ||
// 50: <Text color="red">50</Text>, | ||
// 100: '100 KM', | ||
// }, | ||
// }; | ||
|
||
// export const WithTooltip: Story<SliderProps> = (args) => { | ||
// const handle = ({ value, dragging, ...rest }: { value: number; dragging?: boolean }) => ( | ||
// <SliderTooltip visible={dragging} prefixCls="rc-slider-tooltip" overlay={`${value} KM`} placement="top"> | ||
// <SliderHandle value={value} {...rest} /> | ||
// </SliderTooltip> | ||
// ); | ||
// return <Slider {...args} handle={handle} />; | ||
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,111 @@ | ||
import React from 'react'; | ||
import { Global, css } from '@emotion/react'; | ||
import RcSlider, { Range as RcRange, Handle, SliderTooltip } from 'rc-slider'; | ||
import React, { useRef, useState } from 'react'; | ||
import { classes } from '../../helpers'; | ||
import { elevationCss } from '../../helpers/elevations'; | ||
import { marginCss } from '../../helpers/margin'; | ||
import { paddingCss } from '../../helpers/padding'; | ||
import { useTheme } from '../Theme/Theme'; | ||
import { rcSliderStyle } from './style'; | ||
import { RangeProps } from 'rc-slider/lib/Range'; | ||
import { SliderProps } from 'rc-slider/lib/Slider'; | ||
import { HandleProps } from 'rc-slider/lib/Handle'; | ||
import { style } from './style'; | ||
|
||
export const Slider: React.FC<SliderProps> = (props) => { | ||
const theme = useTheme(); | ||
rcSliderStyle(theme); | ||
const railRef = useRef<HTMLDivElement>(); | ||
const handleRef = useRef<HTMLDivElement>(); | ||
|
||
return ( | ||
<> | ||
<Global styles={css(rcSliderStyle(theme))} /> | ||
<RcSlider {...props} /> | ||
</> | ||
); | ||
}; | ||
const { min = 0, max = 100 } = props; | ||
const [value, updateValue] = useState(props.value || min); | ||
|
||
export const Range: React.FC<RangeProps> = (props) => { | ||
const theme = useTheme(); | ||
rcSliderStyle(theme); | ||
const [dragActive, updateDragActive] = useState(false); | ||
const css = style(theme, dragActive); | ||
|
||
const computeValue = (value: number) => { | ||
// console.log(value); | ||
// return (value * (max - min)) / 100; | ||
return value; | ||
}; | ||
|
||
const handleOnRailClick = (e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>) => { | ||
const rect = railRef.current?.getBoundingClientRect(); | ||
let value = 0; | ||
if (e.type === 'touchstart') { | ||
value = (((e as React.TouchEvent).touches[0].clientX - rect.left) * 100) / rect.width; | ||
} else { | ||
value = (((e as React.MouseEvent).clientX - rect.left) * 100) / rect.width; | ||
} | ||
value = computeValue(value); | ||
if (value >= min && value <= max) { | ||
props.onChange(value); | ||
updateValue(value); | ||
} | ||
updateDragActive(true); | ||
|
||
document.addEventListener('mousemove', drag); | ||
document.addEventListener('mouseup', dragEnd); | ||
document.addEventListener('touchmove', drag); | ||
document.addEventListener('touchend', dragEnd); | ||
}; | ||
|
||
const dragEnd = () => { | ||
updateDragActive(false); | ||
document.removeEventListener('mousemove', drag); | ||
document.removeEventListener('mouseup', dragEnd); | ||
document.removeEventListener('touchmove', drag); | ||
document.removeEventListener('touchend', dragEnd); | ||
}; | ||
|
||
const drag = (e: MouseEvent | TouchEvent) => { | ||
e.preventDefault(); | ||
|
||
const rect = railRef.current?.getBoundingClientRect(); | ||
|
||
let value = 0; | ||
if (e.type === 'touchstart') { | ||
value = (((e as TouchEvent).touches[0].clientX - rect.left) * 100) / rect.width; | ||
} else { | ||
value = (((e as MouseEvent).clientX - rect.left) * 100) / rect.width; | ||
} | ||
|
||
value = computeValue(value); | ||
|
||
if (value >= min && value <= max) { | ||
props.onChange(value); | ||
updateValue(value); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Global styles={css(rcSliderStyle(theme))} /> | ||
<RcRange {...props} /> | ||
</> | ||
<div | ||
className={classes('sha-el-slider', css.slider, marginCss(2))} | ||
onTouchStart={handleOnRailClick} | ||
onMouseDown={handleOnRailClick} | ||
> | ||
<div className={classes('sha-el-slider-rail')} ref={railRef} /> | ||
<div className={classes('sha-el-slider-track')} style={{ width: value + '%' }} /> | ||
<div className={classes('sha-el-slider-handle')} style={{ left: value + '%' }} ref={handleRef} /> | ||
<div | ||
className={classes('sha-el-slider-tooltip', elevationCss(4), paddingCss([5, 0]))} | ||
style={{ left: `calc(${value}% - 20px)` }} | ||
> | ||
{value} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export { RangeProps, SliderProps, HandleProps, Handle as SliderHandle, SliderTooltip }; | ||
export interface SliderProps { | ||
/** | ||
* Value of slider. | ||
*/ | ||
value?: number; | ||
/** | ||
* onChange event handler. | ||
*/ | ||
onChange?: (value: number) => void; | ||
/** | ||
* Min value of slider. | ||
*/ | ||
min?: number; | ||
/** | ||
* Max value of slider. | ||
*/ | ||
max?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { Slider, SliderTooltip, Range, SliderHandle } from './Slider'; | ||
export { Slider } from './Slider'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.