1.2.1 • Published 2 years ago

react-logic-block v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-logic-block

Utils and components for using logic-block in react apps.

Typescript

The library originally written in typescript, so it fully support typescript. Provided types:

  • FieldValidateFunction
  • FieldInput
  • FieldComponentProps
  • FormErrors
  • FormContextType
  • FormProps
  • Form

The Form component implemented with generics, it is pretty cool to use it with it.

//block description
type FormValues = { data: string; };
type FormUtil = { calcDataHash: () => string; };
const SomeBlock = Block<FormValues, FormUtil>({ ... });

//... at some component
<Form<FormValues, FormUtil> block={SomeBlock}>

</Form>

Form API

Content:

Form

Form is a wrapper component for you fields. Form provides block values and some handy meta data for form, all values are provided via useForm hook or FormContext

Props:

NameTypeOptional?Description
blockBlockFactoryMandatoryBlock which will use for handling form values
initialValuesPartial<TFormValues>OptionalInitial values for form. Be carefull, if initialValues changes during life-time the component will re-render this new initialValues to form values
childrenReact.NodeOptionalAny react components for rendering form. All nested components can use FormContext, Field and useField utils
validate(values: Object) => ObjectOptionalValidation function, returns object with errors. Keys for errors should be the same as keys for invalid values in values parameter of that function
onSubmit(values: Object) => voidOptionalCallback of form submit action, in that moment values passed all validations
onSubmitFailed() => voidOptionalCallback for failed submit action
onSubmitSuccess() => voidOptionalCallback for successful submit action (Invokes once after submit action call)

Field

Field is a wrapper component for inputs or any other component that should be able to change form values. All extra props to the component will pass to rendering component

Props:

NameTypeOptional?Description
namestringMandatoryThe path to form value (field)
componentReact.ComponentMandatoryComponent that will be rendered for field
validate(fieldValue: any) => undefined | stringOptionalField level validation function. It applies only field value on every change of field. Use for validation statically defined function, don't construct function in render body of a component

useForm

React hook for gettings all the utilities for the form in which component wrapped. This hook does not apply any arguments, and returns the FormContext values.

// ...
const {
  values,
  util,
  submit,
  // ...etcs
} = useForm();
// ...

useField

React hook for field rendering functional component

Arguments:

NameTypeOptional?Description
namestringMandatoryThe path to form value (field)
validate(fieldValue: any) => undefined | stringOptionalField level validation function. It applies only field value on every change of field. Use for validation statically defined function, don't construct function in render body of a component

FormContext

Context of current form

Values:

NameTypeDescription
valuesTFormValues extends ObjectCurrent values of form
utilTFormUtil extends ObjectCurrent util values of form
errorsObjectCurrent errors of form
hasErrorbooleanShows if form has any error
formErrorundefined \| stringMessage of the form error
submittedbooleanShows if the form where successfully submitted
change(fieldName: string, value: any) => voidChange field value function
update(RecursivePartial<TFormValues>) => voidOriginal update function from values block passed to the Form
submit() => voidSubmit action function
registerField(name: string, field: FieldHandler) => numberRegistering field function. Custom field component should be registered on mount
unregisterField(name: string, fieldId: number) => booleanUnregistering field function. Custom field component should be unregistered on unmount

Author

Ilya Melishnikov

LICENSE

MIT

1.2.0

2 years ago

1.2.1

2 years ago

1.1.0

2 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago