-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: range slider CU-2e09ymt (#219)
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { default } from './range-slider'; | ||
export type { Props as RangeSliderProps } from './range-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Meta, Story } from '@storybook/react/types-6-0'; | ||
import React, { useState } from 'react'; | ||
import { Box } from 'rebass'; | ||
import RangeSlider, { Props } from './range-slider'; | ||
import Value from '../typography/value'; | ||
import Divider from '../divider'; | ||
import Labeling from '../typography/labeling'; | ||
|
||
export default { | ||
title: 'Quartz/RangeSlider', | ||
component: RangeSlider, | ||
} as Meta; | ||
|
||
export const DefaultSlider: Story<Pick<Props, 'label' | 'step' | 'range'>> = ( | ||
props, | ||
) => { | ||
const [value, setValue] = useState([2, 4]); | ||
|
||
return ( | ||
<Box width="700px"> | ||
<RangeSlider value={value} onChange={setValue} {...props} /> | ||
<Divider /> | ||
<Labeling>Current value:</Labeling> | ||
<Value>[{value.join(', ')}]</Value> | ||
</Box> | ||
); | ||
}; | ||
|
||
DefaultSlider.args = { | ||
label: 'CPU cores', | ||
step: 1, | ||
range: [0, 10], | ||
}; | ||
|
||
export const WithCustomDisplayValue: Story< | ||
Pick<Props, 'label' | 'step' | 'range' | 'formatDisplayValue'> | ||
> = (props) => { | ||
const [value, setValue] = useState([128, 256]); | ||
|
||
return ( | ||
<Box width="700px"> | ||
<RangeSlider value={value} onChange={setValue} {...props} /> | ||
<Divider /> | ||
<Labeling>Current value:</Labeling> | ||
<Value>[{value.join(', ')}]</Value> | ||
</Box> | ||
); | ||
}; | ||
|
||
WithCustomDisplayValue.args = { | ||
label: 'RAM', | ||
step: 128, | ||
range: [0, 1024], | ||
formatDisplayValue: (value) => `${value} MB`, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import { Range as RCRangeSlider } from 'rc-slider'; | ||
import { FlexProps, Flex } from 'rebass'; | ||
import { useTheme } from 'emotion-theming'; | ||
import { ITheme } from '../../theme/types'; | ||
import Labeling from '../typography/labeling'; | ||
import Value from '../typography/value'; | ||
|
||
export interface Props extends Omit<FlexProps, 'value' | 'css' | 'onChange'> { | ||
label: string; | ||
value: number[]; | ||
range: number[]; | ||
step: number; | ||
disabled?: boolean; | ||
onChange: (value: number[]) => void; | ||
formatDisplayValue?: (value: number) => string; | ||
} | ||
|
||
const RangeSlider = ({ | ||
label, | ||
defaultValue, | ||
value, | ||
onChange, | ||
range, | ||
step, | ||
disabled, | ||
formatDisplayValue = (displayValue) => displayValue.toString(), | ||
...flexProps | ||
}: Props) => { | ||
const theme = useTheme<ITheme>(); | ||
|
||
return ( | ||
<Flex flexDirection="column" {...flexProps}> | ||
<Flex alignItems="center" mb="10px"> | ||
<Labeling bold mr="8px"> | ||
{label} | ||
</Labeling> | ||
<Flex | ||
backgroundColor="primaryShade2" | ||
px="4px" | ||
py="2px" | ||
sx={{ borderRadius: '2px' }} | ||
> | ||
<Value color="primary">{value.map(formatDisplayValue).join('-')}</Value> | ||
</Flex> | ||
</Flex> | ||
<RCRangeSlider | ||
disabled={disabled} | ||
value={value} | ||
onChange={onChange} | ||
min={range[0]} | ||
max={range[1]} | ||
step={step} | ||
railStyle={{ | ||
backgroundColor: theme.colors.grayShade2, | ||
}} | ||
trackStyle={[ | ||
{ | ||
backgroundColor: theme.colors.primary, | ||
}, | ||
]} | ||
handleStyle={[ | ||
{ | ||
borderColor: theme.colors.primary, | ||
boxShadow: `0 0 5px ${theme.colors.primary}`, | ||
}, | ||
]} | ||
/> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default RangeSlider; |
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 |
---|---|---|
|
@@ -44,3 +44,4 @@ WithCustomDisplayValue.args = { | |
range: [0, 1024], | ||
formatDisplayValue: (value) => `${value} MB`, | ||
}; | ||
|
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