0.1.0-alpha.40 • Published 6 months ago

protobuf-auto-form v0.1.0-alpha.40

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

profobuf-auto-form

React component that renders a form corresponding to protobuf schema.

thumbnail

How to use

Using the protobuf schema below,

enum ArticleType {
    SIMPLE = 1;
    DETAILED = 2;
}

message Article {
    string title = 1;
    ArticleType type = 2;
    optional string content = 3;
    repeated string tags = 4;
}

Basic usage

AutoForm required protobufjs reflection object. You can find the documentation here.

// obtaining protobuf namespace object
const namespace = protobuf.Namespace.fromJson('', jsonDescriptor)

// jsx
<AutoForm
    messageType="Article"
    namespace={namespace}
    onSubmitValid={(result) => {
        // handle result
    }}
>
    <button type="submit" className="btn btn-xs btn-accent">Submit</button>
</AutoForm>

Providing initial values

The initial value must to be a plain JSON object which is a valid protobuf message.

<AutoForm
    messageType="Article"
    namespace={namespace}
    initialState={/* initial value here */}
    onSubmitValid={(result) => {
        // handle result
    }}
>
    <button type="submit" className="btn btn-xs btn-accent">Submit</button>
</AutoForm>

Overriding input component

There are two way of overriding input component. 1. Overriding field 2. Overriding type

Example

// Overriding input field
const LimittedInput: React.FC<OverriddenFieldProps<string>> = ({
  value,
  onChange,
}) => (
  <input
    value={value}
    maxLength={10} // whatever you want
    onChange={(e) => {
      onChange(e.target.value);
    }}
  />
);

// 1. field override
<AutoForm
    messageType="Article"
    namespace={namespace}
    fieldOverride={{
        title: LimittedInput
    }}
    onSubmitValid={(result) => {
        // handle result
    }}
>
    <button type="submit" className="btn btn-xs btn-accent">Submit</button>
</AutoForm>

// 2. type override
<AutoForm
    messageType="Article"
    namespace={namespace}
    typeOverride={{
        string: LimittedInput
    }}
    onSubmitValid={(result) => {
        // handle result
    }}
>
    <button type="submit" className="btn btn-xs btn-accent">Submit</button>
</AutoForm>
0.1.0-alpha.40

6 months ago

0.1.0-alpha.38

11 months ago

0.1.0-alpha.37

11 months ago

0.1.0-alpha.39

10 months ago

0.1.0-alpha.32

1 year ago

0.1.0-alpha.31

1 year ago

0.1.0-alpha.34

12 months ago

0.1.0-alpha.33

1 year ago

0.1.0-alpha.36

12 months ago

0.1.0-alpha.35

12 months ago

0.1.0-alpha.30

1 year ago

0.1.0-alpha.27

1 year ago

0.1.0-alpha.26

1 year ago

0.1.0-alpha.29

1 year ago

0.1.0-alpha.28

1 year ago

0.1.0-beta.1

1 year ago

0.1.0-alpha.14

1 year ago

0.1.0-alpha.13

1 year ago

0.1.0-alpha.16

1 year ago

0.1.0-alpha.15

1 year ago

0.1.0-alpha.18

1 year ago

0.1.0-alpha.17

1 year ago

0.1.0-alpha.19

1 year ago

0.1.0-alpha.21

1 year ago

0.1.0-alpha.20

1 year ago

0.1.0-alpha.23

1 year ago

0.1.0-alpha.22

1 year ago

0.1.0-alpha.25

1 year ago

0.1.0-alpha.24

1 year ago

0.1.0-alpha.12

2 years ago

0.1.0-alpha.11

2 years ago

0.1.0-alpha.10

2 years ago

0.1.0-alpha.9

2 years ago

0.1.0-alpha.8

2 years ago

0.1.0-alpha.6

2 years ago

0.1.0-alpha.5

2 years ago

0.1.0-alpha.4

2 years ago

0.1.0-alpha.3

2 years ago

0.1.0-alpha.2

2 years ago

0.1.0-alpha.1

2 years ago