feat: expose form validation methods from EventTypePlatformWrapper (#22655)
* feat: expose form validation methods from EventTypePlatformWrapper - Add validateForm and handleFormSubmit methods to onFormStateChange prop - Update useEventTypeForm hook to return validateForm method - Add proper TypeScript types for form validation result - Enable external form validation and submission capabilities Co-Authored-By: [email protected] <[email protected]> * fix: correct forwardRef syntax to resolve parsing errors - Fix forwardRef parameter destructuring syntax - Use proper props parameter pattern for forwardRef components - Ensure prettier can parse the file correctly Co-Authored-By: [email protected] <[email protected]> * feat: add form validation and submission handlers to event types platform wrapper * refactor: move form validation and submit handlers from hook to wrapper component * fix: add ref to the save button for proper form validation * feat: extend ref-based validation API to AvailabilitySettingsPlatformWrapper - Add AvailabilityFormValidationResult and AvailabilitySettingsPlatformWrapperRef types - Implement forwardRef pattern in AvailabilitySettings component with save button ref - Add validateForm and handleFormSubmit methods using useImperativeHandle - Update AvailabilitySettingsPlatformWrapper to support ref forwarding - Add comprehensive documentation for ref-based validation API in both event-type.mdx and availability-settings.mdx - Follow same patterns established in EventTypePlatformWrapper for consistency Co-Authored-By: [email protected] <[email protected]> * refactor: rename AvailabilitySettingsPlatformWrapperRef to AvailabilitySettingsFormRef * feat: add ref-based validation pattern to availability example page - Mirror the ref-based validation implementation from event-types.tsx - Add useRef, handleValidate, and handleSubmit functions - Demonstrate validateForm() and handleFormSubmit() methods - Add validation and submit buttons to showcase the new API - Use AvailabilitySettingsFormRef type for proper typing Co-Authored-By: [email protected] <[email protected]> * refactor: remove unused state variables * docs: clarify form validation behavior for availability and event type atoms * chore: remove console.logs --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]> Co-authored-by: Somay Chauhan <[email protected]>
This commit is contained in:
15 files changed
+755
-455
No files matched your search
@@ -1,16 +1,49 @@
|
||||
import { Navbar } from "@/components/Navbar";
|
||||
import { Inter } from "next/font/google";
|
||||
import { useRef, useCallback } from "react";
|
||||
|
||||
import type { AvailabilitySettingsFormRef } from "@calcom/atoms";
|
||||
import { AvailabilitySettings } from "@calcom/atoms";
|
||||
|
||||
const inter = Inter({ subsets: ["latin"] });
|
||||
|
||||
export default function Availability(props: { calUsername: string; calEmail: string }) {
|
||||
const availabilityRef = useRef<AvailabilitySettingsFormRef>(null);
|
||||
|
||||
const handleFormStateChange = useCallback((formState: unknown) => {
|
||||
console.log(formState, "formStateeeeee");
|
||||
}, []);
|
||||
|
||||
const handleValidate = async () => {
|
||||
const result = await availabilityRef.current?.validateForm();
|
||||
console.log("Validation result:", result);
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
availabilityRef.current?.handleFormSubmit();
|
||||
};
|
||||
|
||||
return (
|
||||
<main className={`flex min-h-screen flex-col ${inter.className}`}>
|
||||
<Navbar username={props.calUsername} />
|
||||
<div>
|
||||
<h1 className="mx-10 my-4 text-2xl font-semibold">Availability Settings</h1>
|
||||
|
||||
<div className="mx-10 mb-4 flex gap-4">
|
||||
<button
|
||||
onClick={handleValidate}
|
||||
className="rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50">
|
||||
Validate Form
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSubmit}
|
||||
className="rounded-md border border-transparent bg-blue-600 px-4 py-2 text-sm font-medium text-white hover:bg-blue-700">
|
||||
Submit Form
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<AvailabilitySettings
|
||||
ref={availabilityRef}
|
||||
enableOverrides={true}
|
||||
customClassNames={{
|
||||
subtitlesClassName: "text-red-500",
|
||||
@@ -18,6 +51,7 @@ export default function Availability(props: { calUsername: string; calEmail: str
|
||||
editableHeadingClassName: "underline font-semibold",
|
||||
hiddenSwitchClassname: { thumb: "bg-red-500" },
|
||||
}}
|
||||
onFormStateChange={handleFormStateChange}
|
||||
onUpdateSuccess={() => {
|
||||
console.log("Updated successfully");
|
||||
}}
|
||||
|
||||
@@ -2,8 +2,9 @@ import { Navbar } from "@/components/Navbar";
|
||||
import { Inter } from "next/font/google";
|
||||
// eslint-disable-next-line @calcom/eslint/deprecated-imports-next-router
|
||||
import { useRouter } from "next/router";
|
||||
import { useState } from "react";
|
||||
import { useState, useRef, useCallback } from "react";
|
||||
|
||||
import type { EventSettingsFromRef } from "@calcom/atoms";
|
||||
import {
|
||||
useEventTypes,
|
||||
useTeamEventTypes,
|
||||
@@ -18,6 +19,20 @@ export default function Bookings(props: { calUsername: string; calEmail: string
|
||||
const [eventTypeId, setEventTypeId] = useState<number | null>(null);
|
||||
const [isTeamEvent, setIsTeamEvent] = useState<boolean>(false);
|
||||
const router = useRouter();
|
||||
const eventTypeRef = useRef<EventSettingsFromRef>(null);
|
||||
|
||||
const handleFormStateChange = useCallback((formState: unknown) => {
|
||||
console.log(formState, "formStateeeeee");
|
||||
}, []);
|
||||
|
||||
const handleValidate = async () => {
|
||||
const result = await eventTypeRef.current?.validateForm();
|
||||
console.log("Validation result:", result);
|
||||
};
|
||||
|
||||
const handleSubmit = () => {
|
||||
eventTypeRef.current?.handleFormSubmit();
|
||||
};
|
||||
const { isLoading: isLoadingEvents, data: eventTypes, refetch } = useEventTypes(props.calUsername);
|
||||
const { data: teams } = useTeams();
|
||||
const {
|
||||
@@ -739,7 +754,22 @@ export default function Bookings(props: { calUsername: string; calEmail: string
|
||||
setEventTypeId(null);
|
||||
}}
|
||||
onDeleteError={console.error}
|
||||
onFormStateChange={handleFormStateChange}
|
||||
ref={eventTypeRef}
|
||||
/>
|
||||
|
||||
<div className="mt-4 flex justify-center gap-4">
|
||||
<button
|
||||
onClick={handleValidate}
|
||||
className="rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600">
|
||||
Validate Form
|
||||
</button>
|
||||
<button
|
||||
onClick={handleSubmit}
|
||||
className="rounded bg-green-500 px-4 py-2 text-white hover:bg-green-600">
|
||||
Submit Form
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
Reference in new issue
Block a user