1.0.97 • Published 11 months ago

kemis-react-form v1.0.97

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

Code Example (using a form inside a Modal window) in a React TypeScript project:

import { useCallback, useEffect, useState } from "react";
import { SelectItemOptionsType } from "primereact/selectitem"; // Optional: For Dropdown
import { useForm } from "react-hook-form"; // Give us access to many form features
import {
  Dropdown,
  FormDialog,
  InputText,
  InputTextArea,
} from "kemis-react-form"; // Import the package
import IMEquipment from "~/models/Equipments"; // Our custom model
import axios from 'axios'; // Optional: To create the API instance to send the POST/PUT request

interface IModalProps {
  header: string;
  show: boolean;
  dataEdit: IMEquipment;
  onHide: () => void;
  onRefreshTable: (refreshTable: boolean) => void;
}

const ModalForm = ({ header, show, dataEdit, onHide, onRefreshTable }) => {
  // Control the form:
  const form = useForm({ defaultValues });
  // To reset the form from scratch:
  const { reset } = useForm({ defaultValues });
  // For the dropdown:
  const [customers, setCustomers] = useState<SelectItemOptionsType>([
    { id: 1, name: "Nicanor Orlando" },
  ]);
  // To submit the form and POST/PUT the data in our selected url:
  const api = axios.create({
    baseURL: "our-backend-base-url.com,
  });

  // To fetch the data to show in dropdown:
  useEffect(() => {
    fetch("https://example.com/api/data")
      .then((response) => response.json())
      .then((json) => {
        const data = response.data.map((customers) => ({
          // Choose the value to send in the form:
          value: customers.id,
          // Choose value to show to the user in the dropdown
          label: customers.name,
        }));
        setCustomers(data);
      })
      .catch((error) => console.error(error));
  }, []);

  // If we have something in dataEdit we fill the fields who are included in the dataEdit object.
  useEffect(() => {
    if (dataEdit) {
      Object.keys(dataEdit).forEach((key: string) => {
        const k = key as keyof IMEquipment;
        form.setValue(k, dataEdit[k]);
      });
    }
  }, [dataEdit, form]);

  // When we close our modal:
  const handleHide = useCallback(() => {
    reset();
    onHide();
  }, [onHide, reset]);

  return (
    <FormDialog
      api={api} // Optional: This obj allows to make the POST/PUT request to our service/api
      url="/example/api/" // Optional: path to POST/PUT our form data
      header={header} // Modal header
      dataEdit={dataEdit} // If we want to autocomplete fields
      form={form} // To control the form data
      visible={show} // Boolean to show the form
      onHide={handleHide} // We send our function to control what happen when we close the modal window
      onRefreshTable={onRefreshTable} // Optional: Function to refresh the table when we close our form
      classNameDialog="w-5/6 lg:w-3/6" // Optional: Custom className
    >
      // If we want, we can divide the visual in tabs:
      <TabView renderActiveOnly={false}>
        // The first tab to show:
        <TabPanel header="Main">
          // Distribution of the form:
          <div className="flex flex-col sm:grid sm:grid-cols-4 gap-3 mb-4">
            <Dropdown
              // Custom class (It will expand at half ot the container):
              className="col-span-2"
              // Our key value to access customer (in this case):
              name="customer"
              // Specificate if is required:
              rules={{ required: "customer is required." }}
              // Our label on the top of the field:
              label="Customer"
              // The initial data to be selected:
              selected={dataEdit?.customer ? dataEdit.customer : ""}
              // Our array of different options
              options={customers}
              // To control/manage the data and submit
              form={form}
            />
            <InputText
              className="col-span-2"
              name="equipment"
              rules={{ required: "equipment is required." }}
              label="Equipment"
              type="text"
              form={form}
            />
            <InputTextArea
              className="col-span-4"
              name="observation"
              label="Observation"
              form={form}
            />
          </div>
        </TabPanel>
        // The second tab to show:
        <TabPanel header="Secondary data">
          <div className="flex flex-col sm:grid sm:grid-cols-2 gap-2">
            <InputMask
              autoFocus
              name="zip_code"
              label="Zip code"
              mask="99.999-999" // To specificate the mask to use
              form={form}
              onComplete={(e) => console.log("Execute our function if we want")}
            />
          </div>
        </TabPanel>
      </TabView>
    </FormDialog>
  );
};
export default ModalForm;

Author

Nicanor Orlando

1.0.62

12 months ago

1.0.61

12 months ago

1.0.60

12 months ago

1.0.66

12 months ago

1.0.65

12 months ago

1.0.64

12 months ago

1.0.63

12 months ago

1.0.69

11 months ago

1.0.68

11 months ago

1.0.67

11 months ago

1.0.73

11 months ago

1.0.72

11 months ago

1.0.71

11 months ago

1.0.70

11 months ago

1.0.77

11 months ago

1.0.76

11 months ago

1.0.75

11 months ago

1.0.74

11 months ago

1.0.79

11 months ago

1.0.78

11 months ago

1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.44

12 months ago

1.0.43

12 months ago

1.0.42

12 months ago

1.0.41

1 year ago

1.0.48

12 months ago

1.0.47

12 months ago

1.0.46

12 months ago

1.0.45

12 months ago

1.0.49

12 months ago

1.0.51

12 months ago

1.0.50

12 months ago

1.0.55

12 months ago

1.0.54

12 months ago

1.0.53

12 months ago

1.0.52

12 months ago

1.0.59

12 months ago

1.0.58

12 months ago

1.0.57

12 months ago

1.0.56

12 months ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.33

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.80

11 months ago

1.0.84

11 months ago

1.0.83

11 months ago

1.0.82

11 months ago

1.0.81

11 months ago

1.0.88

11 months ago

1.0.87

11 months ago

1.0.86

11 months ago

1.0.85

11 months ago

1.0.89

11 months ago

1.0.91

11 months ago

1.0.90

11 months ago

1.0.95

11 months ago

1.0.94

11 months ago

1.0.93

11 months ago

1.0.92

11 months ago

1.0.97

11 months ago

1.0.96

11 months ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago