-
Notifications
You must be signed in to change notification settings - Fork 13
Feat: Component MonthPicker #204
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
Open
ayushnirwal
wants to merge
4
commits into
develop
Choose a base branch
from
feat/month-picker
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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,3 @@ | ||
| export { default as MonthPicker } from "./monthPicker"; | ||
| export * from "./monthPicker"; | ||
| export * from "./types"; | ||
79 changes: 79 additions & 0 deletions
79
packages/frappe-ui-react/src/components/monthPicker/monthPicker.stories.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,79 @@ | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { useState } from "react"; | ||
|
|
||
| import MonthPicker from "./monthPicker"; | ||
| import type { MonthPickerProps } from "./types"; | ||
|
|
||
| export default { | ||
| title: "Components/MonthPicker", | ||
| component: MonthPicker, | ||
| tags: ["autodocs"], | ||
| argTypes: { | ||
| value: { | ||
| control: "text", | ||
| description: | ||
| "Selected month value in 'Month Year' format (e.g., 'January 2026').", | ||
| }, | ||
| placeholder: { | ||
| control: "text", | ||
| description: "Placeholder text for the MonthPicker button.", | ||
| }, | ||
| className: { | ||
| control: "text", | ||
| description: "CSS class names to apply to the button.", | ||
| }, | ||
| placement: { | ||
| control: "select", | ||
| options: [ | ||
| "top-start", | ||
| "top", | ||
| "top-end", | ||
| "bottom-start", | ||
| "bottom", | ||
| "bottom-end", | ||
| "left-start", | ||
| "left", | ||
| "left-end", | ||
| "right-start", | ||
| "right", | ||
| "right-end", | ||
| ], | ||
| description: "Popover placement relative to the target.", | ||
| }, | ||
| onChange: { | ||
| action: "onChange", | ||
| description: "Callback fired when the month value changes.", | ||
| }, | ||
| }, | ||
| parameters: { docs: { source: { type: "dynamic" } }, layout: "centered" }, | ||
| } as Meta<typeof MonthPicker>; | ||
|
|
||
| type Story = StoryObj<MonthPickerProps>; | ||
|
|
||
| export const Default: Story = { | ||
| render: (args) => { | ||
| const [value, setValue] = useState<string>(""); | ||
| return ( | ||
| <div className="w-80 p-2"> | ||
| <MonthPicker {...args} value={value} onChange={setValue} /> | ||
| </div> | ||
| ); | ||
| }, | ||
| args: { | ||
| placeholder: "Select month", | ||
| }, | ||
| }; | ||
|
|
||
| export const FitWidth: Story = { | ||
| render: (args) => { | ||
| const [value, setValue] = useState<string>(""); | ||
| return ( | ||
| <div className="p-2"> | ||
| <MonthPicker {...args} value={value} onChange={setValue} /> | ||
| </div> | ||
| ); | ||
| }, | ||
| args: { | ||
| placeholder: "Select month", | ||
| }, | ||
| }; |
152 changes: 152 additions & 0 deletions
152
packages/frappe-ui-react/src/components/monthPicker/monthPicker.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,152 @@ | ||
| /** | ||
| * External dependencies. | ||
| */ | ||
| import { useCallback, useMemo, useState } from "react"; | ||
| import { ChevronLeft, ChevronRight, Calendar } from "lucide-react"; | ||
| import clsx from "clsx"; | ||
|
|
||
| /** | ||
| * Internal dependencies. | ||
| */ | ||
| import { dayjs } from "../../utils/dayjs"; | ||
| import { Popover } from "../popover"; | ||
| import { Button } from "../button"; | ||
| import type { MonthPickerProps } from "./types"; | ||
|
|
||
| const MONTHS = [ | ||
| "January", | ||
| "February", | ||
| "March", | ||
| "April", | ||
| "May", | ||
| "June", | ||
| "July", | ||
| "August", | ||
| "September", | ||
| "October", | ||
| "November", | ||
| "December", | ||
| ]; | ||
|
|
||
| const parseValue = (val: string | undefined) => { | ||
| if (!val) return null; | ||
| const parsed = dayjs(val, "MMMM YYYY"); | ||
b1ink0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (parsed.isValid()) { | ||
| return { month: parsed.format("MMMM"), year: parsed.year() }; | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| const MonthPicker = ({ | ||
| value, | ||
| placeholder = "Select month", | ||
| className, | ||
| placement, | ||
| onChange, | ||
| }: MonthPickerProps) => { | ||
| const [open, setOpen] = useState(false); | ||
| const [viewMode, setViewMode] = useState<"month" | "year">("month"); | ||
| const [currentYear, setCurrentYear] = useState<number>( | ||
| parseValue(value)?.year ?? new Date().getFullYear() | ||
| ); | ||
b1ink0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const yearRangeStart = useMemo( | ||
| () => currentYear - (currentYear % 12), | ||
| [currentYear] | ||
| ); | ||
|
|
||
| const yearRange = useMemo( | ||
| () => Array.from({ length: 12 }, (_, i) => yearRangeStart + i), | ||
| [yearRangeStart] | ||
| ); | ||
|
|
||
| const pickerList = useMemo( | ||
| () => (viewMode === "year" ? yearRange : MONTHS), | ||
| [viewMode, yearRange] | ||
| ); | ||
|
|
||
| const toggleViewMode = useCallback(() => { | ||
| setViewMode((prevMode) => (prevMode === "month" ? "year" : "month")); | ||
| }, []); | ||
|
|
||
| const prev = useCallback(() => { | ||
| setCurrentYear((y) => (viewMode === "year" ? y - 12 : y - 1)); | ||
| }, [viewMode]); | ||
|
|
||
| const next = useCallback(() => { | ||
| setCurrentYear((y) => (viewMode === "year" ? y + 12 : y + 1)); | ||
| }, [viewMode]); | ||
|
|
||
| const handleOpenChange = useCallback((isOpen: boolean) => { | ||
| setOpen(isOpen); | ||
| if (!isOpen) setViewMode("month"); | ||
| }, []); | ||
|
|
||
| const handleOnClick = useCallback( | ||
| (v: string | number) => { | ||
| const parts = (value || "").split(" "); | ||
| const indexToModify = viewMode === "year" ? 1 : 0; | ||
| parts[indexToModify] = String(v); | ||
| const newValue = parts.join(" "); | ||
| onChange?.(newValue); | ||
b1ink0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| [value, viewMode, onChange] | ||
| ); | ||
|
|
||
| return ( | ||
| <Popover | ||
| trigger="click" | ||
| placement={placement || "bottom-start"} | ||
| show={open} | ||
| onUpdateShow={handleOpenChange} | ||
| target={({ togglePopover }) => ( | ||
| <Button | ||
| onClick={togglePopover} | ||
| className={clsx("w-full justify-between!", className)} | ||
| iconRight={() => <Calendar className="w-4 h-4" />} | ||
| > | ||
| {value || placeholder} | ||
| </Button> | ||
| )} | ||
| popoverClass="w-min!" | ||
| body={() => ( | ||
| <div className="mt-2 w-max content shadow-xl rounded-lg border border-outline-gray-1 bg-surface-modal p-2"> | ||
| <div className="flex gap-2 justify-between"> | ||
| <Button variant="ghost" onClick={prev}> | ||
| <ChevronLeft className="w-4 h-4 text-ink-gray-5" /> | ||
| </Button> | ||
|
|
||
| <Button onClick={toggleViewMode}> | ||
| {viewMode === "month" | ||
| ? (value || "").split(" ")[1] || currentYear | ||
b1ink0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : `${yearRangeStart} - ${yearRangeStart + 11}`} | ||
| </Button> | ||
|
|
||
| <Button variant="ghost" onClick={next}> | ||
| <ChevronRight className="w-4 h-4 text-ink-gray-5" /> | ||
| </Button> | ||
b1ink0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </div> | ||
|
|
||
| <hr className="my-2 border-outline-gray-1" /> | ||
|
|
||
| <div className="grid grid-cols-3 gap-3"> | ||
| {pickerList.map((month, index) => ( | ||
| <Button | ||
| key={index} | ||
| onClick={() => handleOnClick(month)} | ||
| variant={ | ||
| (value || "").includes(String(month)) ? "solid" : "ghost" | ||
b1ink0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| className="text-sm text-ink-gray-9" | ||
| > | ||
| {viewMode === "month" ? (month as string).slice(0, 3) : month} | ||
| </Button> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| )} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default MonthPicker; | ||
b1ink0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
19 changes: 19 additions & 0 deletions
19
packages/frappe-ui-react/src/components/monthPicker/types.ts
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,19 @@ | ||
| export interface MonthPickerProps { | ||
| value?: string; | ||
| placeholder?: string; | ||
| className?: string; | ||
| placement?: | ||
| | "top-start" | ||
| | "top" | ||
| | "top-end" | ||
| | "bottom-start" | ||
| | "bottom" | ||
| | "bottom-end" | ||
| | "left-start" | ||
| | "left" | ||
| | "left-end" | ||
| | "right-start" | ||
| | "right" | ||
| | "right-end"; | ||
b1ink0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| onChange?: (value: string) => void; | ||
| } | ||
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.