Skip to content

Commit c682e67

Browse files
refactor(comp-510): fix mock price data generation (#547)
Fixes: #546 - Ref: https://coss.com/origin/calendar-date-picker <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixed mock price data generation in comp-510 so the calendar renders once and shows prices for the next 180 days. Also updated the registry item metadata to align with the calendar dependency. - **Bug Fixes** - Generate mock prices once on mount using a local todayDate, removing the unstable today dependency. - Keep date disabling tied to the price map by formatted date key. - **Refactors** - comp-510.json now declares name/type and the calendar registry dependency. - Normalized meta tags and file entry format. <sup>Written for commit b99a9a3. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. -->
1 parent 7a0a5c9 commit c682e67

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

apps/origin/public/r/comp-510.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
{
22
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
3+
"name": "comp-510",
4+
"type": "registry:component",
5+
"registryDependencies": [
6+
"https://coss.com/origin/r/calendar.json"
7+
],
38
"files": [
49
{
5-
"content": "\"use client\";\n\nimport { format } from \"date-fns\";\nimport { useEffect, useState } from \"react\";\nimport type { DayButtonProps } from \"react-day-picker\";\n\nimport { cn } from \"@/registry/default/lib/utils\";\nimport { Calendar } from \"@/registry/default/ui/calendar\";\n\nconst GOOD_PRICE_THRESHOLD = 100;\n\nexport default function Component() {\n const today = new Date();\n const [date, setDate] = useState<Date | undefined>(today);\n\n // Mock price data\n const [mockPriceData, setMockPriceData] = useState<Record<string, number>>(\n {},\n );\n useEffect(() => {\n const generateMockPriceData = () => {\n const data: Record<string, number> = {};\n\n for (let i = 0; i < 180; i++) {\n const date = new Date(today);\n date.setDate(today.getDate() + i);\n const dateKey = format(date, \"yyyy-MM-dd\");\n const randomPrice = Math.floor(Math.random() * (200 - 80 + 1)) + 80;\n data[dateKey] = randomPrice;\n }\n return data;\n };\n setMockPriceData(generateMockPriceData());\n }, [today]);\n\n const isDateDisabled = (date: Date) => {\n return !mockPriceData[format(date, \"yyyy-MM-dd\")];\n };\n\n return (\n <div>\n <Calendar\n className=\"rounded-md border p-2\"\n classNames={{\n day_button: \"size-12\",\n month:\n \"relative first-of-type:before:hidden before:absolute max-md:before:inset-x-2 max-md:before:h-px max-md:before:-top-4 md:before:inset-y-2 md:before:w-px before:bg-border md:before:-left-4\",\n months: \"sm:flex-col md:flex-row gap-8\",\n today: \"*:after:hidden\",\n weekday: \"w-12\",\n }}\n components={{\n DayButton: (props: DayButtonProps) => (\n <DayButton {...props} prices={mockPriceData} />\n ),\n }}\n disabled={isDateDisabled}\n mode=\"single\"\n numberOfMonths={2}\n onSelect={setDate}\n pagedNavigation\n selected={date}\n showOutsideDays={false}\n />\n <p\n aria-live=\"polite\"\n className=\"mt-4 text-center text-muted-foreground text-xs\"\n role=\"region\"\n >\n Pricing calendar -{\" \"}\n <a\n className=\"underline hover:text-foreground\"\n href=\"https://daypicker.dev/\"\n rel=\"noreferrer noopener nofollow\"\n target=\"_blank\"\n >\n React DayPicker\n </a>\n </p>\n </div>\n );\n}\n\nfunction DayButton(props: DayButtonProps & { prices: Record<string, number> }) {\n const { day, prices, ...buttonProps } = props;\n const price = prices[format(day.date, \"yyyy-MM-dd\")];\n const isGoodPrice = price < GOOD_PRICE_THRESHOLD;\n\n return (\n <button {...buttonProps}>\n <span className=\"flex flex-col\">\n {props.children}\n {price && (\n <span\n className={cn(\n \"font-medium text-[10px]\",\n isGoodPrice\n ? \"text-emerald-500\"\n : \"text-muted-foreground group-data-selected:text-primary-foreground/70\",\n )}\n >\n ${price}\n </span>\n )}\n </span>\n </button>\n );\n}\n",
610
"path": "registry/default/components/comp-510.tsx",
11+
"content": "\"use client\";\n\nimport { format } from \"date-fns\";\nimport { useEffect, useState } from \"react\";\nimport type { DayButtonProps } from \"react-day-picker\";\n\nimport { cn } from \"@/registry/default/lib/utils\";\nimport { Calendar } from \"@/registry/default/ui/calendar\";\n\nconst GOOD_PRICE_THRESHOLD = 100;\n\nexport default function Component() {\n const today = new Date();\n const [date, setDate] = useState<Date | undefined>(today);\n\n // Mock price data\n const [mockPriceData, setMockPriceData] = useState<Record<string, number>>(\n {},\n );\n useEffect(() => {\n const generateMockPriceData = () => {\n const data: Record<string, number> = {};\n const todayDate = new Date();\n\n for (let i = 0; i < 180; i++) {\n const date = new Date(todayDate);\n date.setDate(todayDate.getDate() + i);\n const dateKey = format(date, \"yyyy-MM-dd\");\n const randomPrice = Math.floor(Math.random() * (200 - 80 + 1)) + 80;\n data[dateKey] = randomPrice;\n }\n return data;\n };\n setMockPriceData(generateMockPriceData());\n }, []);\n\n const isDateDisabled = (date: Date) => {\n return !mockPriceData[format(date, \"yyyy-MM-dd\")];\n };\n\n return (\n <div>\n <Calendar\n className=\"rounded-md border p-2\"\n classNames={{\n day_button: \"size-12\",\n month:\n \"relative first-of-type:before:hidden before:absolute max-md:before:inset-x-2 max-md:before:h-px max-md:before:-top-4 md:before:inset-y-2 md:before:w-px before:bg-border md:before:-left-4\",\n months: \"sm:flex-col md:flex-row gap-8\",\n today: \"*:after:hidden\",\n weekday: \"w-12\",\n }}\n components={{\n DayButton: (props: DayButtonProps) => (\n <DayButton {...props} prices={mockPriceData} />\n ),\n }}\n disabled={isDateDisabled}\n mode=\"single\"\n numberOfMonths={2}\n onSelect={setDate}\n pagedNavigation\n selected={date}\n showOutsideDays={false}\n />\n <p\n aria-live=\"polite\"\n className=\"mt-4 text-center text-muted-foreground text-xs\"\n role=\"region\"\n >\n Pricing calendar -{\" \"}\n <a\n className=\"underline hover:text-foreground\"\n href=\"https://daypicker.dev/\"\n rel=\"noreferrer noopener nofollow\"\n target=\"_blank\"\n >\n React DayPicker\n </a>\n </p>\n </div>\n );\n}\n\nfunction DayButton(props: DayButtonProps & { prices: Record<string, number> }) {\n const { day, prices, ...buttonProps } = props;\n const price = prices[format(day.date, \"yyyy-MM-dd\")];\n const isGoodPrice = price < GOOD_PRICE_THRESHOLD;\n\n return (\n <button {...buttonProps}>\n <span className=\"flex flex-col\">\n {props.children}\n {price && (\n <span\n className={cn(\n \"font-medium text-[10px]\",\n isGoodPrice\n ? \"text-emerald-500\"\n : \"text-muted-foreground group-data-selected:text-primary-foreground/70\",\n )}\n >\n ${price}\n </span>\n )}\n </span>\n </button>\n );\n}\n",
712
"type": "registry:component"
813
}
914
],
1015
"meta": {
1116
"colSpan": 3,
1217
"style": 1,
13-
"tags": ["calendar", "date", "react daypicker"]
14-
},
15-
"name": "comp-510",
16-
"registryDependencies": ["https://coss.com/origin/r/calendar.json"],
17-
"type": "registry:component"
18-
}
18+
"tags": [
19+
"calendar",
20+
"date",
21+
"react daypicker"
22+
]
23+
}
24+
}

apps/origin/registry/default/components/comp-510.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ export default function Component() {
2020
useEffect(() => {
2121
const generateMockPriceData = () => {
2222
const data: Record<string, number> = {};
23+
const todayDate = new Date();
2324

2425
for (let i = 0; i < 180; i++) {
25-
const date = new Date(today);
26-
date.setDate(today.getDate() + i);
26+
const date = new Date(todayDate);
27+
date.setDate(todayDate.getDate() + i);
2728
const dateKey = format(date, "yyyy-MM-dd");
2829
const randomPrice = Math.floor(Math.random() * (200 - 80 + 1)) + 80;
2930
data[dateKey] = randomPrice;
3031
}
3132
return data;
3233
};
3334
setMockPriceData(generateMockPriceData());
34-
}, [today]);
35+
}, []);
3536

3637
const isDateDisabled = (date: Date) => {
3738
return !mockPriceData[format(date, "yyyy-MM-dd")];

0 commit comments

Comments
 (0)