Skip to content

Commit

Permalink
Merge pull request #402 from AkshataKatwal16/relese-changes
Browse files Browse the repository at this point in the history
Issue feat: Add custom  number widget for rjsf form
  • Loading branch information
itsvick authored Dec 4, 2024
2 parents 46b2d6b + 073fd55 commit ae778ee
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/components/CustomNumberWidget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import TextField from '@mui/material/TextField';

interface NumberWidgetProps {
value: any;
onChange: (value: any) => void;
}

const CustomNumberWidget: React.FC<NumberWidgetProps> = ({
value,
onChange,
...rest
}) => {
const handleWheel = (event: any) => {
if (event.target instanceof HTMLInputElement) {
event.target.blur();
}
};

return (
<TextField
type="number"
value={value || ''}
onChange={(e) =>
onChange(e.target.value ? Number(e.target.value) : undefined)
}
onWheel={handleWheel}
{...rest}
/>
);
};

export default CustomNumberWidget;
3 changes: 3 additions & 0 deletions src/components/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MultiSelectCheckboxes from "./form/MultiSelectCheckboxes";
import MultiSelectDropdown from "./form/MultiSelectDropdown";
const FormWithMaterialUI = withTheme(MaterialUITheme);
import { getCurrentYearPattern } from "@/utils/Helper";
import CustomNumberWidget from './CustomNumberWidget';

interface DynamicFormProps {
schema: any;
Expand Down Expand Up @@ -61,6 +62,8 @@ const DynamicForm: React.FC<DynamicFormProps> = ({
MultiSelectDropdown: MultiSelectDropdown,
MultiSelectCheckboxes: MultiSelectCheckboxes,
CustomRadioWidget: CustomRadioWidget,
CustomNumberWidget: CustomNumberWidget,

};

const handleError = (errors: any) => {
Expand Down
4 changes: 3 additions & 1 deletion src/components/GeneratedSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export const GenerateSchemaAndUiSchema = (
if (field?.minLength !== undefined && field?.minLength !== null) {
fieldSchema.minimum = Number(field.minLength);
}
fieldUiSchema['ui:widget'] = 'CustomNumberWidget';

// fieldUiSchema['ui:field'] = 'NumberInputField';
break;
case "drop_down":
Expand Down Expand Up @@ -107,7 +109,7 @@ export const GenerateSchemaAndUiSchema = (
? opt.label
: t(`FORM.${opt.label}`),
}));
fieldUiSchema["ui:widget"] = "CustomRadioWidget";
fieldUiSchema["ui:widget"] = "CustomRadioWidget";
break;
default:
break;
Expand Down

0 comments on commit ae778ee

Please sign in to comment.