Skip to content
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

Issue #PS-1249 feat:UI of form fields to show form #364

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

suvarnakale
Copy link
Collaborator

@suvarnakale suvarnakale commented Jul 9, 2024

Description

Please describe the changes in your pull request in few words here.

Changes

List the changes done to fix a bug or introducing a new feature.

How to test

Describe the steps required to test the changes proposed in the pull request.

Please consider using the closing keyword if the pull request is proposed to
fix an issue already created in the repository
(https://help.github.com/articles/closing-issues-using-keywords/)

Summary by CodeRabbit

  • New Features

    • Introduced a CustomRadioWidget component for custom radio button functionality.
    • Added a MultiSelectCheckboxes component for multi-select checkbox functionality.
    • Created an addLearner component to render a dynamic form for learner registration.
  • Bug Fixes

    • Improved error handling in forms, allowing the user to focus on problematic fields.
  • Enhancements

    • Updated the DynamicForm component to include custom widgets and enhanced error handling.
    • Defined and exported a registration form schema and UI schema for consistent form rendering.

Copy link

coderabbitai bot commented Jul 9, 2024

Walkthrough

The changes introduce a dynamic form functionality featuring custom widgets such as MultiSelectCheckboxes, CustomCheckboxWidget, and CustomRadioWidget for React applications. These widgets are imported and used in the DynamicForm component. Additionally, a new page addLearner.tsx leverages this dynamic form for user registration, utilizing a predefined form schema and UI schema for rendering.

Changes

Files Change Summaries
src/components/CustomRadioWidget.tsx Introduced a custom radio button widget component. Added props for options and onChange.
src/components/DynamicForm.tsx Added imports for custom widgets. Modified props and implemented error handling in the form.
src/components/MultiSelectCheckboxes.tsx Added a multi-select checkbox component with customizable options and event handling.
src/pages/addLearner.tsx Introduced a new component for a dynamic form with event handlers for submission and errors.
src/utils/schema.js Added a registration form schema and UI schema for form rendering specifications.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 19

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bb2f3ee and 7b77c53.

Files selected for processing (5)
  • src/components/CustomRadioWidget.tsx (1 hunks)
  • src/components/DynamicForm.tsx (3 hunks)
  • src/components/MultiSelectCheckboxes.tsx (1 hunks)
  • src/pages/addLearner.tsx (1 hunks)
  • src/utils/schema.js (1 hunks)
Additional comments not posted (3)
src/utils/schema.js (3)

1-78: Verify the usage of errorMessage in the schema.

The errorMessage property in the firstName field is not standard JSON Schema and may require additional handling in the form rendering logic.


80-96: LGTM! Verify custom widget integration.

The UI schema correctly uses custom widgets to enhance form rendering.

Ensure that the custom widgets (CustomCheckboxWidget, CustomRadioWidget, MultiSelectCheckboxes) are properly implemented and integrated with the form renderer.


98-215: LGTM! Verify alignment with backend API.

The formReadResponse object is comprehensive and includes all necessary details for form rendering and validation.

Ensure that the endpoint (/user/v1/add-facilitator) and field definitions align with the backend API requirements.

Comment on lines +8 to +17
interface CustomRadioWidgetProps {
value: string;
id: string;
required: boolean;
disabled: boolean;
readonly: boolean;
label: string;
options: any;
onChange: any;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure correct typing for options and onChange.

The options and onChange props are typed as any. It is better to provide specific types to ensure type safety and better code readability.

interface CustomRadioWidgetProps {
  value: string;
  id: string;
  required: boolean;
  disabled: boolean;
  readonly: boolean;
  label: string;
  options: {
    enumOptions: { value: string; label: string }[];
  };
  onChange: (value: string) => void;
}

Comment on lines +18 to +27
const CustomRadioWidget: React.FC<WidgetProps> = ({
id,
value,
required,
disabled,
readonly,
label,
options,
onChange,
}) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use destructuring for options.enumOptions.

Destructuring options directly in the function parameters can improve readability.

const CustomRadioWidget: React.FC<WidgetProps & CustomRadioWidgetProps> = ({
  id,
  value,
  required,
  disabled,
  readonly,
  label,
  options: { enumOptions },
  onChange,
}) => {

Comment on lines +28 to +30
const handleChange = (event: any) => {
onChange(event.target.value);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle onChange more robustly.

Ensure that onChange is always called with a string value.

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
  onChange(event.target.value);
};

Comment on lines +32 to +47
return (
<div>
<FormLabel component="legend">{label}</FormLabel>
<RadioGroup id={id} value={value} onChange={handleChange} row>
{options?.enumOptions?.map((option: any) => (
<FormControlLabel
key={option.value}
value={option.value}
control={<Radio />}
label={option.label}
disabled={disabled || readonly}
/>
))}
</RadioGroup>
</div>
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add id attribute to the FormLabel.

To improve accessibility, link the FormLabel to the RadioGroup using the htmlFor attribute.

return (
  <div>
    <FormLabel component="legend" htmlFor={id}>{label}</FormLabel>
    <RadioGroup id={id} value={value} onChange={handleChange} row>
      {enumOptions?.map((option) => (
        <FormControlLabel
          key={option.value}
          value={option.value}
          control={<Radio />}
          label={option.label}
          disabled={disabled || readonly}
        />
      ))}
    </RadioGroup>
  </div>
);

Comment on lines +1 to +3
import DynamicForm from '@/components/DynamicForm';
import React from 'react';
import { schema, uiSchema } from '@/utils/schema';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize imports.

Avoid using relative paths for imports when aliases are available.

import DynamicForm from '@/components/DynamicForm';
import { schema, uiSchema } from '@/utils/schema';

Comment on lines +1 to +11
import React, { useRef } from 'react';
import { IChangeEvent } from '@rjsf/core';
import ISubmitEvent from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';
import { Theme as MaterialUITheme } from '@rjsf/mui';
import { withTheme } from '@rjsf/core';
import { RJSFSchema } from '@rjsf/utils';
import MultiSelectCheckboxes from './MultiSelectCheckboxes';
import CustomCheckboxWidget from './CustomCheckboxWidget';
import CustomRadioWidget from './CustomRadioWidget';
import CustomErrorList from './CustomErrorList';
import { RJSFSchema, WidgetProps } from '@rjsf/utils';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimize imports.

Avoid using relative paths for imports when aliases are available.

import { IChangeEvent, ISubmitEvent, withTheme } from '@rjsf/core';
import validator from '@rjsf/validator-ajv8';
import { Theme as MaterialUITheme } from '@rjsf/mui';
import { RJSFSchema, WidgetProps } from '@rjsf/utils';
import MultiSelectCheckboxes from '@/components/MultiSelectCheckboxes';
import CustomCheckboxWidget from '@/components/CustomCheckboxWidget';
import CustomRadioWidget from '@/components/CustomRadioWidget';
import CustomErrorList from '@/components/CustomErrorList';

Comment on lines +26 to +28
widgets: {
[key: string]: React.FC<WidgetProps<any, RJSFSchema, any>>;
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure correct typing for widgets.

The widgets property in DynamicFormProps should have a more specific type.

widgets: {
  MultiSelectCheckboxes: typeof MultiSelectCheckboxes;
  CustomCheckboxWidget: typeof CustomCheckboxWidget;
  CustomRadioWidget: typeof CustomRadioWidget;
};

Comment on lines +38 to +42
const widgets = {
MultiSelectCheckboxes: MultiSelectCheckboxes,
CustomCheckboxWidget: CustomCheckboxWidget,
CustomRadioWidget: CustomRadioWidget,
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use shorthand property names.

Shorthand property names can improve readability.

const widgets = {
  MultiSelectCheckboxes,
  CustomCheckboxWidget,
  CustomRadioWidget,
};

Comment on lines +45 to +64
const handleError = (errors: any) => {
if (errors.length > 0) {
// Adjust the selector based on the actual structure of the form element names
const property = errors[0].property.replace(/^root\./, '');
const errorField = document.querySelector(
`[name$="${property}"]`
) as HTMLElement;

if (errorField) {
errorField.focus();
} else {
// If the name-based selector fails, try to select by ID as a fallback
const fallbackField = document.getElementById(property) as HTMLElement;
if (fallbackField) {
fallbackField.focus();
}
}
}
onError(errors);
};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve error handling logic.

The handleError function can be simplified and made more robust.

const handleError = (errors: any) => {
  if (errors.length > 0) {
    const property = errors[0].property.replace(/^root\./, '');
    const errorField = document.querySelector(`[name$="${property}"]`) as HTMLElement;

    if (errorField) {
      errorField.focus();
    } else {
      const fallbackField = document.getElementById(property) as HTMLElement;
      if (fallbackField) {
        fallbackField.focus();
      }
    }
  }
  onError(errors);
};

Comment on lines +65 to +79

return (
<FormWithMaterialUI
schema={schema}
uiSchema={uiSchema}
formData={formData}
onChange={onChange}
onSubmit={onSubmit}
onError={onError}
validator={validator}
liveValidate
showErrorList={false}
widgets={widgets}
noHtml5Validate
onError={handleError}
// ErrorList={CustomErrorList}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Improve form rendering.

Consider using a more responsive layout for the form.

return (
  <FormWithMaterialUI
    schema={schema}
    uiSchema={uiSchema}
    formData={formData}
    onChange={onChange}
    onSubmit={onSubmit}
    validator={validator}
    liveValidate
    showErrorList={false}
    widgets={widgets}
    noHtml5Validate
    onError={handleError}
    // ErrorList={CustomErrorList}
  />
);

@itsvick itsvick merged commit 98a6314 into tekdi:shiksha-2.0 Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants