Skip to content

Commit

Permalink
Update new date field value format and added value preview (#2682)
Browse files Browse the repository at this point in the history
Update new date field value format and added value preview


![image](https://github.com/zesty-io/manager-ui/assets/28705606/7afd5cc6-4bd0-4261-b810-9d3c681e04b0)
  • Loading branch information
finnar-bin authored Apr 17, 2024
1 parent 465efe0 commit f34693f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
26 changes: 7 additions & 19 deletions src/apps/content-editor/src/app/components/Editor/Field/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -889,32 +889,20 @@ export const Field = ({
);

case "date":
/**
* Every time this parent compenent re-renders it creates a new function
* invalidating the FieldTypeData components referential prop check,
* causing it to re-render as well. This `onChange` handler doesn't need
* to change once created.
*/
const onDateChange = useCallback(
(value, name, datatype) => {
/**
* Flatpickr emits a utc timestamp, offset from users local time.
* Legacy behavior did not send utc but sent the value as is selected by the user
* this ensures that behavior is maintained.
*/
onChange(value, name, datatype);
},
[onChange]
);

return (
<FieldShell settings={fieldData} errors={errors}>
<FieldTypeDate
name={name}
required={required}
value={value ? moment(value).toDate() : null}
// format="MMM dd, yyyy"
onChange={(date) => onDateChange(date, name, datatype)}
onChange={(date) => {
onChange(
date ? moment(date).format("yyyy-MM-DD") : null,
name,
datatype
);
}}
error={errors && Object.values(errors)?.some((error) => !!error)}
/>
</FieldShell>
Expand Down
17 changes: 16 additions & 1 deletion src/shell/components/FieldTypeDate/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Button from "@mui/material/Button";
import { Typography, Stack, Box, TextField } from "@mui/material";
import format from "date-fns/format";
import CalendarTodayRoundedIcon from "@mui/icons-material/CalendarTodayRounded";
import moment from "moment";

export interface FieldTypeDateProps extends DatePickerProps<Date> {
name: string;
Expand All @@ -18,6 +19,7 @@ export interface FieldTypeDateProps extends DatePickerProps<Date> {
timePicker?: React.ReactNode;
};
onClear?: () => void;
valueFormatPreview?: string;
}

const parseDateInput = (input: string): Date | null => {
Expand Down Expand Up @@ -56,7 +58,14 @@ const parseDateInput = (input: string): Date | null => {
};

export const FieldTypeDate = memo(
({ required, error, slots, onClear, ...props }: FieldTypeDateProps) => {
({
required,
error,
slots,
onClear,
valueFormatPreview,
...props
}: FieldTypeDateProps) => {
const textFieldRef = useRef<HTMLInputElement>(null);
const [isOpen, setIsOpen] = useState(false);

Expand Down Expand Up @@ -195,6 +204,12 @@ export const FieldTypeDate = memo(
Clear
</Button>
</Stack>
{(valueFormatPreview || props.value) && (
<Typography variant="body3" color="text.secondary" sx={{ mt: 0.5 }}>
Stored as{" "}
{valueFormatPreview ?? moment(props.value).format("yyyy-MM-DD")}
</Typography>
)}
</LocalizationProvider>
);
}
Expand Down
8 changes: 3 additions & 5 deletions src/shell/components/FieldTypeDateTime/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export const FieldTypeDateTime = ({
name={name}
required={required}
value={dateString ? moment(dateString).toDate() : null}
valueFormatPreview={
dateString && timeString ? `${dateString} ${timeString}` : null
}
onChange={(date) => {
if (date) {
onChange(
Expand Down Expand Up @@ -222,11 +225,6 @@ export const FieldTypeDateTime = ({
),
}}
/>
{dateString && timeString && (
<Typography variant="body3" color="text.secondary" sx={{ mt: 0.5 }}>
Stored as {dateString} {timeString}
</Typography>
)}
</>
);
};

0 comments on commit f34693f

Please sign in to comment.