Easy form building library based on mobx-react
Easy form building library based on mobx-react
yarn add easy-modx-form
or
npm install easy-modx-form
Main form component. Setups contexts for the inner elements (such as inputs, buttons and other components).
parameters:
Form validation works along with per-field validation but have higher priority. Form validation could be asynchronous.
import React from "react";
import ReactDOM from "react-dom";
import { Form, createFormStore, Submit, FieldText } from "eazy-modx-form";
const form = createFormStore();
const App: React.FC = () => (
<Form
store={form}
initialValues={initializationFn}
submit={submitFn}
validate={validationFn}
>
<FieldText name="name" />
<Submit>Submit</Submit>
</Form>
);
ReactDOM.render(
App,
document.getElementById("external-module-root")
)
Basic text field component.
parameters:
...
<Form store={form}>
...
<FormText
name="name"
/>
...
</Form>
...
Basic textarea field component.
parameters: Same as for FieldText
Basic checkbox field component.
parameters: Same as for FieldText
Basic radio field component.
parameters: Same as for FieldText
value (number | string) - value of the radio element |
General component for build your own custom field components
parameters:
object
which will be passed into field renderer componentimport React from "react";
import { Field } from "easy-mobx-form";
type TBTextFieldData = {
placeholder?: string;
};
const TBTextFieldRenderer: FieldRenderer<TBTextFieldData> = (props) => {
const {
value,
error,
onChange,
disabled,
pristine,
forceError,
initializing,
name,
} = props;
const handleChange = (e: React.SyntheticEvent<HTMLInputElement>) => {
onChange(e.currentTarget.value);
};
return (
<>
<input
type="text"
name={name}
onChange={handleChange}
className="form-control"
value={value || ""}
disabled={disabled || initializing}
/>
{
error && (forceError || !pristine)
? <small class="form-text text-muted">{error}</small>
: null
}
</>
);
}
export const TBTextField: React.FC<BaseFieldComponent & {
data?: TBTextFieldData;
// custom properties
label: string;
}> = (props) => {
return (
<div class="form-group">
<label>{props.label}</label>
<Field
component={TBTextFieldRenderer}
name={props.name}
validate={props.validate}
data={props.data}
/>
</div>
);
};
Submit button
parameters:
button
elementimport React from "react";
import { Form, Submit } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Submit name="submit" value="update" className="btn btn-default">Update</Submit>
<Submit name="submit" value="delete" className="btn btn-danger">Delete</Submit>
</Form>
);
Reset button. Restore form values to the initial
parameters:
import React from "react";
import { Form, Reset } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Reset className="btn btn-default">Reset</Submit>
</Form>
);
Component for rendering the list of validation errors
parameters:
import React from "react";
import { Form, Errors } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Errors />
</Form>
);
By default will renders simple list of errors:
<ul>
<li>fieldName1: Error message 1</li>
<li>fieldName2: Error message 2</li>
</ul>
Component for rendering single error message (returned by validation)
parameters:
Wrapper component which children will be rendered only when form is initialized. Opposite to Initializing
```typescript jsx import React from “react”; import { Form, Initialized } from “eazy-modx-form”;
const App: React.FC = () => (
<Form store={form}>
### Initializing
Wrapper component which children will be rendered when form is initializing (i.e. loading data).
Opposite to [Initialized](#initialized)
```tsx
import React from "react";
import { Form, Initializing } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Initializing>
<SomeLoaderComponent />
</Initializing>
</Form>
);
Component which children will be rendered while form is submitting.
import React from "react";
import { Form, Submitting } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Submitting>
Submitting...
</Submitting>
</Form>
);
Helper component for rendering successful submit results.
properties:
import React from "react";
import { Form, SubmitSuccessComponent } from "eazy-modx-form";
type SubmitResult = {
message: string;
id: number;
};
const SubmitSucessRenderer: SubmitSuccessComponent<SubmitResult> = (props) => (
<div className="alert alert-success">
Success [{props.result.id}]: {props.result.message}
</div>
)
const App: React.FC = () => (
<Form store={form}>
<SubmitSuccessMessage
component={SubmitSucessRenderer}
/>
</Form>
);
Helper component for rendering submitting errors
properties:
import React from "react";
import { Form, SubmitErrorMessage } from "eazy-modx-form";
type SubmittingError = {
message: string;
};
const SubmitErrorRenderer: SubmitErrorComponent<SubmittingError> = (props) => (
<div className="alert alert-error">
Error: {props.result.message}
</div>
)
const App: React.FC = () => (
<Form store={form}>
<SubmitErrorComponent
component={SubmitErrorRenderer}
/>
</Form>
);
Renders its children while form is validation (i.e. when async validation is implemented)
import React from "react";
import { Form, Validating } from "eazy-modx-form";
const App: React.FC = () => (
<Form store={form}>
<Validating>
Wait please...
</Submitting>
</Form>
);
Component for in-place accessing to the form values
import React from "react";
import { Form, Values, createFormStore, Submit } from "eazy-modx-form";
type FormValues = {
firstName: string;
lastName: string;
};
const form = createFormStore<FormValues>();
const initialValues = {
firstName: "John",
lastName: "Wick",
};
const App: React.FC = () => (
<Form
store={form}
initialValues={initialValues}
>
<Values>
{(values: FormValues) => (
<div>
<div>First name: {values.firstName}</div>
<div>Last name: {values.lastName}</div>
</div>
)}
</Values>
<div>
<FormText name="firstName" />
<FormText name="lastName" />
</div>
<Submit>Submit</Submit>
</Form>
);