-
Notifications
You must be signed in to change notification settings - Fork 136
3827 client - Add Trigger Form components for form-based workflows #3829
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
63a6b70
3827 client - Add Trigger Form components for form-based workflows
ivicac 14d9dec
3827 client - Add `DateTimePicker` component for selecting date and time
ivicac 663ef01
3827 client - Add feature flag
ivicac 1f30e82
3827 client - Additional fixes
ivicac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
client/src/components/DateTimePicker/DateTimePicker.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| import '@testing-library/jest-dom'; | ||
| import {fireEvent, render, screen, userEvent} from '@/shared/util/test-utils'; | ||
| import {format} from 'date-fns'; | ||
| import {describe, expect, it, vi} from 'vitest'; | ||
|
|
||
| import DateTimePicker from './DateTimePicker'; | ||
|
|
||
| describe('DateTimePicker', () => { | ||
| it('should render the trigger button with placeholder when no value is provided', () => { | ||
| render(<DateTimePicker onChange={vi.fn()} />); | ||
|
|
||
| expect(screen.getByText('Pick a date and time')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render the trigger button with formatted date when value is provided', () => { | ||
| const date = new Date(2023, 11, 25, 10, 30); | ||
|
|
||
| render(<DateTimePicker onChange={vi.fn()} value={date} />); | ||
|
|
||
| expect(screen.getByText(format(date, 'PPP HH:mm'))).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should open popover and allow date selection', async () => { | ||
| const onChange = vi.fn(); | ||
| const initialDate = new Date(2023, 11, 25, 10, 30); | ||
|
|
||
| render(<DateTimePicker onChange={onChange} value={initialDate} />); | ||
|
|
||
| const trigger = screen.getByRole('button'); | ||
|
|
||
| await userEvent.click(trigger); | ||
|
|
||
| // Calendar should be visible. | ||
| // We look for a day, e.g., 26th. | ||
| const day26 = screen.getByText('26'); | ||
|
|
||
| await userEvent.click(day26); | ||
|
|
||
| expect(onChange).toHaveBeenCalled(); | ||
|
|
||
| const calledDate = onChange.mock.calls[0][0]; | ||
|
|
||
| expect(calledDate.getDate()).toBe(26); | ||
| // Time should be preserved | ||
| expect(calledDate.getHours()).toBe(10); | ||
| expect(calledDate.getMinutes()).toBe(30); | ||
| }); | ||
|
|
||
| it('should allow time change', async () => { | ||
| const onChange = vi.fn(); | ||
| const initialDate = new Date(2023, 11, 25, 10, 30); | ||
|
|
||
| render(<DateTimePicker onChange={onChange} value={initialDate} />); | ||
|
|
||
| await userEvent.click(screen.getByRole('button')); | ||
|
|
||
| const timeInput = document.querySelector('input[type="time"]') as HTMLInputElement; | ||
|
|
||
| fireEvent.change(timeInput, {target: {value: '15:45'}}); | ||
|
|
||
| expect(onChange).toHaveBeenCalled(); | ||
|
|
||
| const calledDate = onChange.mock.calls[0][0]; | ||
|
|
||
| expect(calledDate.getHours()).toBe(15); | ||
| expect(calledDate.getMinutes()).toBe(45); | ||
| // Date should be preserved | ||
| expect(calledDate.getDate()).toBe(25); | ||
| }); | ||
|
|
||
| it('should ignore invalid time inputs', async () => { | ||
| const onChange = vi.fn(); | ||
| const initialDate = new Date(2023, 11, 25, 10, 30); | ||
|
|
||
| render(<DateTimePicker onChange={onChange} value={initialDate} />); | ||
|
|
||
| await userEvent.click(screen.getByRole('button')); | ||
|
|
||
| const timeInput = document.querySelector('input[type="time"]') as HTMLInputElement; | ||
|
|
||
| // Invalid hours | ||
| fireEvent.change(timeInput, {target: {value: '25:00'}}); | ||
|
|
||
| expect(onChange).not.toHaveBeenCalled(); | ||
|
|
||
| // Invalid minutes | ||
| fireEvent.change(timeInput, {target: {value: '10:60'}}); | ||
|
|
||
| expect(onChange).not.toHaveBeenCalled(); | ||
|
|
||
| // Non-numeric | ||
| fireEvent.change(timeInput, {target: {value: 'ab:cd'}}); | ||
|
|
||
| expect(onChange).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle time change when no initial date is selected', async () => { | ||
| const onChange = vi.fn(); | ||
|
|
||
| render(<DateTimePicker onChange={onChange} />); | ||
|
|
||
| await userEvent.click(screen.getByRole('button')); | ||
|
|
||
| const timeInput = document.querySelector('input[type="time"]') as HTMLInputElement; | ||
|
|
||
| fireEvent.change(timeInput, {target: {value: '12:00'}}); | ||
|
|
||
| expect(onChange).toHaveBeenCalled(); | ||
|
|
||
| const calledDate = onChange.mock.calls[0][0]; | ||
|
|
||
| expect(calledDate.getHours()).toBe(12); | ||
| expect(calledDate.getMinutes()).toBe(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| 'use client'; | ||
|
|
||
| import {Button} from '@/components/ui/button'; | ||
| import {Calendar} from '@/components/ui/calendar'; | ||
| import {Popover, PopoverContent, PopoverTrigger} from '@/components/ui/popover'; | ||
| import {cn} from '@/shared/util/cn-utils'; | ||
| import {format} from 'date-fns'; | ||
| import {CalendarIcon} from 'lucide-react'; | ||
| import {ChangeEvent, useEffect, useState} from 'react'; | ||
|
|
||
| export default function DateTimePicker({onChange, value}: {onChange: (date: Date | undefined) => void; value?: Date}) { | ||
| const [date, setDate] = useState<Date | undefined>(value); | ||
|
|
||
| const handleSelect = (selectedDate: Date | undefined) => { | ||
| if (selectedDate && date && !isNaN(date.getTime())) { | ||
| selectedDate.setHours(date.getHours()); | ||
| selectedDate.setMinutes(date.getMinutes()); | ||
| } | ||
|
|
||
| setDate(selectedDate); | ||
|
|
||
| onChange(selectedDate); | ||
| }; | ||
ivicac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const handleTimeChange = (e: ChangeEvent<HTMLInputElement>) => { | ||
ivicac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const [hours, minutes] = e.target.value.split(':').map(Number); | ||
|
|
||
| if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) { | ||
| return; | ||
| } | ||
|
|
||
| const newDate = date ? new Date(date) : new Date(); | ||
|
|
||
| newDate.setHours(hours); | ||
| newDate.setMinutes(minutes); | ||
|
|
||
| setDate(newDate); | ||
|
|
||
| onChange(newDate); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| setDate(value); | ||
| }, [value]); | ||
|
|
||
| return ( | ||
| <Popover> | ||
| <PopoverTrigger asChild> | ||
| <Button | ||
| className={cn('w-full justify-start text-left font-normal', !date && 'text-muted-foreground')} | ||
| variant="outline" | ||
| > | ||
| <CalendarIcon className="mr-2 size-4" /> | ||
|
|
||
| {date ? format(date, 'PPP HH:mm') : <span>Pick a date and time</span>} | ||
| </Button> | ||
| </PopoverTrigger> | ||
|
|
||
| <PopoverContent align="start" className="w-auto p-0"> | ||
| <Calendar autoFocus mode="single" onSelect={handleSelect} selected={date} /> | ||
|
|
||
| <div className="border-t p-3"> | ||
| <input | ||
| className="w-full rounded-md border border-input bg-background px-3 py-1 text-sm shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring" | ||
| onChange={handleTimeChange} | ||
| type="time" | ||
| value={date ? format(date, 'HH:mm') : ''} | ||
| /> | ||
| </div> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.