Jafar

Jafar

  • Docs
  • Form Demos
  • Components Demos
  • Layout Demos
  • Editor
  • GitHub

›React Form

Getting started

  • Introduction

Form Class

  • Installation
  • Overview
  • Arguments
  • Id
  • Data
  • Fields
  • Path
  • Component
  • Formatter & Parser
  • Dependencies
  • Required
  • Validators
  • Exclude Term
  • Disable Term
  • Require Term
  • Dependencies Change
  • Context
  • Dirty
  • Invalid
  • Errors
  • Processing
  • Pending Actions
  • Hooks
  • Term
  • Log
  • Error Codes
  • Actions
  • Definition Shorthand

Advanced Usages

  • Grid Usage
  • Form Persistency
  • Undo & Redo
  • Replay Actions
  • Track Actions
  • Server Validation
  • Entity Templates
  • Folder Structure

React Form

  • Installation
  • Overview
  • Form
  • Field
  • Components
  • Layout
  • Demos

More Info

  • Test Jafar
  • Packages
  • Why Jafar?
  • Videos
  • Channels

Contribute

  • Code of Conduct
  • Contributing Guide
  • Contributors

Field

Field component is a specific child component that uses the Form's context to get data from the form and change the form data.

Field Component

@jafar/react-form/Field serves as a default field component. Its the result of withContext(SomeFieldView) hoc (that pass the underline field view component all the data that the field view needs to render) and a default FieldView component:

import withContext from './field-with-context';
import FieldView from './FieldView';

export default withContext(FieldView);

Props

NameTypeDescription
idrequired stringField id as represented in the json of model.fields object keys

Note: Field component propagates every property to the component it wraps, except for jafar-reserved properties, that appear in Field View. However, specifying the property outside of Jafar's context also means it is not part of the lifeycycle of Jafar any more. Therefore, we strongly recommend to use Jafar Form configuration field.component.state object to pass data for each underline component.

Usage Example

import { Form, Field } from '@jafar/react-form';
import SomeInputText from './my-components/Text.jsx';

const model = {
  id: 'user-form',
  fields: {
    firstName: {
      path: 'firstName',
      label: 'First Name',
      component: {
        name: 'InputText',
      },
    },
    lastName: {
      path: 'lastName',
      label: 'Last Name',
      component: {
        name: 'InputText',
      },
    }
  }
};

const resources = {
  components: {
    InputText: : { renderer: SomeInputText },
  }
};

<Form model={model} resources={resources}>
  <h2>User Details</h2>
  <Field id="firstName" />
  <Field id="lastName" />
</Form>

Structure

field-structure

withContext(FieldView) HOC

A function that gets a FieldView component as a parameter and returns a new wrapped component, which receives data from the closest Form Provider's context using the id prop, and populates props for the FormView from its form's context.model.fields[id].

Field View

Renders the specified component (for instance, inputText, checkboxList, etc) and some metadata like label, description and errors. Jafar provides a default FieldView but any app can design its own field view and use it like const Field = withContext(MyCustomFieldView;

Note

The fields in model.fields represent the form. Each field defines a settings that will affect its lifecycle. If you choose NOT to include a specific field in the render function - the field's lifecycle is still evaluated (meaning that excludeTerm, validations and disableTerm are evaluated nonetheless). Keep in mind that all fields are evaluated even without being rendered.

Props

NameTypeDescription
idrequired stringField id
labelstringField label
descriptionstringField info
componentfunction / classfunction. React ui component which manipulates the field value. Component should get a set of predefined props as described in Unified api components
valueanyValue of the field, supplied from the model.data
stateobjectRepresents the ui state of the field's component
excludedbooleanIs the field excluded? (and not rendered)
disabledbooleanIs the field disabled?
dirtybooleanIs the field value not equals to its initial value?
requiredbooleanIs the field required to have a valid value?
emptybooleanIs the field value considered empty? (according to isEmpty function)
invalidbooleanIs the field invalid? (if a field is required and empty - and have required error, its not considered invalid in terms of UX
errorsobject arrayCurrent field errors - array of { name, message }
onValueChangefunctionCallback to pass new value for the field
onStateChangefunctionCallback to pass new component state for the field

Our Field View

For the following form definition:

import CheckboxCollection from '@jafar/react-components/edit/CheckboxCollection';

const form = {
  id: 'user-form',
  fields: {
    hobbies: {
      label: 'Hobbies',
      path: 'hobbies',
      description: 'Select at least 3 hobbies',
      component: {
        name: 'CheckboxCollection',
        state: {
          items: [
            { label: 'Basketball', value: 'BASKETBALL'},
            { label: 'Football', value: 'FOOTBALL'},
            { label: 'Shop', value: 'SHOP'},
            { label: 'Fashion', value: 'FASHION'},
            { label: 'Cook', value: 'COOK'},
          ]
        },
      },
      required: true,
      validators: [{
        name: 'minLength',
        args: {
          value: 3,
        }
      }]
    },
  },
  data: {
    hobbies: ['SHOP', 'FASHION'],
  }
};

const resources = {
  components: {
    CheckboxCollection: { renderer: CheckboxCollection },
  }
}

FieldView will render:

field-image

Custom Field View

Defined a custom FieldView component. Define a brand new component, or extend the default FieldView component, and override just some of the render functions.

Note: in our default FieldView exposed component we implemented shouldComponentUpdate to avoid unnecessary field renders. You should use the same approach in a custom field view. Another short option will be to extend our default FieldView and override only the render function or some of getHeader, getBody and getFooter class functions.

Example

import CheckboxCollection from '@jafar/react-components/edit/CheckboxCollection';
import Text from '@jafar/react-components/edit/Text';
import { Form, withContext} from '@jafar/react-form';
import CustomFieldView from '../views/CustomFieldView';

// create field with custom field view
const Field = withContext(CustomFieldView);

const form = {
  id: 'user-form',
  fields: {
    name: {
      path: 'name',
      label: 'Name',
      description: 'Enter user name',
      component: {
        name: 'Text',
      },
      required: true,
    },
    hobbies: {
      label: 'Hobbies',
      path: 'hobbies',
      description: 'Select user hobbies',
      component: {
        name: 'CheckboxCollection',
        state: {
          items: [
            { label: 'Basketball', value: 'BASKETBALL'},
            { label: 'Football', value: 'FOOTBALL'},
            { label: 'Shop', value: 'SHOP'},
            { label: 'Fashion', value: 'FASHION'},
            { label: 'Cook', value: 'COOK'},
          ]
        },
      },
    },
  },
  data: {
    hobbies: ['SHOP', 'FASHION'],
  }
};

const resources = {
  components: {
    CheckboxCollection: { renderer: CheckboxCollection },
    Text: { renderer: Text },
  }
}

<Form model={model} resources={resources}>
  <Field id="name" />
  <Field id="hobbies" />
</Form>

Example custom-field-view

You have more detailed example in our form demos

← FormComponents →
  • Field Component
    • Props
    • Usage Example
    • Structure
  • withContext(FieldView) HOC
  • Field View
    • Props
    • Our Field View
    • Custom Field View
Jafar
Copyright © 2020 Verizon Media
DOCS
IntroductionForm ClassReact FormReact ComponentsReact Layout
DEMOS
React FormReact ComponentsReact Layout
CONTRIBUTING
Code Of ConductContributing GuideUsers
CHANNELS
StarGitHubStack OverflowMediumVideos