Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@traveloka/react-schema-form

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@traveloka/react-schema-form

## Installation yarn ``` yarn add @traveloka/react-schema-form ``` npm ``` npm i @traveloka/react-schema-form --save ```

  • 3.6.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
323
decreased by-12.7%
Maintainers
3
Weekly downloads
 
Created
Source

React Schema

Installation

yarn

yarn add @traveloka/react-schema-form

npm

npm i @traveloka/react-schema-form --save

Motivation

Form is common in any application. The final result of form is data. This library would separate the UI and Form logic by giving basic API to Field Component. The flow how to get the data, would be the responsible of the Field Component.

Getting Started

Basic Usage

// InputField.js

import React from 'react';

export default function InputField({ onChange, value, ...rest }) {
  return (
    <input onChange={(e) => onChange(e.target.value)} value={value} {...rest} />
  );
}
// LoginForm.js

import React from 'react';
import { Form, Field } from '@traveloka/react-schema-form';  // ES6
import InputField from './InputField';

export default class LoginForm extends React.Component {

  form = null;

  render() {
    return (
      <React.Fragment>
        <Form fieldRef={(el) => form = el}>
          <Field name="email" component={InputField} />
          <Field name="password" component={InputField} type="password"/>
        </Form>
        <button onClick={this.handleSubmit}>Submit</button>
      </React.Fragment>
    );
  }

  handleSubmit = () => {
    this.props.onSubmit(this.form.getValues());
  }
}

Documentation

- Field

Importing
var Field = require('@traveloka/react-schema-form').Field;  // ES5

import { Field } from '@traveloka/react-schema-form';  // ES6
Props
PropertyTypeDefault ValueDescription
namestringnonerequired
fieldRefReact.createRefnoneto be able access field methods.
componentReact.ClassComponentnonerequired
revalidateOnErrorbooleantruewill auto validate when have an error
validateOnChangebooleanfalsewill auto validate when value is changes
normalizefunction(value) => valuewill serialize the value
defaultValueanydefault value.
rulesfuncarray of func
Methods
MethodsDescription
getValue() => anyreturn current field value
setValue(newValue: any) => anyset current field value
getError() => ValidationResultreturn current field error message
setError(error: ValidationResult) => ValidationResultset current field error
validate() => ValidationResulttrigger field to validate
reset() => voidreset field to defaultValue, and remove error
initialize(value: any) => anyset field defaultValue
getDefaultValue() => anyget field defaultValue
hasError() => booleanreturn true if field have an error
isDirty() => booleanreturn true if field value is not same with defaultValue
Given Props to Field Component
MethodsDescription
name: Stringthe Field name
label: StringUC Words of name
isDirty: Booleanstate of the changes value of field
`error: Stringnull`
onChange: (v) => anyfunction to change field value
value: anythe value of field
Usage
1. A Component

This can be any component class that you have written or have imported from a third party library.

/// MyCustomInput.js
import React, { Component } from 'react'

class MyCustomInput extends Component {
  static propTypes = {
    value: PropTypes.any,
    onChange: PropTypes.func,
    isDirty: PropTypes.bool,
    error: PropTypes.string,
  }

  render() {
    const { value, onChange } = this.props
    return (
      <div>
        <span>The current value is {value}.</span>
        <button type="button" onClick={() => onChange(value + 1)}>Inc</button>
        <button type="button" onClick={() => onChange(value - 1)}>Dec</button>
      </div>
    )
  }
}

Then, somewhere in your form...

import MyCustomInput from './MyCustomInput'
...
<Field name="myField" component={MyCustomInput}/>
2. Validation

Field accept rules as props. Rules can be a function / array of function. Each function accept one arguments, the value. The function just need to return null / string. The return value would be the error message for the field component (eg: if there's no error, return null).

// MyErrorableInput.js

import React from 'react';

export default function MyErrorableInput({ value, onChange, error, ...rest }) {
  return (
    <div>
      <input value={value} onChange={(e) => onChange(e.target.value)} {...rest} />
      {!!error && <div className="error">{error}</div>}
    </div>
  );
}

For rule functions

// isEmail.js
export default function isEmail(value) {
  return value.contains('@') ? null : `${value} is not an email format.`;
}

// maxLength.js
const maxLength = (length) => (value) => {
  return value && value.length <= length ? null : `Length must not exceed ${length} chars.`;
}

Then somewhere in your form...

import isEmail from './isEmail';
import maxLength from './maxLength';
import MyErrorableInput from './MyErrorableInput';

<Field name="email" component={MyCustomInput} rules={[isEmail, maxLength(10)]}>

- Form

Props
PropertyTypeDefault ValueDescription
arraybooleanfalsewould return the values as array
normalizefunction(val) => vala function that normalize the form values
fieldRefReact.createRefnoneto be able access field methods.
Methods
MethodsDescription
getValue() => ValueByFieldNamereturn form value by fieldName
setValue(newValue: ValueByFieldName) => ValueByFieldNameset form value by fieldName
getError() => ValidationResultByFieldNamereturn form error message by fieldName
setError(error: ValidationResultByFieldName) => ValidationResultByFieldNameset form error by fieldName
validate() => ValidationResultByFieldNametrigger form to validate
reset() => voidreset form to defaultValue, and remove error
initialize(value: ValueByFieldName) => anyset form defaultValue by fieldName
getDefaultValue() => ValueByFieldNameget defaultValue by fieldName
hasError() => booleanreturn true if some field have an error
hasErrorField(fieldName: string) => booleanreturn true if field have an error
isDirty() => booleanreturn true if some field value is not same with defaultValue
isDirtyField(fieldName: string) => booleanreturn true if field value is not same with field defaultValue
getValueField(fieldName: string) => anyreturn field value
setValueField(fieldName: string, value: any) => anyset field value
getErrorField(fieldName: string) => ValidationResultreturn field error message
setErrorField(fieldName: string, error: ValidationResult) => ValidationResultset field error message
validateField(fieldName: string) => ValidationResultvalidate specify field
Children Function Params
PropertyTypeDescription
values[name: string]: anyform value by fieldName
isDirtybooleantrue if some field value is not same with defaultValue
hasErrorbooleantrue if some field have an error
error[name: string]: ValidationResultform error message by fieldName

Nested Forms

You could also build a nested form like this

<Form fieldRef={form => this.form = form}>
  <div>
    <Field name="email" component={InputField} />
    <Form name="profile">
      <Field name="first" component={InputField} />
      <Field name="last" component={InputField} />
    </Form>
  </div>
</Form>

// when you get value
this.form.getValue()
{
  email: ...,
  profile: {
    first: ...,
    last: ...
  }
}

Playground

  • Example 1 Edit [@traveloka/react-schema-form] #1
  • Example 2 Edit [@traveloka/react-schema-form] #2

FAQs

Package last updated on 01 Dec 2023

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