1.0.2 ā€¢ Published 9 months ago

mui-custom-form v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

šŸ“ CustomForm

CustomForm is a versatile React form component designed to simplify your form-building journey. Powered by MUI components and the flexibility of react-hook-form, this package offers an intuitive way to create dynamic and responsive forms.

šŸ“š Table of Contents

šŸ›  Installation

npm install mui-custom-form

šŸ“¦ Dependencies:

  • @mui/material
  • @mui/x-date-pickers
  • react-hook-form

āœØ Features

  • šŸŽ› Dynamic Field Types
  • šŸŽØ MUI Integration
  • šŸ“± Responsive Design

šŸš€ Usage

šŸ”§ Props

CustomForm Component

NameDescriptionIs Required?
fieldsGroups2D array representing groups of fields in the form.Yes
onSubmitArray containing functions for form submission and error handling.Yes
formControlControl object from react-hook-form.Yes
submitButtonBoolean to toggle the visibility of the submit button.No
resetButtonBoolean to toggle the visibility of the reset button.No
actionButtonsPlacementCSS property for button placement.No
otherPropsAny additional props to pass down to the form component.No

CustomField 2D Array

NameDescriptionIs Required?
labelThe display label of the field.Yes
nameThe name attribute of the field.Yes
typeThe type of the field.Yes
listAn array of options for select type fields.Conditional
requiredIs the field mandatory?No
otherPropsAny additional props.No
spanGrid span for the field.No

* list prop is required when the type is single-select or multi-select.


šŸ“– Examples

Basic Form

const BasicForm = () => {
  const formControl = useForm<{ username: string; birthdate: string }>()

  const fieldsGroups: IFieldGroup = [
    [
      {
        label: "Username",
        name: "username",
        type: "text",
        required: true,
      },
      {
        label: "Birthdate",
        name: "birthdate",
        type: "date",
        required: true,
      },
    ],
  ]

  const handleSubmit = (data: { username: string; birthdate: string }) => {
    console.log({ success: data })
  }

  const submitError = (data: unknown) => {
    console.log({ error: data })
  }

  return (
    <CustomForm
      fieldsGroups={fieldsGroups}
      onSubmit={[handleSubmit, submitError]}
      formControl={formControl}
    />
  )
}

Form with Zod Validation

const GENDERS = ["Male", "Female"] as const
const HOBBIES = ["Coding", "Collections", "Hiking"] as const

const Fields = z.object({
  username: z.string(),
  age: z.number().min(16).max(80),
  gender: z.enum(GENDERS),
  hobbies: z.array(z.enum(HOBBIES)).nonempty("Please choose one"),
  birthDate: z.date(),
  file: z.instanceof(File).optional(),
})

type FieldTypes = z.infer<typeof Fields>

const FormWithZod = () => {
  const formControl = useForm<FieldTypes>({
    resolver: zodResolver(Fields),
  })

  const fieldsGroups: IFieldGroup<FieldTypes> = [
    [
      {
        label: "Username",
        name: "username",
        type: "text",
        required: true,
      },
      {
        label: "Age",
        name: "age",
        type: "number",
        required: true,
      },
    ],
    [
      {
        label: "Gender",
        name: "gender",
        type: "single-select",
        list: GENDERS.map((gender) => ({ label: gender, value: gender })),
      },
      {
        label: "Hobbies",
        name: "hobbies",
        type: "multi-select",
        list: HOBBIES.map((hobby) => ({ label: hobby, value: hobby })),
        required: true,
      },
    ],
    [
      {
        label: "Date of birth",
        name: "birthDate",
        type: "date",
      },
      {
        label: "Upload Image",
        name: "file",
        type: "file",
      },
    ],
  ]

  const onSubmit = (data: FieldTypes) => {
    console.log({ success: data })
  }

  return (
    <CustomForm
      fieldsGroups={fieldsGroups}
      onSubmit={[onSubmit]}
      formControl={formControl}
    />
  )
}

šŸ¤ Contribution

Your contributions are always welcome!

License

This project is licensed under the MIT license.


1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

0.1.8

9 months ago

0.1.7

9 months ago

0.1.6

9 months ago

0.1.5

9 months ago

0.1.4

9 months ago

0.1.3

9 months ago

0.1.2

9 months ago

0.1.1

9 months ago

0.1.0

9 months ago