New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@muzikanto/observable-form

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@muzikanto/observable-form

Form state manager

  • 2.7.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Observable-form

npm version downloads dependencies Status size Code style

Introduction

Installation

npm i @muzikanto/observable @muzikanto/observable-form
# or
yarn add @muzikanto/observable @muzikanto/observable-form

Examples

demo

example code example with hooks code

create api

interface State {
   text: string;
   field: string;
   deep: {
      one: string;
      two: string;
   };
   arr: string[];
}

const validationSchema = Yup.object().shape({
   text: Yup.string()
      .required()
      .max(5),
   field: Yup.string()
      .required()
      .max(3),
});

const form = createForm<FormState>({
   validateOnMount: false,
   initialState: {
      text: '123',
      field: '',
      deep: { one: '1', two: '' },
      arr: ['1', '2', '3'],
   },
   validationSchema,
});

form.values.watch(state => console.log('values: ', state));

create field

function FieldInput(props: { name: string }) {
   const { value, error, touched, setFieldTouched, setFieldValue } = useField<string>({
      name: props.name,
      validate: v => {
         if (false) {
            return 'my custom error';
         }
      },
   });

   const touchedAndError = Boolean(touched && error);

   return (
      <>
         <input
            value={value}
            onChange={e => setFieldValue(e.target.value)}
            onBlur={() => setFieldTouched(true)}
         />
         {touchedAndError && <span>{error}</span>}
      </>
   );
}

Use

function FormComponent() {
   return (
      <Form
         form={form}
         formId='form-id'
         formProps={{}}
         Confirm={({ open, onClose, onAccept }) => (
            <Dialog open={open} onClose={onClose}>
               <DialogContent>
                  <Button
                     onClick={() => {
                        onClose();
                        form.reset();
                     }}
                  >
                     Reject
                  </Button>
                  <Button onClick={onAccept}>Accept</Button>
               </DialogContent>
            </Dialog>
         )}
      >
         <FieldInput name='text' />
         <FieldInput name='field' />
         <FieldInput name='deep.one' />

         <button onClick={() => form.reset()}>RESET</button>
         <button onClick={() => form.validate()}>VALIDATE</button>

         <Submit
            component={({ onClick, disabled }) => (
               <button onClick={onClick} disabled={disabled}>
                  Submit
               </button>
            )}
         />
         <ErrorMessage name='field' component={props => <span {...props} />} />
      </Form>
   );
}

License

MIT

Keywords

FAQs

Package last updated on 14 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc