-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Feature request: Time picker #255
Comments
Hi everyone, as we wait for an official DateTime picker for the library, I decided to re-create this using React Aria's See the demo below: Screen.Recording.2023-06-01.at.2.47.23.PM.movWould this be of interest to any of you? I can move this to a repo/codesandbox. I ran in to a "focus" problem with React Aria that requires two clicks to close the popover though (since they manually control event propagation and I guess Radix needs it to trigger the popover to close). As a workaround, I'm using another Aria hook called |
That looks very nice, does the time picker has to go with date picker? can it be separate like mantine.dev time picker? |
Thanks! I ended up creating a separate component for a TimePicker too since having a Popover for just the time felt excessive. This is what it looks like. With the DateTime picker, you can select the Screen.Recording.2023-06-01.at.7.30.09.PM.mov |
Lets see what they're planning for a release next week and if it isn't something related to a time picker, I'll spend some time extracting this out in to a separate repo for everyone to use/contribute to until then. |
Do you already have this on repo/codesandbox ser? |
I got caught up on a separate project - I will share a repo/sandbox by end of the weekend! |
Thanks for the swift response ser! It'd be great if you can get it there
sooner!
…On Thu, Jun 15, 2023, 11:33 PM Jordan Lewallen ***@***.***> wrote:
I got caught up on a separate project - I will share a sandbox by end of
the weekend!
—
Reply to this email directly, view it on GitHub
<#255 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOAV32GR52ZCN7N76PZESMTXLMTMXANCNFSM6AAAAAAXQGF6DI>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Hi everyone - as promised here is a repository that demo's react-aria's EDIT: I also don't use NextJS App Router in my projects so I just threw "use client" everywhere and hoped for the best lol Hope it helps as a starting point :) |
This is a nice surprise for the Father's Day. :) Well done. |
+1 To this... |
@uncvrd |
so
Not sure if this is the best approach, but since the date can be See instructions on Let me know if y'all have better implementations! |
This is really nice @uncvrd, thank you! I can't seem to figure out how to just disregard the timezone, so instead I've done this: <DateTimePicker
onBlur={field.onBlur}
value={
!!field.value
? parseAbsolute(field.value.toISOString(), "GMT")
: null
}
onChange={(date) => {
field.onChange(!!date ? date.toDate("GMT") : new Date());
}}
granularity="minute"
/> Do you have any idea how I could instead just use Super helpful component!! |
What was released? I'm having trouble finding something related to a time picker. |
@wasauce nothing related to a date picker - it was the new CLI which is why I shared my own implementation for now |
@uncvrd it looks wonderful. going to dive in. Thank you! |
How to use in form ? |
For anyone like myself wanting a simple (interim) solution, you can use |
Here's a simple one that re-uses shadcn's calendar component. I stole the time field from @uncvrd. Screen.Recording.2023-10-25.at.10.22.05.mov |
this is breaking when used with forms :( @zacwellmer |
This works great until you set an onChange event -- then it begins to close before you've had a chance to fill in all values. Example: I select a date, type in my hour, and begin to type in minutes, but it closes (loses focus) as soon as I've typed in a single number. If I wanted to type in "45", it only allows me to type in the first number before closing. I have to reopen it to type "5" and yet again to select "AM/PM". |
Reached this request while searching for this. Here's the implementation I settled. It uses timespace lib in case anyone is interested. const form = useForm(...)
<FormField
control={form.control}
{/*statTime field type is: Date */}
name="startTime"
render={({ field }) => (
<FormItem className="w-1/3">
<FormLabel>Label</FormLabel>
<FormControl>
{/*This is the component*/}
<TimePicker onChange={field.onChange} value={field.value}>
<TimePickerSegment segment={"hours"} />
<TimePickerSeparator>:</TimePickerSeparator>
<TimePickerSegment segment={"minutes"} />
</TimePicker>
</FormControl>
</FormItem>
)}
/> Component Source: //time-picker.tsx
import { type DateType, useTimescape } from "timescape/react";
import * as React from "react";
import {
createContext,
forwardRef,
type HTMLAttributes,
type ReactNode,
useContext,
} from "react";
import { cn } from "@/lib/utils/style";
import type { Options } from "timescape";
export type TimePickerProps = HTMLAttributes<HTMLDivElement> & {
value?: Date;
onChange: (date?: Date) => void;
children: ReactNode;
options?: Omit<Options, "date" | "onChangeDate">;
};
type TimePickerContextValue = ReturnType<typeof useTimescape>;
const TimePickerContext = createContext<TimePickerContextValue | null>(null);
const useTimepickerContext = (): TimePickerContextValue => {
const context = useContext(TimePickerContext);
if (!context) {
throw new Error(
"Unable to access TimePickerContext. This component should be wrapped by a TimePicker component",
);
}
return context;
};
const TimePicker = forwardRef<React.ElementRef<"div">, TimePickerProps>(
({ value, onChange, options, className, ...props }, ref) => {
const timePickerContext = useTimescape({
date: value,
onChangeDate: onChange,
...options,
});
return (
<TimePickerContext.Provider value={timePickerContext}>
<div
ref={ref}
{...props}
className={cn(
"flex w-auto h-10 rounded-md border border-input bg-background px-3 py-1 text-sm",
"ring-offset-background focus-within:outline-none focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2",
"disabled:cursor-not-allowed disabled:opacity-50",
className,
)}
></div>
</TimePickerContext.Provider>
);
},
);
TimePicker.displayName = "TimePicker";
type TimePickerSegmentProps = Omit<
HTMLAttributes<HTMLInputElement>,
"value" | "onChange"
> & {
segment: DateType;
inputClassName?: string;
};
const TimePickerSegment = forwardRef<
React.ElementRef<"input">,
TimePickerSegmentProps
>(({ segment, inputClassName, className, ...props }, ref) => {
const { getInputProps } = useTimepickerContext();
const { ref: timePickerInputRef } = getInputProps(segment);
return (
<div
className={cn(
"rounded-md focus-within:bg-accent text-accent-foreground px-2 py-1",
)}
>
<input
ref={(node) => {
if (typeof ref === "function") {
ref(node);
} else {
if (ref) ref.current = node;
}
timePickerInputRef(node);
}}
{...props}
className={cn(
"tabular-nums caret-transparent",
"bg-transparent ring-0 ring-offset-0 border-transparent focus-visible:border-transparent focus-visible:ring-0 outline-none",
inputClassName,
)}
/>
</div>
);
});
TimePickerSegment.displayName = "TimePickerSegment";
type TimePickerSeparatorProps = HTMLAttributes<HTMLSpanElement>;
const TimePickerSeparator = forwardRef<
React.ElementRef<"span">,
TimePickerSeparatorProps
>(({ className, ...props }, ref) => {
return (
<span ref={ref} {...props} className={cn("text-sm py-1", className)}></span>
);
});
TimePickerSeparator.displayName = "TimePickerSeparator";
export { TimePicker, TimePickerSegment, TimePickerSeparator }; PS: This could also be used for date and time. Check out timescape docs: https://github.com/dan-lee/timescape |
This is great. I have tried some sort of implementation like this but how would you store this into a Prisma? would it be a simple DateTime? and would MySQL accept this too? |
A time-picker would be great! For now we're just using a select, with time options in the 3hr range like so: This way of doing it has some issues though. For example we need custom logic to return the correct type instead of a string, and it also doesn't look that good, so a separate TimePicker component would be really nice to have. Would also be cool to see how it could be integrated into the existing DatePicker. |
For those looking for a super easy drop in solution that surprisingly themes well to shadcn automatically, here is an example using the native time selector as @danvoyce mentioned using react hook form and modifying the original date object so form submission is easy:
now that i think of it this could also be a variant element of the native Shadcn calendar too 😅 |
thanks to @uncvrd |
Found this component . Was simple and solved the purpose , It uses Shadcn. |
@hsuanyi-chou thanks for the credit ❤️ |
@liamlows is it possible to provide full implementation? with the imports, calendar and input files? |
Is there a way to convert this DateTimePicker value to ISO 8601 like : 2024-04-10 19:22:36.2213+00 ? I work with Supabase and Nextjs |
i think you can convert the Date object to your wish when assigning the value either on |
Screen.Recording.2024-05-19.at.6.40.12.PM.movHere's my spin on a basic date/time picker using |
|
i am using the component and it works fine when i input the time but I want it to display to the columns.tsx shadcn ui component using the data table in the format of HH:MMAM but instead it displays as HH:MM:SS in 24 hour format. how would you make it display? would it be a regular expression? |
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you. |
Please keep this issue open. It would be a very useful feature |
agree, i think it would be a great feature and we should keep it open |
You can try this simple date picker or a powerful datetime picker with options for timezone, month, year, and time selection. https://shadcn-datetime-picker-xi.vercel.app/ https://github.com/huybuidac/shadcn-datetime-picker Screen.Recording.2024-10-13.at.23.28.30.mov |
@huybuidac did you create a pull request with this component? it looks awesome |
Please open this back up this would be very useful |
You can checkout this datetime picker here Source Code: https://github.com/Maliksidk19/shadcn-datetime-picker URL: https://shadcn-datetime-picker.vercel.app/datetime-picker |
No description provided.
The text was updated successfully, but these errors were encountered: