0.3.46 • Published 3 years ago

react-native-jsonschema-web-form v0.3.46

Weekly downloads
339
License
MIT
Repository
github
Last release
3 years ago

React Native Web JSONSchema Form

Dependencies Codacy Badge NPM

Render customizable forms using JSON schema for responsive websites and Expo apps (both iOS and Android). This library was inpired on react-jsonschema-form but was built with React Native and React Native Web in mind.

Table of Contents

Documentation

Coming soon!

Setup

React Native Web JSONSchema Form was created to facilitate the development of write once, run anywhere web and mobile apps. In order to accomplish that, this library is heavily based on React Native and React Native Web.

Requirements

First you need to install react ^16.8.3 (this library uses react-hooks).

yarn add react

If you're using Expo, they use a custom version of react-native and therefore you need to check what is the React Native repository for the Expo version you're using. For Expo v33.x.x you'd run:

yarn add https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz

If your project is also being used for web, please install React Native Web. Make sure your babel/webpack build replace all react-native imports with react-native-web (details here). If you used React Create App, aliasing is already taken care off for you.

yarn add react-dom react-native-web 

This library is backed by react-native-elements. Please make sure you have installed react-native-elements and its dependencies.

yarn add react-native-elements

Please make sure you have installed react-native-community/datetimepicker and its dependencies.

yarn add @react-native-community/datetimepicker

Please make sure you have installed react-native-image-picker and its dependencies.

yarn add react-native-image-picker

Installation

Install the library using yarn or npm.

yarn add react-native-web-jsonschema-form

Examples

Basic Form

import React from 'react';
import { useHistory } from 'react-router';
import { Alert } from 'react-native';
import { UIProvider, Form } from 'react-native-web-jsonschema-form';
import { Router, Switch } from 'react-router-dom';
// import { Router, Switch } from 'react-router-native';

const theme = {
  input: {
    focused: StyleSheet.create({
      border: {
        borderColor: 'yellow',
      },
    }),
  },
};

const schema = {
  type: 'object',
  properties: {
    username: { type: 'string' },
    password: { type: 'string' },
  },
};

const BasicForm = ({ formData, onChange }) => (
  <Form
    formData={formData}
    schema={schema}
    onChange={onChange}
  />
);

const ThemeWrapper = ({ children }) => {
  const history = useHistory();

  return (
    <UIProvider theme={theme} history={history}>
      {children}
    </UIProvider>
  );
};

const App = () => {
  const [formData, setFormData] = useState({});
  
  const onChange = (event) => setFormData({
    ...formData,
    [event.params.name]: event.params.value,
  });

  return (
    <Router>
      <Switch>
        <ThemeWrapper>
          <BasicForm
            formData={formData}
            onChange={onChange}
          />
        </ThemeWrapper>
      </Switch>
    </Router>
  );
};

Event Handlers

import React from 'React';
import PropTypes from 'prop-types';
import { Loading, Alert } from 'react-native-web-ui-components';
import { Form } from 'react-native-web-jsonschema-form';

class MyForm extends React.Component {
  static propTypes = {
    controller: PropTypes.string.isRequired,
    action: PropTypes.string.isRequired,
  };

  constructor(props) {
    super(props);
    this.state = {
      schema: null,
      message: null,
      posting: null,
    };
  }

  onSubmit = async (event) => {
    const { action, controller } = this.props;
    const { values } = event.params;
    this.setState({ posting: true });
    return fetch(`/${controller}/${action}`, {
      method: 'POST',
      body: JSON.stringify(values),
    });
  };

  onSuccess = async (event) => {
    const { response } = event.params;
    this.setState({
      posting: false,
      message: response.message,
    });
  };

  onError = async (event) => {
    // These are errors for fields that are not included in the schema
    const { exceptions } = event.params;
    const warning = Object.keys(exceptions).map(k => exceptions[k].join('\n'));
    this.setState({
      posting: false,
      message: warning.length ? warning.join('\n') : null,
    });
  };

  render() {
    const { schema, posting, message } = this.state;
    if (!schema) {
      const self = this;
      fetch(`/get-schema/${controller}/${action}`)
        .then((schema) => self.setState({ schema }));

      return <Loading />;
    }

    return (
      <React.Fragment>
        {posting ? <Loading /> : null}
        {message ? (
          <Alert>
            Message
          </Alert>
        ) : null}
        <Form
          schema={schema}
          onSubmit={this.onSubmit}
          onSuccess={this.onSuccess}
          onError={this.onError}
        />
      </React.Fragment>
    );
  }
}

Custom Theme

There are 5 input states: regular, focused, disabled, readonly and error. On which one of them you can define styles for background, border, text, placeholder, opacity, selected and unselected. These properties will be used accordingly by the widgets provided in this library. For example, selected and unselected will be used checkboxes and radioboxes to represent checked and unchecked.

const theme = {
  input: {
    focused: StyleSheet.create({
      border: {
        borderColor: 'yellow',
        borderWidth: 2,
        borderStyle: 'solid',
      },
      background: {
        backgroundColor: 'white',
      },
      text: {
        fontSize: 14,
        color: '#545454',
      },
      placeholder: {
        color: '#FAFAFA',
      },
      opacity: {
        opacity: 1,
      },
      selected: {
        color: 'blue',
      },
      unselected: {
        color: '#FAFAFA',
      },
    }),
    regular: {...},
    disabled: {...},
    readonly: {...},
    error: {...},
  },
};

const ThemeWrapper = ({ children }) => {
  const history = useHistory();

  return (
    <UIProvider theme={theme} history={history}>
      {children}
    </UIProvider>
  );
};

Form Validation

See https://github.com/CareLuLu/react-native-web-jsonschema-form/issues/139#issuecomment-654377982.

Array Fields

See https://github.com/CareLuLu/react-native-web-jsonschema-form/issues/113#issuecomment-621375353.

Props

The Form has the following props:

import React from 'react';
import Form from 'react-native-web-jsonschema-form';

const Example = ({
  // Misc
  name, // String to be used as id, if empty a hash will be used instead.
  onRef, // Function to be called with the form instance. This is NOT a DOM/Native element.
  scroller, // If provided, this will be passed to the widgets to allow disabling ScrollView during a gesture.
  wigdets, // Object with a list of custom widgets.

  
  // Data
  formData, // Initial data to populate the form. If this attribute changes, the form will update the data.
  filterEmptyValues, // If true, all empty and non-required fields will be omitted from the submitted values.

  // Schema
  schema, // JSON schema
  uiSchema, // JSON schema modifying UI defaults for schema
  errorSchema, // JSON schema with errors
  
  // Events
  // * All events can be synchronous or asynchronous functions.
  // * All events receive one parameter `event` with `name`, `preventDefault()` and `params`.
  onFocus,
  onChange,
  onSubmit,
  onCancel,
  onSuccess,
  onError,

  // Layout
  buttonPosition, // left, right, center
  submitButton, // If false, it will not be rendered. If it is a string, it will be used as the default button text.
  cancelButton, // If false, it will not be rendered. If it is a string, it will be used as the default button text.
  SubmitButton, // Component to render the submit button
  CancelButton, // Component to render the cancel button
}) => (
  <Form />
)
0.3.46

3 years ago

0.3.45

3 years ago

0.3.42

4 years ago

0.3.41

4 years ago

0.3.44

4 years ago

0.3.43

4 years ago

0.3.40

4 years ago

0.3.39

4 years ago

0.3.38

4 years ago

0.3.37

4 years ago

0.3.36

4 years ago

0.3.35

4 years ago

0.3.34

4 years ago

0.3.33

4 years ago

0.3.30

4 years ago

0.3.29

4 years ago

0.3.28

4 years ago

0.3.27

4 years ago

0.3.26

4 years ago

0.3.25

4 years ago

0.3.24

4 years ago

0.3.23

4 years ago

0.3.22

4 years ago

0.3.21

4 years ago

0.3.20

4 years ago

0.3.19

4 years ago

0.3.18

4 years ago

0.3.17

4 years ago

0.3.16

4 years ago

0.3.15

4 years ago

0.3.14

4 years ago

0.3.13

4 years ago

0.3.12

4 years ago

0.3.11

4 years ago

0.3.10

4 years ago

0.3.9

4 years ago

0.3.8

4 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.99

4 years ago

0.2.98

4 years ago

0.2.97

4 years ago

0.2.96

4 years ago

0.2.95

4 years ago

0.2.94

4 years ago

0.2.93

4 years ago

0.2.92

4 years ago

0.2.91

4 years ago

0.2.90

4 years ago

0.2.89

4 years ago

0.2.88

4 years ago

0.2.87

4 years ago

0.2.86

4 years ago

0.2.85

4 years ago

0.2.84

4 years ago

0.2.83

4 years ago

0.2.82

4 years ago

0.2.81

4 years ago

0.2.80

4 years ago

0.2.79

4 years ago

0.2.78

4 years ago

0.2.77

4 years ago

0.2.74

4 years ago

0.2.76

4 years ago

0.2.75

4 years ago

0.2.73

4 years ago

0.2.72

4 years ago

0.2.71

4 years ago

0.2.70

4 years ago

0.2.69

4 years ago

0.2.68

4 years ago

0.2.67

4 years ago

0.2.66

4 years ago

0.2.63

4 years ago

0.2.62

4 years ago

0.2.65

4 years ago

0.2.64

4 years ago

0.2.61

4 years ago

0.2.59

4 years ago

0.2.58

4 years ago

0.2.57

4 years ago

0.2.56

4 years ago

0.2.55

4 years ago

0.2.54

4 years ago

0.2.53

4 years ago

0.2.52

4 years ago

0.2.51

4 years ago

0.2.50

4 years ago

0.2.41

4 years ago

0.2.49

4 years ago

0.2.48

4 years ago

0.2.47

4 years ago

0.2.46

4 years ago

0.2.45

4 years ago

0.2.44

4 years ago

