0.0.6 • Published 8 months ago

react-formulary v0.0.6

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

React + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

es posible enviar componentes estilizados

import './App.css';
import useFormulary from './hook/use-formulary';
import Formulary from './components/formulary';
import { Component, useEffect, useState } from 'react';
import { defaultImage } from './utils/default-image';
import { styled } from 'styled-components';
const StyledInput = styled.input`
  padding: 1rem;
  background-color: lightblue;
  border-radius: 10px;
  width: 100%;
`;

const StyledLabel = styled.label`
  text-align: left;
  width: 100%;
`;
const StyledImage = styled.img`
  width: 100px;
  height: auto;
`;

const sleep = async (ms) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, ms);
  });
};
function App() {
  const [changes, setChanges] = useState(0);
  useEffect(() => {}, [changes]);

  const context = useFormulary({
    formTitleComponent: 'Datos personales',
    loadingComponent: 'loading..',
    submitComponent: <button type="submit">Continuar</button>,
    asyncOnChange: async (event) => {
      console.log('cambiando', event.target.name, event.target.value);
    },
    asyncOnLoad: async (fields) => {
      console.log('onload', fields);
      return [
        {
          id: 11,
          name: 'firstName',
          value: 'Hola mundo',
        },
        {
          id: 15,
          name: 'ineReverso',
          src: defaultImage,
        },
      ];
    },
    asyncOnSubmit: async (result) => {
      await sleep(1000);
      console.log('submit', result);
      return true;
    },
    asyncOnAutoSave: async (result) => {
      await sleep(1000);
      console.log('autosave', result);
      return true;
    },
    fieldsArray: [
      {
        name: 'firstName',
        inputComponent: <StyledInput required={true} />,
        labelComponent: <StyledLabel>Nombre Principal</StyledLabel>,
        flexDirection: 'column',
      },
      {
        name: 'ineReverso',
        inputComponent: <StyledInput type="file" required={true} />,
        labelComponent: <StyledLabel>Ine por el reverso</StyledLabel>,
        flexDirection: 'column',
        previewComponent: <StyledImage />,
      },
    ],
  });

  return (
    <div style={{ width: '100%' }}>
      <button
        onClick={() => {
          context.setTitle('Nuevo Titulo');
          setChanges(changes + 1);
        }}
      >
        Cambiar titulo
      </button>

      <button
        onClick={() => {
          context.addField({
            name: 'ineFrente',
            inputComponent: <StyledInput type="file" required={true} />,
            labelComponent: <StyledLabel>Ine por el frente</StyledLabel>,
            flexDirection: 'column',
            previewComponent: <StyledImage />,
          });
          setChanges(changes + 1);
        }}
      >
        Agregar un campo
      </button>

      <button
        onClick={() => {
          context.setFields([
            {
              name: 'phone',
              inputComponent: <StyledInput type="number" required={true} />,
              labelComponent: <StyledLabel>Telefono Principal</StyledLabel>,
              flexDirection: 'column',
            },
            {
              name: 'state',
              inputComponent: <StyledInput required={true} />,

              labelComponent: (
                <StyledLabel>Estado de la republical</StyledLabel>
              ),
              flexDirection: 'column',
            },
            {
              name: 'gafete',
              inputComponent: <StyledInput type="file" required={true} />,
              labelComponent: <StyledLabel>Foto para gafete</StyledLabel>,
              flexDirection: 'column',
              previewComponent: <StyledImage />,
            },
          ]);
          setChanges(changes + 1);
        }}
      >
        cambiar todos los campos
      </button>

      <button
        onClick={() => {
          context.setOptions('state', ['Michoacan', 'Guanajuato']);
          setChanges(changes + 1);
        }}
      >
        Agregar opciones a estado
      </button>

      <button
        onClick={() => {
          context.setFieldValue('phone', '4434858687');
          context.setFieldProp('phone', { readOnly: true });
          setChanges(changes + 1);
        }}
      >
        Agregar valor a telefono
      </button>

      <button
        onClick={() => {
          context.setFieldSrc('gafete', defaultImage);
          setChanges(changes + 1);
        }}
      >
        Agregar src al gafete
      </button>

      <Formulary context={context} />
    </div>
  );
}

export default App;
0.0.6

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago

0.0.0

8 months ago