|
| 1 | +/* eslint-disable react/no-unknown-property */ |
| 2 | +import { useField, useFormikContext } from 'formik'; |
| 3 | +import { Box, DropButton, Button, Text } from 'grommet'; |
| 4 | +import { Alert } from 'grommet-icons'; |
| 5 | +import { normalizeColor } from 'grommet/utils'; |
| 6 | +import React, { FunctionComponent, ReactElement, useState } from 'react'; |
| 7 | +import { defineMessages, useIntl } from 'react-intl'; |
| 8 | +import TimePicker, { TimePickerValue } from 'react-time-picker'; |
| 9 | +import { useTheme } from 'styled-components'; |
| 10 | + |
| 11 | +const messages = defineMessages({ |
| 12 | + invalidTime: { |
| 13 | + defaultMessage: 'Input time is not valid: it should be set in the future.', |
| 14 | + description: 'Error message when event scheduling time update is in the past.', |
| 15 | + id: 'components.design-system.Formik.FormikTimePicker.invalidTime', |
| 16 | + }, |
| 17 | +}); |
| 18 | + |
| 19 | +export interface FormikTimePickerProps { |
| 20 | + name: string; |
| 21 | + isToday: boolean; |
| 22 | + onChange: (event: React.ChangeEvent<HTMLInputElement>) => void; |
| 23 | +} |
| 24 | + |
| 25 | +const computeSuggestions = (): string[] => { |
| 26 | + let suggestions = ['00:00']; |
| 27 | + let minutes: string = '00'; |
| 28 | + let hours: string = '00'; |
| 29 | + |
| 30 | + while (`${hours}:${minutes}` < '23:45') { |
| 31 | + console.log(`${hours}:${minutes}`); |
| 32 | + if (minutes == '45') { |
| 33 | + hours = +hours >= 9 ? (+hours + 1).toString() : `0${(+hours + 1).toString()}`; |
| 34 | + minutes = '00'; |
| 35 | + } else { |
| 36 | + minutes = (+minutes + 15).toString(); |
| 37 | + } |
| 38 | + suggestions.push(`${hours}:${minutes}`); |
| 39 | + } |
| 40 | + return suggestions; |
| 41 | +}; |
| 42 | + |
| 43 | +const allSuggestions = computeSuggestions(); |
| 44 | + |
| 45 | +interface suggestionButtonProps { |
| 46 | + buttonValue: string; |
| 47 | + choiceValue: string; |
| 48 | + onClick: (value: string) => void; |
| 49 | +} |
| 50 | + |
| 51 | +const SuggestionButton: FunctionComponent<suggestionButtonProps> = ({ ...props }): ReactElement => { |
| 52 | + const isChosenButton: boolean = props.buttonValue == props.choiceValue; |
| 53 | + const theme = useTheme(); |
| 54 | + return ( |
| 55 | + <Button |
| 56 | + color={isChosenButton ? `${normalizeColor('light-2', theme)}` : 'black'} |
| 57 | + fill={isChosenButton ? 'horizontal' : false} |
| 58 | + justify="center" |
| 59 | + margin={{ top: 'xsmall' }} |
| 60 | + primary={isChosenButton} |
| 61 | + onClick={() => { |
| 62 | + props.onClick(props.buttonValue); |
| 63 | + }} |
| 64 | + > |
| 65 | + <Box alignContent="center" alignSelf="center"> |
| 66 | + <Text color="black" textAlign="center"> |
| 67 | + {props.buttonValue} |
| 68 | + </Text> |
| 69 | + </Box> |
| 70 | + </Button> |
| 71 | + ); |
| 72 | +}; |
| 73 | + |
| 74 | +const FormikTimePicker: FunctionComponent<FormikTimePickerProps> = ({ ...props }) => { |
| 75 | + const [field] = useField(props.name); |
| 76 | + const formikContext = useFormikContext(); |
| 77 | + const [open, setOpen] = useState<boolean | undefined>(undefined); |
| 78 | + const [timeError, setTimeError] = useState(false); |
| 79 | + const intl = useIntl(); |
| 80 | + |
| 81 | + const onTimeChange = (value: TimePickerValue | string) => { |
| 82 | + const today = new Date(); |
| 83 | + if (props.isToday && value < `${today.getHours()}:${today.getMinutes()}`) { |
| 84 | + setTimeError(true); |
| 85 | + } else { |
| 86 | + setTimeError(false); |
| 87 | + formikContext.setFieldValue(props.name, value); |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + const onTimeSelectChange = (value: string) => { |
| 92 | + onTimeChange(value); |
| 93 | + setOpen(false); |
| 94 | + }; |
| 95 | + |
| 96 | + const suggestionButtons = allSuggestions.map((value: string) => ( |
| 97 | + <SuggestionButton |
| 98 | + key={value} |
| 99 | + buttonValue={value} |
| 100 | + choiceValue={field.value} |
| 101 | + onClick={onTimeSelectChange} |
| 102 | + /> |
| 103 | + )); |
| 104 | + |
| 105 | + return ( |
| 106 | + <Box align="start" pad="small"> |
| 107 | + <DropButton |
| 108 | + dropAlign={{ top: 'bottom' }} |
| 109 | + onClose={() => setOpen(false)} |
| 110 | + open={open} |
| 111 | + dropContent={ |
| 112 | + <Box align="center" basis="small" direction="column" gap="5px"> |
| 113 | + {suggestionButtons} |
| 114 | + </Box> |
| 115 | + } |
| 116 | + onOpen={() => { |
| 117 | + setOpen(true); |
| 118 | + }} |
| 119 | + > |
| 120 | + <TimePicker disableClock onChange={onTimeChange} value={field.value}></TimePicker> |
| 121 | + </DropButton> |
| 122 | + {timeError && ( |
| 123 | + <Box |
| 124 | + align="center" |
| 125 | + background="#ffcccb" |
| 126 | + direction="row" |
| 127 | + gap="8px" |
| 128 | + margin={{ top: 'small' }} |
| 129 | + pad="8px" |
| 130 | + round="small" |
| 131 | + > |
| 132 | + <Alert color="status-critical" size="medium" /> |
| 133 | + <Text color="status-critical" size="small"> |
| 134 | + {intl.formatMessage(messages.invalidTime)} |
| 135 | + </Text> |
| 136 | + </Box> |
| 137 | + )} |
| 138 | + </Box> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +export default FormikTimePicker; |
0 commit comments