UNPKG

react-week-picker-pro

Version:

A modern, functional React component for selecting weeks with comprehensive features including week numbers, date ranges, and flexible configuration options.

395 lines (291 loc) โ€ข 10 kB
# React Week Picker Pro ๐Ÿ“… [![npm version](https://badge.fury.io/js/react-week-picker-pro.svg)](https://badge.fury.io/js/react-week-picker-pro) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Bundle Size](https://img.shields.io/bundlephobia/minzip/react-week-picker-pro)](https://bundlephobia.com/package/react-week-picker-pro) [![Downloads](https://img.shields.io/npm/dm/react-week-picker-pro.svg)](https://npmjs.com/package/react-week-picker-pro) > A modern, lightweight React component for selecting weeks with comprehensive features and zero dependencies. ## โœจ Features - ๐Ÿ—“๏ธ **Week-only selection** - Select entire weeks, not individual days - ๐Ÿ“Š **ISO Week numbers** - Standards-compliant week numbering system - ๐Ÿ“… **Date range display** - Shows selected week's start and end dates - ๐Ÿšซ **Date restrictions** - Min/max date constraints support - ๐ŸŒ **Flexible configuration** - Sunday or Monday week start - ๐ŸŽจ **Custom formatting** - Configurable date display formats - ๐Ÿ“ฑ **Mobile responsive** - Perfect on all screen sizes - โ™ฟ **Accessible** - WCAG compliant with keyboard navigation - ๐ŸŽฏ **Lightweight** - Under 6KB gzipped - ๐Ÿ“ฆ **Tree-shakeable** - Import only what you need - ๐Ÿ’ช **TypeScript ready** - Full type definitions included - โšก **Vite powered** - Modern build system - ๐ŸŽจ **Customizable styling** - Easy to theme and customize ## ๐Ÿš€ Installation ```bash npm install react-week-picker-pro prop-types ``` Or with yarn: ```bash yarn add react-week-picker-pro prop-types ``` **Note**: `prop-types` is a peer dependency. ## ๐Ÿ“– Quick Start ```jsx import React, { useState } from 'react'; import WeekPicker from 'react-week-picker-pro'; import 'react-week-picker-pro/dist/style.css'; function App() { const [selectedWeek, setSelectedWeek] = useState(); const handleWeekSelect = (weekStart, weekEnd, weekNumber) => { setSelectedWeek(weekStart); console.log(`Selected Week ${weekNumber}: ${weekStart.toDateString()} - ${weekEnd.toDateString()}`); }; return ( <div> <h1>Select a Week</h1> <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} /> </div> ); } export default App; ``` ## ๐ŸŽฏ Live Demo Check out the [interactive demo](https://github.com/Developer-Nijat/react-week-picker-pro) to see all features in action! ## ๐Ÿ“‹ API Reference ### Props | Prop | Type | Default | Description | |------|------|---------|-------------| | `selectedWeek` | `Date` | `undefined` | Currently selected week | | `onWeekSelect` | `function` | `undefined` | Callback when a week is selected | | `minDate` | `Date` | `undefined` | Minimum selectable date | | `maxDate` | `Date` | `undefined` | Maximum selectable date | | `className` | `string` | `''` | Additional CSS class name | | `dateFormat` | `string` | `'MMM dd, yyyy'` | Date format for display | | `showWeekNumbers` | `boolean` | `true` | Whether to show week numbers | | `firstDayOfWeek` | `0 \| 1` | `1` | First day of week (0=Sunday, 1=Monday) | ### Callback Parameters The `onWeekSelect` callback receives three parameters: ```jsx const handleWeekSelect = (weekStart, weekEnd, weekNumber) => { // weekStart: Date - Start of the selected week // weekEnd: Date - End of the selected week // weekNumber: number - ISO week number }; ``` ## ๐ŸŽจ Examples ### Basic Usage ```jsx import WeekPicker from 'react-week-picker-pro'; import 'react-week-picker-pro/dist/style.css'; <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} /> ``` ### With Date Restrictions ```jsx <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} minDate={new Date(2024, 0, 1)} // From Jan 1, 2024 maxDate={new Date(2024, 11, 31)} // Until Dec 31, 2024 /> ``` ### Custom Date Format ```jsx <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} dateFormat="yyyy-MM-dd" // ISO format /> ``` ### Sunday First Week ```jsx <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} firstDayOfWeek={0} // Sunday first /> ``` ### Without Week Numbers ```jsx <WeekPicker selectedWeek={selectedWeek} onWeekSelect={handleWeekSelect} showWeekNumbers={false} /> ``` ### Controlled Component ```jsx const [selectedWeek, setSelectedWeek] = useState(); const selectCurrentWeek = () => { setSelectedWeek(new Date()); }; const clearSelection = () => { setSelectedWeek(undefined); }; return ( <div> <button onClick={selectCurrentWeek}>Select This Week</button> <button onClick={clearSelection}>Clear</button> <WeekPicker selectedWeek={selectedWeek} onWeekSelect={(start) => setSelectedWeek(start)} /> </div> ); ``` ## ๐Ÿ› ๏ธ Utility Functions The library also exports useful utility functions: ```jsx import { getISOWeekNumber, getWeekStart, getWeekEnd, formatDate, isSameWeek } from 'react-week-picker-pro'; // Get ISO week number for any date const weekNum = getISOWeekNumber(new Date()); // e.g., 27 // Get week boundaries const weekStart = getWeekStart(new Date(), 1); // Monday first const weekEnd = getWeekEnd(new Date(), 1); // Format dates const formatted = formatDate(new Date(), 'yyyy-MM-dd'); // "2025-07-07" // Compare weeks const sameWeek = isSameWeek(date1, date2, 1); // boolean ``` ## ๐ŸŽจ Styling & Customization ### CSS Classes The component uses BEM-style CSS classes for easy customization: ```css .week-picker { /* Main container */ } .week-picker__header { /* Month/year header */ } .week-picker__nav-button { /* Navigation buttons */ } .week-picker__calendar { /* Calendar grid */ } .week-picker__week-row { /* Week rows */ } .week-picker__week-row--selected { /* Selected week */ } .week-picker__day { /* Individual days */ } .week-picker__day--today { /* Today highlight */ } ``` ### Custom Styling Example ```css /* Custom theme */ .week-picker { --primary-color: #3b82f6; --selected-bg: #dbeafe; --hover-bg: #f9fafb; } .week-picker__week-row--selected { background: var(--selected-bg); border-color: var(--primary-color); } .week-picker__nav-button:hover { background: var(--hover-bg); } ``` ### Dark Mode Support The component includes built-in dark mode support via CSS media queries: ```css @media (prefers-color-scheme: dark) { .week-picker { background: #1f2937; color: #f9fafb; } } ``` ## ๐Ÿ“ฑ Responsive Design The component is fully responsive and adapts to different screen sizes: - **Desktop**: Full-featured layout with hover effects - **Tablet**: Optimized spacing and touch targets - **Mobile**: Compact layout with larger touch areas ## โ™ฟ Accessibility - **Keyboard Navigation**: Full keyboard support - **Screen Readers**: ARIA labels and semantic HTML - **Focus Management**: Clear focus indicators - **WCAG Compliance**: Meets accessibility standards ### Keyboard Shortcuts - `Arrow Keys`: Navigate between months - `Enter/Space`: Select week - `Tab`: Navigate between interactive elements ## ๐ŸŒ Browser Support - Chrome 60+ - Firefox 60+ - Safari 12+ - Edge 79+ ## ๐Ÿ“ฆ Bundle Size - **Minified**: ~8KB - **Gzipped**: ~3KB - **Zero dependencies** (except peer dependencies) ## ๐Ÿงช Testing The library includes comprehensive tests: ```bash # Run tests npm test # Run tests with coverage npm run test:coverage # Run tests in watch mode npm run test:watch ``` ## ๐Ÿ”ง Development ### Prerequisites - Node.js 18+ - npm or yarn ### Setup ```bash # Clone the repository git clone https://github.com/Developer-Nijat/react-week-picker-pro.git cd react-week-picker-pro # Install dependencies npm install # Start development server npm run dev # Build library npm run build # Run tests npm test ``` ### Project Structure ``` react-week-picker-pro/ โ”œโ”€โ”€ lib/ # Library source code โ”‚ โ”œโ”€โ”€ components/ # React components โ”‚ โ”œโ”€โ”€ utils/ # Utility functions โ”‚ โ””โ”€โ”€ styles/ # CSS styles โ”œโ”€โ”€ src/ # Demo application โ”œโ”€โ”€ tests/ # Test files โ”œโ”€โ”€ dist/ # Built library โ””โ”€โ”€ docs/ # Documentation ``` ## ๐Ÿค Contributing Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details. ### Development Workflow 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Add tests for new features 5. Ensure all tests pass 6. Submit a pull request ## ๐Ÿ“ Changelog See [CHANGELOG.md](CHANGELOG.md) for a detailed list of changes. ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿ‘จโ€๐Ÿ’ป Author **Nijat Aliyev** - GitHub: [Developer-Nijat](https://github.com/Developer-Nijat) - npm: [developer.nijat](https://www.npmjs.com/~developer.nijat) ## ๐Ÿ™ Acknowledgments - Built with [Vite](https://vitejs.dev/) for lightning-fast development - Styled with modern CSS for beautiful, accessible design - Tested with [Vitest](https://vitest.dev/) for reliable functionality - Inspired by modern date picker libraries ## ๐Ÿ”— Related Projects - [react-datepicker](https://github.com/Hacker0x01/react-datepicker) - Full date picker - [react-day-picker](https://github.com/gpbl/react-day-picker) - Flexible date picker - [react-calendar](https://github.com/wojtekmaj/react-calendar) - Calendar component --- <div align="center"> **[โฌ† Back to Top](#react-week-picker-pro-)** Made with โค๏ธ by [@developer.nijat](https://github.com/Developer-Nijat) </div>