-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(TimePicker-compat): add story for custom parsing from time stri…
…ng to date (#29707) * story * typefix * rename to parseTimeStringToDate * Update packages/react-components/react-timepicker-compat-preview/stories/TimePicker/TimePickerCustomParsingAndValidation.stories.tsx Co-authored-by: ling1726 <[email protected]> * story update * chglog --------- Co-authored-by: ling1726 <[email protected]>
- Loading branch information
1 parent
8a1688f
commit ff6d3f1
Showing
10 changed files
with
92 additions
and
53 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-timepicker-compat-preview-290fcc1b-351b-4d9b-8389-0acf4a709294.json
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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: rename `formatTimeStringToDate` to `parseTimeStringToDate`.", | ||
"packageName": "@fluentui/react-timepicker-compat-preview", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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
40 changes: 0 additions & 40 deletions
40
...react-timepicker-compat-preview/stories/TimePicker/TimePickerCustomTimeString.stories.tsx
This file was deleted.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
...timepicker-compat-preview/stories/TimePicker/TimePickerFreeformCustomParsing.md
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,4 @@ | ||
This story sets custom time string in the dropdown options, and performs custom parsing from the input text to Date object on selection. | ||
|
||
- The time display format in the dropdown can be customized using `formatDateToTimeString`. | ||
- Freefrom TimePicker can have custom parsing for input text to Date object using `parseTimeStringToDate`. |
68 changes: 68 additions & 0 deletions
68
...-timepicker-compat-preview/stories/TimePicker/TimePickerFreeformCustomParsing.stories.tsx
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,68 @@ | ||
import * as React from 'react'; | ||
import { Field, makeStyles } from '@fluentui/react-components'; | ||
import { TimePicker, TimePickerProps } from '@fluentui/react-timepicker-compat-preview'; | ||
import story from './TimePickerFreeformCustomParsing.md'; | ||
|
||
const useStyles = makeStyles({ | ||
root: { | ||
maxWidth: '300px', | ||
}, | ||
}); | ||
|
||
const formatDateToTimeString = (date: Date) => { | ||
const localeTimeString = date.toLocaleTimeString([], { | ||
hour: 'numeric', | ||
minute: '2-digit', | ||
hourCycle: 'h12', | ||
}); | ||
if (date.getHours() < 12) { | ||
return `Morning: ${localeTimeString}`; | ||
} | ||
return `Afternoon: ${localeTimeString}`; | ||
}; | ||
|
||
export const FreeformCustomParsing = () => { | ||
const styles = useStyles(); | ||
|
||
const [anchor] = React.useState(() => new Date(2023, 1, 1)); | ||
|
||
const parseTimeStringToDate: TimePickerProps['parseTimeStringToDate'] = (time: string | undefined) => { | ||
if (!time) { | ||
return { date: null }; | ||
} | ||
|
||
const [hours, minutes] = (time.split(' ')[1].match(/\d+/g) ?? []).map(Number); | ||
const adjustedHours = time.includes('Afternoon: ') && hours !== 12 ? hours + 12 : hours; | ||
const date = new Date(anchor.getFullYear(), anchor.getMonth(), anchor.getDate(), adjustedHours, minutes); | ||
|
||
return { date }; | ||
}; | ||
|
||
const [selectedTimeText, setSelectedTimeText] = React.useState<string | undefined>(undefined); | ||
const onTimeChange: TimePickerProps['onTimeChange'] = (_ev, data) => { | ||
setSelectedTimeText(data.selectedTimeText); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Field label="Coffee time" className={styles.root}> | ||
<TimePicker | ||
freeform | ||
dateAnchor={anchor} | ||
formatDateToTimeString={formatDateToTimeString} | ||
parseTimeStringToDate={parseTimeStringToDate} | ||
onTimeChange={onTimeChange} | ||
/> | ||
</Field> | ||
{selectedTimeText && <div>Selected time: {selectedTimeText}</div>} | ||
</div> | ||
); | ||
}; | ||
|
||
FreeformCustomParsing.parameters = { | ||
docs: { | ||
description: { | ||
story, | ||
}, | ||
}, | ||
}; |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...TimePicker/TimePickerFreeform.stories.tsx → ...ckerFreeformWithErrorHandling.stories.tsx
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