Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TimeRangeInput derive state instead of syncing #318

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/ui/src/components/time-range-input.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Meta, StoryObj } from "@storybook/react";
import { useState } from "react";
import { TimeRangeInput } from "./time-range-input";

const meta: Meta<typeof TimeRangeInput> = {
component: () => {
const [value, setValue] = useState({ start: "", end: "" });
return (
<TimeRangeInput
value={value}
onChange={(value) => {
setValue(value);
}}
/>
);
},
};

export default meta;

type Story = StoryObj<typeof TimeRangeInput>;

export const Default: Story = {};
20 changes: 6 additions & 14 deletions packages/ui/src/components/time-range-input.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { differenceInMinutes, parse } from "date-fns";
import { useEffect, useState } from "react";
import { useMemo } from "react";
import { Icons } from "./icons";

export function TimeRangeInput({
Expand All @@ -11,26 +11,20 @@ export function TimeRangeInput({
value: { start: string; end: string };
onChange: (value: { start: string; end: string }) => void;
}) {
const [startTime, setStartTime] = useState(value.start);
const [endTime, setEndTime] = useState(value.end);
const [duration, setDuration] = useState("");
const startTime = value.start;
const endTime = value.end;

useEffect(() => {
setStartTime(value.start);
setEndTime(value.end);
}, [value]);

useEffect(() => {
const duration = useMemo(() => {
if (!startTime || !endTime) {
return;
return "";
}

const start = parse(startTime, "HH:mm", new Date());
const end = parse(endTime, "HH:mm", new Date());
const diff = differenceInMinutes(end, start);
const hours = Math.floor(diff / 60);
const minutes = diff % 60;
setDuration(`${hours}h ${minutes}min`);
return `${hours}h ${minutes}min`;
}, [startTime, endTime]);

return (
Expand All @@ -41,7 +35,6 @@ export function TimeRangeInput({
type="time"
value={startTime}
onChange={(e) => {
setStartTime(e.target.value);
onChange({ ...value, start: e.target.value });
}}
className="bg-transparent focus:outline-none text-sm"
Expand All @@ -55,7 +48,6 @@ export function TimeRangeInput({
type="time"
value={endTime}
onChange={(e) => {
setEndTime(e.target.value);
onChange({ ...value, end: e.target.value });
}}
className="bg-transparent focus:outline-none text-sm"
Expand Down