|
| 1 | +# TimePicker Component |
| 2 | + |
| 3 | +A React component for time input with intelligent Chrome-like segment typing behavior. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The TimePicker component provides an intuitive time input interface with separate segments for hours, minutes, seconds (optional), and AM/PM period (for 12-hour format). It features smart typing behavior that mimics Chrome's date/time input controls. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +### Smart Typing Behavior |
| 12 | + |
| 13 | +- **Auto-coercion**: Invalid entries are automatically corrected (e.g., typing "9" in hours becomes "09") |
| 14 | +- **Progressive completion**: Type digits sequentially to build complete values (e.g., "1" → "01", then "5" → "15") |
| 15 | +- **Buffer management**: Handles rapid typing with timeout-based commits to prevent race conditions |
| 16 | +- **Auto-advance**: Automatically moves to next segment when current segment is complete |
| 17 | + |
| 18 | +### Keyboard Navigation |
| 19 | + |
| 20 | +- **Arrow keys**: Navigate between segments and increment/decrement values |
| 21 | +- **Tab**: Standard tab navigation between segments |
| 22 | +- **Delete/Backspace**: Clear current segment |
| 23 | +- **Separators**: Type ":", ".", "," or space to advance to next segment |
| 24 | + |
| 25 | +### Format Support |
| 26 | + |
| 27 | +- **24-hour format**: "HH:mm" or "HH:mm:ss" |
| 28 | +- **12-hour format**: "HH:mm a" or "HH:mm:ss a" (with AM/PM) |
| 29 | +- **Flexible display**: Configurable time format with optional seconds |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +```tsx |
| 34 | +import { TimePicker } from 'src/app-components/TimePicker/TimePicker'; |
| 35 | + |
| 36 | +// Basic usage |
| 37 | +<TimePicker |
| 38 | + id="time-input" |
| 39 | + value="14:30" |
| 40 | + onChange={(value) => console.log(value)} |
| 41 | + aria-label="Select time" |
| 42 | +/> |
| 43 | + |
| 44 | +// With 12-hour format and seconds |
| 45 | +<TimePicker |
| 46 | + id="time-input" |
| 47 | + value="2:30:45 PM" |
| 48 | + format="HH:mm:ss a" |
| 49 | + onChange={(value) => console.log(value)} |
| 50 | + aria-label="Select appointment time" |
| 51 | +/> |
| 52 | +``` |
| 53 | + |
| 54 | +## Props |
| 55 | + |
| 56 | +### Required Props |
| 57 | + |
| 58 | +- `id: string` - Unique identifier for the component |
| 59 | +- `onChange: (value: string) => void` - Callback when time value changes |
| 60 | +- `aria-label: string` - Accessibility label for the time picker |
| 61 | + |
| 62 | +### Optional Props |
| 63 | + |
| 64 | +- `value?: string` - Current time value in the specified format |
| 65 | +- `format?: TimeFormat` - Time format string (default: "HH:mm") |
| 66 | +- `disabled?: boolean` - Whether the component is disabled |
| 67 | +- `readOnly?: boolean` - Whether the component is read-only |
| 68 | +- `className?: string` - Additional CSS classes |
| 69 | +- `placeholder?: string` - Placeholder text when empty |
| 70 | + |
| 71 | +## Component Architecture |
| 72 | + |
| 73 | +### Core Components |
| 74 | + |
| 75 | +#### TimePicker (Main Component) |
| 76 | + |
| 77 | +- Manages overall time state and validation |
| 78 | +- Handles format parsing and time value composition |
| 79 | +- Coordinates segment navigation and focus management |
| 80 | + |
| 81 | +#### TimeSegment |
| 82 | + |
| 83 | +- Individual input segment for hours, minutes, seconds, or period |
| 84 | +- Implements Chrome-like typing behavior with buffer management |
| 85 | +- Handles keyboard navigation and value coercion |
| 86 | + |
| 87 | +### Supporting Modules |
| 88 | + |
| 89 | +#### segmentTyping.ts |
| 90 | + |
| 91 | +- **Input Processing**: Smart coercion logic for different segment types |
| 92 | +- **Buffer Management**: Handles multi-character input with timeouts |
| 93 | +- **Validation**: Ensures values stay within valid ranges |
| 94 | + |
| 95 | +#### keyboardNavigation.ts |
| 96 | + |
| 97 | +- **Navigation Logic**: Arrow key navigation between segments |
| 98 | +- **Value Manipulation**: Increment/decrement with arrow keys |
| 99 | +- **Key Handling**: Special key processing (Tab, Delete, etc.) |
| 100 | + |
| 101 | +#### timeFormatUtils.ts |
| 102 | + |
| 103 | +- **Format Parsing**: Converts format strings to display patterns |
| 104 | +- **Value Formatting**: Formats time values for display |
| 105 | +- **Validation**: Validates time format strings |
| 106 | + |
| 107 | +## Typing Behavior Details |
| 108 | + |
| 109 | +### Hour Input |
| 110 | + |
| 111 | +- **24-hour mode**: First digit 0-2 waits for second digit, 3-9 auto-coerces to 0X |
| 112 | +- **12-hour mode**: First digit 0-1 waits for second digit, 2-9 auto-coerces to 0X |
| 113 | +- **Second digit**: Validates against first digit (e.g., 2X limited to 20-23 in 24-hour) |
| 114 | + |
| 115 | +### Minute/Second Input |
| 116 | + |
| 117 | +- **First digit**: 0-5 waits for second digit, 6-9 auto-coerces to 0X |
| 118 | +- **Second digit**: Always accepts 0-9 |
| 119 | +- **Overflow handling**: Values > 59 are corrected during validation |
| 120 | + |
| 121 | +### Period Input (AM/PM) |
| 122 | + |
| 123 | +- **A/a key**: Sets to AM |
| 124 | +- **P/p key**: Sets to PM |
| 125 | +- **Case insensitive**: Accepts both upper and lower case |
| 126 | + |
| 127 | +## Buffer Management |
| 128 | + |
| 129 | +The component uses a sophisticated buffer system to handle rapid typing: |
| 130 | + |
| 131 | +1. **Immediate Display**: Shows formatted value immediately as user types |
| 132 | +2. **Timeout Commit**: Commits buffered value after 1 second of inactivity |
| 133 | +3. **Race Condition Prevention**: Uses refs to avoid stale closure issues |
| 134 | +4. **State Synchronization**: Keeps buffer state in sync with React state |
| 135 | + |
| 136 | +## Accessibility |
| 137 | + |
| 138 | +- **ARIA Labels**: Each segment has descriptive aria-label |
| 139 | +- **Keyboard Navigation**: Full keyboard support for all interactions |
| 140 | +- **Focus Management**: Proper focus handling and visual indicators |
| 141 | +- **Screen Reader Support**: Announces current values and changes |
| 142 | + |
| 143 | +## Testing |
| 144 | + |
| 145 | +The component includes comprehensive tests covering: |
| 146 | + |
| 147 | +- **Typing Scenarios**: Various input patterns and edge cases |
| 148 | +- **Navigation**: Keyboard navigation between segments |
| 149 | +- **Buffer Management**: Race condition prevention and timeout handling |
| 150 | +- **Format Support**: Different time formats and validation |
| 151 | +- **Accessibility**: Screen reader compatibility and ARIA support |
| 152 | + |
| 153 | +## Browser Compatibility |
| 154 | + |
| 155 | +Designed to work consistently across modern browsers with Chrome-like behavior as the reference implementation. |
0 commit comments