@reactables/react-forms 0.4.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/Components/ContactForm.d.ts +5 -0
- package/dist/Components/Field.d.ts +27 -0
- package/dist/Components/Form.d.ts +16 -0
- package/dist/Components/Form.stories.d.ts +10 -0
- package/dist/Components/FormArray.d.ts +12 -0
- package/dist/Components/Input.d.ts +6 -0
- package/dist/Components/index.d.ts +3 -0
- package/dist/Testing/AsyncValidators/blacklistedEmail.d.ts +2 -0
- package/dist/Testing/Models/Contact.d.ts +5 -0
- package/dist/Testing/Validators/arrayLengthRequired.d.ts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +114 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Storybook at: [https://reactables.github.io/reactables/](https://reactables.github.io/reactables/).
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import React, { ChangeEvent, DragEvent, FocusEvent } from 'react';
|
|
2
|
+
import { ControlModels } from '@reactables/forms';
|
|
3
|
+
export type EventHandler<Event> = (event: Event, name?: string) => void;
|
|
4
|
+
export interface CommonFieldInputProps {
|
|
5
|
+
name: string;
|
|
6
|
+
onDragStart?: EventHandler<DragEvent<unknown>>;
|
|
7
|
+
onDrop?: EventHandler<DragEvent<unknown>>;
|
|
8
|
+
onFocus?: EventHandler<FocusEvent<unknown>>;
|
|
9
|
+
}
|
|
10
|
+
export interface EventOrValueHandler<Event> extends EventHandler<Event> {
|
|
11
|
+
(value: unknown): void;
|
|
12
|
+
}
|
|
13
|
+
export interface WrappedFieldInputProps extends CommonFieldInputProps {
|
|
14
|
+
value: any;
|
|
15
|
+
onBlur: EventOrValueHandler<FocusEvent<unknown>>;
|
|
16
|
+
onChange: EventOrValueHandler<ChangeEvent<unknown>>;
|
|
17
|
+
}
|
|
18
|
+
export interface WrappedFieldProps {
|
|
19
|
+
input: WrappedFieldInputProps;
|
|
20
|
+
meta: ControlModels.FormControl<unknown>;
|
|
21
|
+
}
|
|
22
|
+
export interface FieldProps {
|
|
23
|
+
component: React.JSXElementConstructor<WrappedFieldProps>;
|
|
24
|
+
name?: string;
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
export declare const Field: ({ component: Component, name, ...props }: FieldProps) => React.JSX.Element;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AbstractControlConfig, ControlModels, RxFormActions } from '@reactables/forms';
|
|
3
|
+
export declare const FormContext: React.Context<{
|
|
4
|
+
state: ControlModels.Form<unknown>;
|
|
5
|
+
actions: RxFormActions;
|
|
6
|
+
}>;
|
|
7
|
+
export interface FormChildrenProps {
|
|
8
|
+
state: ControlModels.Form<unknown>;
|
|
9
|
+
actions: RxFormActions;
|
|
10
|
+
}
|
|
11
|
+
interface FormProps {
|
|
12
|
+
formConfig: AbstractControlConfig;
|
|
13
|
+
children?: (props: FormChildrenProps) => React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
export declare const Form: ({ formConfig, children }: FormProps) => React.JSX.Element;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { Form } from './Form';
|
|
3
|
+
declare const meta: Meta<typeof Form>;
|
|
4
|
+
export default meta;
|
|
5
|
+
type Story = StoryObj<typeof Form>;
|
|
6
|
+
export declare const BasicControl: Story;
|
|
7
|
+
export declare const Validation: Story;
|
|
8
|
+
export declare const AsyncValidation: Story;
|
|
9
|
+
export declare const FormArrays: Story;
|
|
10
|
+
export declare const ResetForm: Story;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { AbstractControlConfig, ControlRef, ControlModels } from '@reactables/forms';
|
|
3
|
+
export interface FormArrayChildrenProps {
|
|
4
|
+
items: ControlModels.FormControl<unknown>[];
|
|
5
|
+
addControl: (config: AbstractControlConfig) => void;
|
|
6
|
+
removeControl: (controlRef: ControlRef) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface FormArrayProps {
|
|
9
|
+
name?: string;
|
|
10
|
+
children?: (props: FormArrayChildrenProps) => React.ReactNode;
|
|
11
|
+
}
|
|
12
|
+
export declare const FormArray: ({ name, children }: FormArrayProps) => React.JSX.Element;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { WrappedFieldProps } from './Field';
|
|
3
|
+
export interface InputProps extends WrappedFieldProps {
|
|
4
|
+
label?: string | React.ReactElement;
|
|
5
|
+
}
|
|
6
|
+
export declare const Input: ({ input, meta: { touched, errors, pending, valid }, label }: InputProps) => React.JSX.Element;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var forms = require('@reactables/forms');
|
|
7
|
+
var reactHelpers = require('@reactables/react-helpers');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
+
|
|
11
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
12
|
+
|
|
13
|
+
var FormContext = React__default["default"].createContext(null);
|
|
14
|
+
var Form = function (_a) {
|
|
15
|
+
var formConfig = _a.formConfig, children = _a.children;
|
|
16
|
+
var _b = reactHelpers.useReactable(forms.RxForm.build(formConfig)), state = _b.state, actions = _b.actions;
|
|
17
|
+
var formChildrenProps = {
|
|
18
|
+
state: state,
|
|
19
|
+
actions: actions
|
|
20
|
+
};
|
|
21
|
+
return (React__default["default"].createElement(FormContext.Provider, { value: {
|
|
22
|
+
state: state,
|
|
23
|
+
actions: actions
|
|
24
|
+
} }, state !== undefined && children && children(formChildrenProps)));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/******************************************************************************
|
|
28
|
+
Copyright (c) Microsoft Corporation.
|
|
29
|
+
|
|
30
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
31
|
+
purpose with or without fee is hereby granted.
|
|
32
|
+
|
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
34
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
35
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
36
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
37
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
38
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
39
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
40
|
+
***************************************************************************** */
|
|
41
|
+
|
|
42
|
+
var __assign = function() {
|
|
43
|
+
__assign = Object.assign || function __assign(t) {
|
|
44
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
45
|
+
s = arguments[i];
|
|
46
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
47
|
+
}
|
|
48
|
+
return t;
|
|
49
|
+
};
|
|
50
|
+
return __assign.apply(this, arguments);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function __rest(s, e) {
|
|
54
|
+
var t = {};
|
|
55
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
56
|
+
t[p] = s[p];
|
|
57
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
58
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
59
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
60
|
+
t[p[i]] = s[p[i]];
|
|
61
|
+
}
|
|
62
|
+
return t;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
66
|
+
var e = new Error(message);
|
|
67
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
var Field = function (_a) {
|
|
71
|
+
var Component = _a.component, _b = _a.name, name = _b === void 0 ? 'root' : _b, props = __rest(_a, ["component", "name"]);
|
|
72
|
+
var _c = React.useContext(FormContext), state = _c.state, _d = _c.actions, markControlAsTouched = _d.markControlAsTouched, updateValues = _d.updateValues;
|
|
73
|
+
var _e = state[name], controlRef = _e.controlRef, touched = _e.touched, value = _e.value;
|
|
74
|
+
var inputProps = {
|
|
75
|
+
name: name,
|
|
76
|
+
value: value,
|
|
77
|
+
onBlur: function () {
|
|
78
|
+
if (!touched)
|
|
79
|
+
markControlAsTouched({ controlRef: controlRef });
|
|
80
|
+
},
|
|
81
|
+
onChange: function (event) {
|
|
82
|
+
updateValues({
|
|
83
|
+
controlRef: controlRef,
|
|
84
|
+
value: event.currentTarget.value
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return React__default["default"].createElement(Component, __assign({ input: inputProps, meta: state[name] }, props));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
var FormArray = function (_a) {
|
|
92
|
+
var _b = _a.name, name = _b === void 0 ? 'root' : _b, children = _a.children;
|
|
93
|
+
var _c = React.useContext(FormContext), state = _c.state, _d = _c.actions, addControl = _d.addControl, removeControl = _d.removeControl;
|
|
94
|
+
var controlRef = state[name].controlRef;
|
|
95
|
+
var items = forms.getArrayItems(controlRef, state);
|
|
96
|
+
var formArrayChildrenProps = {
|
|
97
|
+
items: items,
|
|
98
|
+
addControl: function (config) {
|
|
99
|
+
addControl({ controlRef: controlRef, config: config });
|
|
100
|
+
},
|
|
101
|
+
removeControl: removeControl
|
|
102
|
+
};
|
|
103
|
+
return React__default["default"].createElement("div", null, children && children(formArrayChildrenProps));
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
exports.Field = Field;
|
|
107
|
+
exports.Form = Form;
|
|
108
|
+
exports.FormArray = FormArray;
|
|
109
|
+
Object.keys(forms).forEach(function (k) {
|
|
110
|
+
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
get: function () { return forms[k]; }
|
|
113
|
+
});
|
|
114
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@reactables/react-forms",
|
|
3
|
+
"description": "React forms with reactables",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rimraf dist && rollup --config",
|
|
10
|
+
"lint": "eslint --max-warnings 0 \"src/**/*.ts*\" && prettier --check src/",
|
|
11
|
+
"fix": "eslint --fix \"src/**/*.ts*\" && prettier --write \"src/**/*.(ts*|scss)\"",
|
|
12
|
+
"storybook": "storybook dev -p 6006",
|
|
13
|
+
"build-storybook": "storybook build",
|
|
14
|
+
"test-storybook": "test-storybook"
|
|
15
|
+
},
|
|
16
|
+
"author": "David Lai",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@reactables/core": "*",
|
|
20
|
+
"@reactables/forms": "^0.4.0-alpha.1",
|
|
21
|
+
"@reactables/react-helpers": "*"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
25
|
+
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0",
|
|
26
|
+
"rxjs": "^6.0.0 || ^7.0.0"
|
|
27
|
+
},
|
|
28
|
+
"version": "0.4.0-alpha.1"
|
|
29
|
+
}
|