Skip to content

Commit

Permalink
Merge pull request #47 from AplinkosMinisterija/39-izuvinimo-laiko-au…
Browse files Browse the repository at this point in the history
…tomatinio-pasirinkimo-pakeitimai

39 izuvinimo laiko automatinio pasirinkimo pakeitimai
  • Loading branch information
DovMa authored Jan 24, 2024
2 parents 39a6f6d + b280e28 commit 6719aa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/components/fields/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const Datepicker = ({
placeholder="2000-01-01"
className={className}
onChange={handleChange}
onInputClick={() => setOpen(!open)}
label={label}
padding={padding}
value={textValue}
Expand Down
31 changes: 25 additions & 6 deletions src/components/fields/TimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { format } from 'date-fns';
import lt from 'date-fns/locale/lt';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import DatePicker, { registerLocale } from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css';
import styled from 'styled-components';
Expand Down Expand Up @@ -33,6 +33,27 @@ const TimePicker = ({
maxDate,
}: TimepickerProps) => {
const [open, setOpen] = useState(false);
const [time, setTime] = useState<Date | null>(null);

const getTimeInterval = (time?: Date) => {
if (!time) {
return null;
}
const minutes = Math.ceil(time.getMinutes() / 30) * 30; //result = 30 | 60 min
const hours = time.getHours();
if (minutes === 60 && hours === 23) {
return null; //if almost midnight, return null as there is no available time option today.
}
return new Date(time.setMinutes(minutes, 0, 0)); //adds 1 hour if minutes = 60;
};

useEffect(() => {
if (!value) {
setTime(null);
} else {
setTime(getTimeInterval(value));
}
}, [value]);

const handleBlur = (event: any) => {
if (!event.currentTarget.contains(event.relatedTarget)) {
Expand Down Expand Up @@ -64,7 +85,7 @@ const TimePicker = ({
showError={true}
label={label}
padding={padding}
value={value ? format(new Date(value), 'HH:mm') : ''}
value={time ? format(new Date(time), 'HH:mm') : ''}
error={error}
rightIcon={<TimeIcon name={'time'} />}
disabled={disabled}
Expand All @@ -79,21 +100,19 @@ const TimePicker = ({
{...(minDate ? { minDate: new Date(minDate) } : {})}
showTimeSelectOnly
timeIntervals={30}
selected={value ? new Date(value as any) : null}
selected={time}
onChange={(date: Date) => {
if (maxDate && date > new Date(maxDate)) {
return onChange(maxDate);
}

if (minDate && date < new Date(minDate)) {
return onChange(minDate);
}

onChange(date);
setOpen(false);
}}
inline
></DatePicker>
/>
) : null}
</TimeContainer>
);
Expand Down

0 comments on commit 6719aa4

Please sign in to comment.