-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slider): add fluent ui range slider
- Loading branch information
1 parent
aeb9a5b
commit b74e0e1
Showing
22 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
Binary file added
BIN
+181 KB
.yarn/cache/@fluentui-date-time-utilities-npm-8.5.0-7c5e3f48a7-6daae1d623.zip
Binary file not shown.
Binary file added
BIN
+70.8 KB
.yarn/cache/@fluentui-dom-utilities-npm-2.2.0-ff9107bcab-ba0f29a464.zip
Binary file not shown.
Binary file added
BIN
+653 KB
.yarn/cache/@fluentui-font-icons-mdl2-npm-8.2.5-0d3fba4780-24fa8b3ec9.zip
Binary file not shown.
Binary file added
BIN
+198 KB
.yarn/cache/@fluentui-foundation-legacy-npm-8.2.5-390c4b6aea-ed99a1d82b.zip
Binary file not shown.
Binary file added
BIN
+41.7 KB
.yarn/cache/@fluentui-keyboard-key-npm-0.4.0-243207fd2a-e114cf098c.zip
Binary file not shown.
Binary file added
BIN
+387 KB
.yarn/cache/@fluentui-merge-styles-npm-8.5.0-98e0b1d583-b7ff64bb89.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+24 KB
.yarn/cache/@fluentui-react-window-provider-npm-2.2.0-555326ae73-31a92060f0.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+264 KB
.yarn/cache/@fluentui-style-utilities-npm-8.6.5-0ae79ad0bf-9b9e2f1269.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+35.3 KB
.yarn/cache/@microsoft-load-themed-styles-npm-1.10.248-9a05e3927c-b51dbb882c.zip
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import React, { useMemo, useState, useCallback } from 'react' | ||
import styled, { useTheme } from 'styled-components' | ||
|
||
import { ISliderProps, Slider as FluentUiSlider } from '@fluentui/react' | ||
import { ThemeProvider, PartialTheme } from '@fluentui/react/lib/Theme' | ||
import { Typography } from '..' | ||
|
||
const dateFormatter = (epoch: number) => { | ||
const date = new Date(epoch) | ||
const year = date.getFullYear() | ||
const month = (date.getMonth() + 1).toString().padStart(2, '0') | ||
const day = date.getDate().toString().padStart(2, '0') | ||
const hours = date.getHours().toString().padStart(2, '0') | ||
const minutes = date.getMinutes().toString().padStart(2, '0') | ||
const seconds = date.getSeconds().toString().padStart(2, '0') | ||
|
||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}` | ||
} | ||
|
||
export const RangeSliderThemed: React.VFC<ISliderProps> = props => { | ||
const practicalTheme = useTheme() | ||
const fluentUiTheme: PartialTheme = useMemo( | ||
() => ({ | ||
palette: { | ||
themePrimary: practicalTheme.color.elementPrimary().toString(), | ||
themeLighter: practicalTheme.color.element13().toString(), // track focus | ||
themeDark: practicalTheme.color.elementPrimary().toString(), | ||
neutralTertiaryAlt: practicalTheme.color.element13().toString(), // track blur | ||
neutralSecondary: practicalTheme.color.elementPrimary().toString(), | ||
white: practicalTheme.color.backgroundPrimary().toString(), | ||
}, | ||
}), | ||
[practicalTheme] | ||
) | ||
|
||
return ( | ||
<ThemeProvider theme={fluentUiTheme} applyTo="none"> | ||
<FluentUiSlider {...props} /> | ||
</ThemeProvider> | ||
) | ||
} | ||
|
||
const ValuesLabelWrapper = styled.div` | ||
display: flex; | ||
justify-content: space-between; | ||
` | ||
|
||
export const RangeSliderField: React.VFC<ISliderProps> = props => { | ||
const { lowerValue, value } = props | ||
|
||
if (lowerValue === undefined || value === undefined) { | ||
throw new Error('lowerValue and value must be set') | ||
} | ||
|
||
return ( | ||
<> | ||
<Typography>{props.label}</Typography> | ||
<RangeSliderThemed {...props} label={undefined} /> | ||
<ValuesLabelWrapper> | ||
<Typography> | ||
{/* Start value label */} | ||
{dateFormatter(lowerValue)} | ||
</Typography> | ||
<Typography> | ||
{/* Stop value label */} | ||
{dateFormatter(value)} | ||
</Typography> | ||
</ValuesLabelWrapper> | ||
</> | ||
) | ||
} | ||
|
||
/** Range Slider with DateTime values in epoch. */ | ||
export const RangeSliderDemo: React.VFC = () => { | ||
const start = '2022-03-10 08:00:00' | ||
const stop = '2022-03-14 12:40:00' | ||
|
||
const epochStart = Date.parse(start) | ||
const epochStop = Date.parse(stop) | ||
|
||
const [sliderValue, setSliderValue] = useState([ | ||
epochStart + 50000000, // calculate this value, or better set it in the demo and not in the component | ||
epochStop - 50000000, | ||
]) | ||
|
||
const sliderOnChange = useCallback( | ||
(_: number, range: [number, number] | undefined) => { | ||
if (range === undefined) { | ||
// handle error? throw? | ||
return | ||
} | ||
setSliderValue(range) | ||
}, | ||
[] | ||
) | ||
|
||
return ( | ||
<RangeSliderField | ||
label="A range slider imported from Fluent UI" | ||
ranged={true} | ||
min={epochStart} | ||
max={epochStop} | ||
lowerValue={sliderValue[0]} | ||
value={sliderValue[1]} | ||
showValue={false} | ||
onChange={sliderOnChange} | ||
step={1000 * 60} // depending on the range, adjust to steps in a reasonable size, so moving knob with keys doesn't take to long | ||
/> | ||
) | ||
} |
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