0.2.43

4 years ago

0.2.42

4 years ago

0.2.40

4 years ago

0.2.39

4 years ago

0.2.38

4 years ago

0.2.37

4 years ago

0.2.36

4 years ago

0.2.35

4 years ago

0.2.34

4 years ago

0.2.33

4 years ago

0.2.32

4 years ago

0.2.31

4 years ago

0.2.30

4 years ago

0.2.29

4 years ago

0.2.28

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.3

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.1.97

4 years ago

0.1.98

4 years ago

0.1.99

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.96

4 years ago

0.1.95

4 years ago

0.1.93

4 years ago

0.1.94

4 years ago

0.1.92

4 years ago

0.1.91

4 years ago

0.1.90

4 years ago

0.1.86

4 years ago

0.1.87

4 years ago

0.1.88

4 years ago

0.1.89

4 years ago

0.1.85

4 years ago

0.1.81

4 years ago

0.1.82

4 years ago

0.1.83

4 years ago

0.1.84

4 years ago

0.1.80

4 years ago

0.1.78

4 years ago

0.1.79

4 years ago

0.1.77

4 years ago

0.1.76

4 years ago

0.1.75

4 years ago

0.1.74

4 years ago

0.1.73

4 years ago

0.1.70

4 years ago

0.1.71

4 years ago

0.1.72

4 years ago

0.1.64

4 years ago

0.1.65

4 years ago

0.1.66

4 years ago

0.1.67

4 years ago

0.1.68

4 years ago

0.1.69

4 years ago

0.1.63

4 years ago

0.1.62

4 years ago

0.1.61

4 years ago

0.1.59

4 years ago

0.1.60

4 years ago

0.1.58

4 years ago

0.1.57

4 years ago

0.1.56

4 years ago

0.1.55

4 years ago

0.1.53

4 years ago

0.1.54

4 years ago

0.1.52

4 years ago

0.1.51

4 years ago

0.1.50

4 years ago

0.1.49

4 years ago

0.1.48

4 years ago

0.1.47

4 years ago

0.1.45

4 years ago

0.1.46

4 years ago

0.1.44

4 years ago

0.1.43

4 years ago

0.1.41

4 years ago

0.1.42

4 years ago

0.1.40

4 years ago

0.1.39

4 years ago

0.1.37

4 years ago

0.1.36

4 years ago

0.1.35

4 years ago

0.1.34

4 years ago

0.1.30

4 years ago

0.1.31

4 years ago

0.1.32

4 years ago

0.1.33

4 years ago

0.1.27

4 years ago

0.1.28

4 years ago

0.1.29

4 years ago

0.1.20

4 years ago

0.1.21

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.24

4 years ago

0.1.25

4 years ago

0.1.26

4 years ago

0.0.86

4 years ago

0.0.87

4 years ago

0.0.88

4 years ago

0.0.89

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.18

4 years ago

0.1.19

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.15

4 years ago

0.1.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.9

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.0.95

4 years ago

0.0.96

4 years ago

0.0.97

4 years ago

0.0.98

4 years ago

0.0.99

4 years ago

0.0.90

4 years ago

0.0.91

4 years ago

0.0.92

4 years ago

0.0.93

4 years ago

0.0.94

4 years ago

0.0.85

4 years ago

0.0.84

4 years ago

0.0.83

4 years ago

0.0.80

4 years ago

0.0.81

4 years ago

0.0.82

4 years ago

0.0.79

4 years ago

0.0.78

4 years ago

0.0.74

4 years ago

0.0.75

4 years ago

0.0.76

4 years ago

0.0.77

4 years ago

0.0.73

4 years ago

0.0.72

4 years ago

0.0.71

4 years ago

0.0.70

4 years ago

0.0.69

4 years ago

0.0.68

4 years ago

0.0.67

4 years ago

0.0.66

4 years ago

0.0.64

4 years ago

0.0.65

4 years ago

0.0.63

4 years ago

0.0.62

4 years ago

0.0.60

4 years ago

0.0.61

4 years ago

0.0.59

4 years ago

0.0.58

4 years ago

0.0.57

4 years ago

0.0.55

4 years ago

0.0.56

4 years ago

0.0.54

4 years ago

0.0.53

4 years ago

0.0.52

4 years ago

0.0.51

4 years ago

0.0.50

4 years ago

0.0.48

4 years ago

0.0.49

4 years ago

0.0.47

4 years ago

0.0.45

4 years ago

0.0.46

4 years ago

0.0.42

4 years ago

0.0.43

4 years ago

0.0.44

4 years ago

0.0.41

4 years ago

0.0.40

4 years ago

0.0.37

4 years ago

0.0.38

4 years ago

0.0.39

4 years ago

0.0.35

4 years ago

0.0.36

4 years ago

0.0.34

4 years ago

0.0.33

4 years ago

0.0.32

4 years ago

0.0.31

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.28

4 years ago

0.0.27

4 years ago

0.0.26

4 years ago

0.0.25

4 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.15

4 years ago

0.0.12

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago