Skip to content

Commit

Permalink
fix: date bug
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstefanequilobe committed Jul 1, 2022
1 parent 8eb62d8 commit 0434f3d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 120 deletions.
211 changes: 94 additions & 117 deletions src/components/blocks/DateSelect.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,117 @@
import React, { useRef } from 'react';
// import { LocalizationProvider } from '@mui/lab';
import React from 'react';
import DateAdapter from '@mui/lab/AdapterDayjs';
// import DatePicker from '@mui/lab/DatePicker';
import { DatePicker, LocalizationProvider } from '@mui/x-date-pickers';
import styled, { css } from 'styled-components';
import { LocalizationProvider } from '@mui/x-date-pickers';
import styled, { css, withTheme } from 'styled-components';
import TextField from '@mui/material/TextField';
import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';

import { formatDate, getISODate } from '../../utils/formatData';
import { DateVariantEnum } from '.';
import { getIsDateValid } from '../../utils/dateUtils';

const InputContainer = styled('div')`
display: flex;
align-items: center;
justify-content: center;
border: 1px solid #d9d9d9;
width: 160px;
border-radius: 2px;
background-color: white;
:hover {
border: 1px solid #40a9ff;
${props =>
props.disabled &&
css`
border: 1px solid #d9d9d9;
`};
}
:focus-within,
::selection {
outline: none;
box-shadow: ${props =>
props.disabled ? 'none' : '0px 0px 4px rgba(24, 144, 255, 0.5)'};
border: ${props =>
props.disabled ? '1px solid #d9d9d9;' : '1px solid #40a9ff'};
export const DateSelectVariantEnum = {
error: 'error',
};

const StyledTextField = styled(TextField)`
width: 100%;
// set height depending on props
.MuiOutlinedInput-root,
.MuiInputBase-root {
height: ${props => {
if (props.size === 'large') return '40px';
if (props.size === 'small') return '24px';
return '32px';
}};
border-radius: 0.125rem;
border: 0.0625rem solid #d9d9d9;
}
${props =>
(props.size === 'large' &&
css`
height: 40px;
`) ||
(props.size === 'default' &&
css`
height: 32px;
`) ||
(props.size === 'small' &&
css`
height: 24px;
`)};
// hover colors
.MuiOutlinedInput-root:hover,
.MuiInputBase-root:hover {
border: 1px solid #40a9ff;
}
// error variant border
${props =>
props.disabled &&
props.dateselectvariant === DateSelectVariantEnum.error &&
css`
background-color: #f5f5f5;
`};
${props => {
if (props.variant === DateVariantEnum.error) {
return css`
.MuiOutlinedInput-root,
.MuiInputBase-root {
border: 1px solid ${props.theme.colors.default.status.error.primary};
:focus-within {
border: 1px solid ${props.theme.colors.default.status.error.primary};
box-shadow: 0px 0px 4px rgba(245, 34, 45, 0.5);
}
:hover {
border: 1px solid ${props.theme.colors.default.status.error.primary};
}
`;
}
}}
`;
}
`}
const Input = styled('input')`
height: 22px;
width: 96px;
border: none;
:focus-visible {
outline: none;
}
${props =>
// disabled background color
${props =>
props.disabled &&
css`
background-color: #f5f5f5;
color: ${props.theme.colors.default.onSurface};
cursor: default;
`};
.MuiOutlinedInput-root,
.MuiInputBase-root {
background-color: #f5f5f5;
}
`}
// remove inner mui border
.MuiOutlinedInput-notchedOutline {
border: 0;
}
`;

const DateSelect = ({
size = 'default',
disabled,
dateValue,
setDateValue,
variant,
onBlur,
name,
}) => {
const divElement = useRef(null);
// '& label.Mui-focused': {
// color: 'green',
// },
// '& .MuiInput-underline:after': {
// borderBottomColor: 'green',
// },
// '& .MuiOutlinedInput-root': {
// '& fieldset': {
// borderColor: 'red',
// },
// '&:hover fieldset': {
// borderColor: 'yellow',
// },
// '&.Mui-focused fieldset': {
// borderColor: 'green',
// },
// },

return (
const DateSelect = withTheme(
({
size = 'default',
disabled,
dateValue,
setDateValue,
variant,
onBlur,
name,
}) => (
<LocalizationProvider dateAdapter={DateAdapter}>
<DatePicker
<DesktopDatePicker
inputFormat="YYYY-MM-DD"
disableOpenPicker={disabled}
label="Select time"
value={getISODate(dateValue)}
onChange={newValue => {
if (newValue) {
setDateValue(formatDate(newValue));
} else {
setDateValue(null);
}
}}
mask="____-__-__"
RegExp="/^d{4}-d{2}-d{2}$/"
views={['year', 'month', 'day']}
value={getIsDateValid(dateValue) ? getISODate(dateValue) : null}
onChange={newValue =>
getIsDateValid(newValue)
? setDateValue(formatDate(newValue))
: setDateValue(newValue)
}
disabled={disabled}
renderInput={({ inputRef, inputProps, InputProps }) => {
return (
<InputContainer
size={size}
ref={divElement}
disabled={disabled}
tabIndex={0}
variant={variant}
>
<Input
ref={inputRef}
{...inputProps}
placeholder="YYYY-MM-DD"
disabled={disabled}
onBlur={onBlur}
name={name}
/>
{InputProps?.endAdornment}
</InputContainer>
);
}}
renderInput={params => (
<StyledTextField
{...params}
disabled={disabled}
size={size}
dateselectvariant={variant}
name={name}
onBlur={onBlur}
/>
)}
/>
</LocalizationProvider>
);
};
),
);

export { DateSelect };
6 changes: 3 additions & 3 deletions src/components/forms/ProjectDetailsForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
BodyContainer,
Body,
DateSelect,
DateVariantEnum,
DateSelectVariantEnum,
DescriptionIcon,
ToolTipContainer,
LabelContainer,
Expand Down Expand Up @@ -373,7 +373,7 @@ const ProjectDetailsForm = () => {
variant={
errors.projectStatusDate &&
touched.projectStatusDate &&
DateVariantEnum.error
DateSelectVariantEnum.error
}
size="large"
dateValue={values.projectStatusDate}
Expand Down Expand Up @@ -770,7 +770,7 @@ const ProjectDetailsForm = () => {
variant={
errors.validationDate &&
touched.validationDate &&
DateVariantEnum.error
DateSelectVariantEnum.error
}
size="large"
dateValue={values.validationDate}
Expand Down
1 change: 1 addition & 0 deletions src/utils/dateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getIsDateValid = date => !isNaN(Date.parse(date));

0 comments on commit 0434f3d

Please sign in to comment.