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

I've made a change to use this component with useForm. Here's the code — I hope it helps some developers working with useForm. #19

Open
im4nu opened this issue Nov 29, 2024 · 0 comments

Comments

@im4nu
Copy link

im4nu commented Nov 29, 2024

First, this time-picker with date is awesome. The UX can be very improved using this component. Thank u Maximilian Kaske, Aaron Green, eliassebastian, Nicolas Cuellar Ochoa, Thibault Le Ouay, Ishan.

Now let me explain my usage.

// Using on main component

{...}
              <Controller
                control={control}
                name="endDate"
                render={({ field }) => (
                  <DateTimePicker
                    value={getElectionByIdQuery?.endDate ?? undefined}
                    onChange={field.onChange}
                  />
                )}
              />
{...}

// The modified component

"use client";

import { format, set } from "date-fns";
import { ptBR } from "date-fns/locale";
import { Calendar as CalendarIcon } from "lucide-react";
import * as React from "react";
import { Button } from "~/components/ui/button";
import { Calendar } from "~/components/ui/calendar";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "~/components/ui/popover";
import { cn } from "~/lib/utils";
import { TimePickerDemo } from "./time-picker-demo";

interface DateTimePickerProps {
  onChange: (date: Date) => void;
  value?: Date;
}

export function DateTimePicker({ onChange, value }: DateTimePickerProps) {
  const [date, setDate] = React.useState<Date>();

  function toggleSelect() {
    if (!date) return;
    onChange(date);
  }

  React.useEffect(() => {
    if (value) {
      setDate(value);
    }
  }, [value]);
  return (
    <Popover
      onOpenChange={(current) => {
        if (!current) {
          toggleSelect();
        }
      }}
    >
      <PopoverTrigger asChild>
        <Button
          variant={"outline"}
          className={cn(
            "w-[280px] justify-start text-left font-normal",
            !date && "text-muted-foreground",
          )}
        >
          <CalendarIcon className="mr-2 h-4 w-4" />
          {date ? (
            format(date, "dd/MM/yyyy | HH:mm")
          ) : (
            <span>Selecione uma data</span>
          )}
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-auto p-0">
        <Calendar
          mode="single"
          selected={date}
          onSelect={(d) => {
            setDate(d);
          }}
          initialFocus
          locale={ptBR}
        />
        <div className="flex justify-between border-t border-border p-3">
          <TimePickerDemo
            setDate={(d) => {
              setDate((currentDate) => {
                if (!currentDate) return;
                if (!d) return;

                // console.log({ d, currentDate });

                return set(currentDate, {
                  hours: d.getHours(),
                  minutes: d.getMinutes(),
                });
              });
            }}
            date={date}
          />
        </div>
      </PopoverContent>
    </Popover>
  );
}
@im4nu im4nu changed the title I've made a change to use this component with useForm, follow the code. I hope it's help some useForm devs. I've made a change to use this component with useForm, follow the code. I hope it helps some useForm devs. Nov 29, 2024
@im4nu im4nu changed the title I've made a change to use this component with useForm, follow the code. I hope it helps some useForm devs. I've made a change to use this component with useForm. Here's the code — I hope it helps some developers working with useForm. Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant