@mirai/ui 1.0.145 → 1.0.146
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
CHANGED
|
@@ -205,6 +205,37 @@ const MyComponent = () => (
|
|
|
205
205
|
--mirai-ui-radio-size: var(--mirai-ui-space-L);
|
|
206
206
|
```
|
|
207
207
|
|
|
208
|
+
### ScrollView
|
|
209
|
+
|
|
210
|
+
This primitive is used to make vertically scrollable views and receives the following props:
|
|
211
|
+
|
|
212
|
+
- `behavior:string` type of scroll animation
|
|
213
|
+
- `children:node`
|
|
214
|
+
- `height:number` height value
|
|
215
|
+
- `horizontal:boolean` layout direction
|
|
216
|
+
- `scrollEventThrottle:number` scroll timeout value
|
|
217
|
+
- `scrollIndicator:boolean` if true the scroll indicator is shown
|
|
218
|
+
- `scrollTo:number` coordinate to scroll to
|
|
219
|
+
- `snap:bool` if you want use the CSS feature `scroll-snap`
|
|
220
|
+
- `tag:string` html tag of resulting element
|
|
221
|
+
- `width:number` width value
|
|
222
|
+
- `onscroll:function` executed when user scrolls over the element
|
|
223
|
+
|
|
224
|
+
```jsx
|
|
225
|
+
import { ScrollView, View } from '@mirai/ui';
|
|
226
|
+
|
|
227
|
+
const MyComponent = ({ isDesktop }) => (
|
|
228
|
+
<ScrollView horizontal={isDesktop} onScroll={() => console.log('scrolling')}>
|
|
229
|
+
<View>Box 1</View>
|
|
230
|
+
<View>Box 2</View>
|
|
231
|
+
</ScrollView>
|
|
232
|
+
);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Select
|
|
236
|
+
|
|
237
|
+
> @TODO
|
|
238
|
+
|
|
208
239
|
### Switch
|
|
209
240
|
|
|
210
241
|
This primitive returns a _mobile_ checkbox button based on the following properties:
|
|
@@ -239,33 +270,6 @@ const MyComponent = () => (
|
|
|
239
270
|
--mirai-ui-switch-size: var(--mirai-ui-space-L);
|
|
240
271
|
```
|
|
241
272
|
|
|
242
|
-
### ScrollView
|
|
243
|
-
|
|
244
|
-
This primitive is used to make vertically scrollable views and receives the following props:
|
|
245
|
-
|
|
246
|
-
- `behavior:string` type of scroll animation
|
|
247
|
-
- `children:node`
|
|
248
|
-
- `height:number` height value
|
|
249
|
-
- `horizontal:boolean` layout direction
|
|
250
|
-
- `scrollEventThrottle:number` scroll timeout value
|
|
251
|
-
- `scrollIndicator:boolean` if true the scroll indicator is shown
|
|
252
|
-
- `scrollTo:number` coordinate to scroll to
|
|
253
|
-
- `snap:bool` if you want use the CSS feature `scroll-snap`
|
|
254
|
-
- `tag:string` html tag of resulting element
|
|
255
|
-
- `width:number` width value
|
|
256
|
-
- `onscroll:function` executed when user scrolls over the element
|
|
257
|
-
|
|
258
|
-
```jsx
|
|
259
|
-
import { ScrollView, View } from '@mirai/ui';
|
|
260
|
-
|
|
261
|
-
const MyComponent = ({ isDesktop }) => (
|
|
262
|
-
<ScrollView horizontal={isDesktop} onScroll={() => console.log('scrolling')}>
|
|
263
|
-
<View>Box 1</View>
|
|
264
|
-
<View>Box 2</View>
|
|
265
|
-
</ScrollView>
|
|
266
|
-
);
|
|
267
|
-
```
|
|
268
|
-
|
|
269
273
|
### Text
|
|
270
274
|
|
|
271
275
|
A primitive for displaying text. It receives the following props:
|
|
@@ -482,6 +486,14 @@ const MyComponent = props => {
|
|
|
482
486
|
--mirai-ui-calendar-week-margin: 2px;
|
|
483
487
|
```
|
|
484
488
|
|
|
489
|
+
### Form
|
|
490
|
+
|
|
491
|
+
> @TODO
|
|
492
|
+
|
|
493
|
+
### InputDate
|
|
494
|
+
|
|
495
|
+
> @TODO
|
|
496
|
+
|
|
485
497
|
### InputNumber
|
|
486
498
|
|
|
487
499
|
Input number component controlling the value with 2 buttons. Receives the following props:
|
|
@@ -578,6 +590,14 @@ const MyComponent = () => {
|
|
|
578
590
|
--mirai-ui-input-option-padding-y: var(--mirai-ui-space-S);
|
|
579
591
|
```
|
|
580
592
|
|
|
593
|
+
### InputPhone
|
|
594
|
+
|
|
595
|
+
> @TODO
|
|
596
|
+
|
|
597
|
+
### InputSelect
|
|
598
|
+
|
|
599
|
+
> @TODO
|
|
600
|
+
|
|
581
601
|
### InputText
|
|
582
602
|
|
|
583
603
|
Text input component receiving the following props:
|
|
@@ -728,6 +748,10 @@ const MyComponent = (props) => {
|
|
|
728
748
|
--mirai-ui-modal-shadow: var(--mirai-ui-shadow);
|
|
729
749
|
```
|
|
730
750
|
|
|
751
|
+
### Notification
|
|
752
|
+
|
|
753
|
+
> @TODO
|
|
754
|
+
|
|
731
755
|
### Progress
|
|
732
756
|
|
|
733
757
|
A progress component receiving the following props:
|
|
@@ -9,11 +9,10 @@ var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
9
9
|
var _react = _interopRequireWildcard(require("react"));
|
|
10
10
|
var _Form = require("./Form.constants");
|
|
11
11
|
var _helpers = require("./helpers");
|
|
12
|
-
var _excluded = ["children", "debounce", "schema", "showErrors", "validateOnMount", "onBlur", "onChange", "onError", "onFocus", "onSubmit"];
|
|
12
|
+
var _excluded = ["children", "debounce", "schema", "showErrors", "tag", "validateOnMount", "onBlur", "onChange", "onError", "onFocus", "onSubmit"];
|
|
13
13
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
14
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
-
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
17
16
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
18
17
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
18
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
@@ -34,6 +33,8 @@ var Form = function Form(_ref) {
|
|
|
34
33
|
_ref$schema = _ref.schema,
|
|
35
34
|
schema = _ref$schema === void 0 ? {} : _ref$schema,
|
|
36
35
|
showErrors = _ref.showErrors,
|
|
36
|
+
_ref$tag = _ref.tag,
|
|
37
|
+
tag = _ref$tag === void 0 ? 'form' : _ref$tag,
|
|
37
38
|
_ref$validateOnMount = _ref.validateOnMount,
|
|
38
39
|
validateOnMount = _ref$validateOnMount === void 0 ? false : _ref$validateOnMount,
|
|
39
40
|
onBlur = _ref.onBlur,
|
|
@@ -145,7 +146,7 @@ var Form = function Form(_ref) {
|
|
|
145
146
|
event.preventDefault();
|
|
146
147
|
};
|
|
147
148
|
return (0, _react.useMemo)(function () {
|
|
148
|
-
return /*#__PURE__*/_react.default.createElement(
|
|
149
|
+
return /*#__PURE__*/_react.default.createElement(tag, _objectSpread(_objectSpread({}, others), {}, {
|
|
149
150
|
onSubmit: handleSubmit
|
|
150
151
|
}), _react.default.Children.map(children, function (child, index) {
|
|
151
152
|
var _objectSpread4;
|
|
@@ -183,6 +184,7 @@ Form.propTypes = {
|
|
|
183
184
|
debounce: _propTypes.default.number,
|
|
184
185
|
schema: _propTypes.default.shape({}),
|
|
185
186
|
showErrors: _propTypes.default.bool,
|
|
187
|
+
tag: _propTypes.default.string,
|
|
186
188
|
validateOnMount: _propTypes.default.bool,
|
|
187
189
|
onBlur: _propTypes.default.func,
|
|
188
190
|
onChange: _propTypes.default.func,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Form.js","names":["Form","children","debounce","DEFAULT_TIMEOUT_ONCHANGE","schema","showErrors","validateOnMount","onBlur","onChange","onError","onFocus","onSubmit","others","useState","error","setError","initialValue","setInitialValue","touched","setTouched","values","setValues","useEffect","nextValues","getChildrenValues","nextChildrenKeys","Object","keys","sort","length","JSON","stringify","nextError","getChildrenErrors","collision","some","key","groupState","value","timer","setTimeout","clearTimeout","handleChange","field","fieldValue","handleError","DEFAULT_TIMEOUT_ONERROR","hasError","changed","errors","handleFocus","event","handleSubmit","preventDefault","useMemo","React","Children","map","child","index","props","type","displayName","getField","cloneElement","FIELDS_WITHOUT_ERRORS","includes","undefined","FIELDS_BOOLEAN","onPress","propTypes","PropTypes","node","number","shape","bool","func"],"sources":["../../../src/components/Form/Form.jsx"],"sourcesContent":["import PropTypes from 'prop-types';\nimport React, { useEffect, useMemo, useState } from 'react';\n\nimport {\n DEFAULT_TIMEOUT_ONCHANGE,\n DEFAULT_TIMEOUT_ONERROR,\n FIELDS_BOOLEAN,\n FIELDS_WITHOUT_ERRORS,\n} from './Form.constants';\nimport { getChildrenErrors, getChildrenValues, getField, groupState } from './helpers';\n\nconst Form = ({\n children,\n debounce = DEFAULT_TIMEOUT_ONCHANGE,\n schema = {},\n showErrors,\n validateOnMount = false,\n onBlur,\n onChange,\n onError,\n onFocus,\n onSubmit,\n ...others\n}) => {\n const [error, setError] = useState({});\n const [initialValue, setInitialValue] = useState({});\n const [touched, setTouched] = useState({});\n const [values, setValues] = useState({});\n\n useEffect(() => {\n const nextValues = getChildrenValues(children);\n const nextChildrenKeys = Object.keys(nextValues).sort();\n\n if (!Object.keys(nextValues).length) return;\n\n if (JSON.stringify(nextChildrenKeys) !== JSON.stringify(Object.keys(initialValue).sort())) {\n setInitialValue(nextValues);\n setValues(nextValues);\n\n if (validateOnMount) {\n const nextError = getChildrenErrors({ children, schema, values: nextValues });\n setError(nextError);\n onError && onError(nextError);\n } else {\n setError({});\n }\n setTouched({});\n } else {\n const collision = nextChildrenKeys.some((key) => JSON.stringify(values[key]) !== JSON.stringify(nextValues[key]));\n if (collision) setValues(nextValues);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [children]);\n\n useEffect(() => {\n if (!onChange || values === initialValue || !Object.keys(values).length) return;\n\n if (!debounce) return onChange(values, groupState({ initialValue, value: values, touched }));\n\n const timer = setTimeout(() => onChange(values, groupState({ initialValue, value: values, touched })), debounce);\n return () => clearTimeout(timer);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [values]);\n\n const handleChange = (field, fieldValue) => {\n const nextValues = { ...values, [field]: fieldValue };\n\n setValues(nextValues);\n setTimeout(() => handleError(nextValues), DEFAULT_TIMEOUT_ONERROR);\n };\n\n const handleError = (values) => {\n const nextError = getChildrenErrors({ children, schema, values });\n const hasError = Object.keys(nextError).length > 0;\n const changed = JSON.stringify(error) !== JSON.stringify(nextError);\n\n if (changed) {\n setError(nextError);\n onError && onError(nextError, hasError);\n }\n\n return { changed, errors: nextError, hasError };\n };\n\n const handleFocus = (field, event) => {\n setTouched({ ...touched, [field]: true });\n if (onFocus) onFocus(field, event);\n };\n\n const handleSubmit = (event) => {\n const { errors, hasError } = handleError(values);\n\n if (hasError && onError) onError(errors, hasError);\n else if (onSubmit) onSubmit(values, groupState({ initialValue, value: values, touched }), event);\n event.preventDefault();\n };\n\n return useMemo(\n () => (\n <form {...others} onSubmit={handleSubmit}>\n {React.Children.map(children, (child, index) => {\n if (!child || child === null) return;\n\n const { props = {}, type: { displayName } = {} } = child || {};\n const { type } = props;\n const field = getField(props);\n\n return React.cloneElement(child, {\n key: index,\n ...(field\n ? {\n ...props,\n ...schema[field],\n error: !FIELDS_WITHOUT_ERRORS.includes(displayName)\n ? props.error || (showErrors && error[field])\n : undefined,\n [FIELDS_BOOLEAN.includes(displayName) ? 'checked' : 'value']: values[field],\n onBlur: onBlur ? (event) => onBlur(field, event) : undefined,\n onChange: (value) => handleChange(field, value),\n onFocus: (event) => handleFocus(field, event),\n }\n : type === 'submit'\n ? { ...props, onPress: handleSubmit }\n : undefined),\n });\n })}\n </form>\n ),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [children, error, others, schema],\n );\n};\n\nForm.displayName = 'Component:Form';\n\nForm.propTypes = {\n children: PropTypes.node,\n debounce: PropTypes.number,\n schema: PropTypes.shape({}),\n showErrors: PropTypes.bool,\n validateOnMount: PropTypes.bool,\n onBlur: PropTypes.func,\n onChange: PropTypes.func,\n onError: PropTypes.func,\n onFocus: PropTypes.func,\n onSubmit: PropTypes.func,\n};\n\nexport { Form };\n"],"mappings":";;;;;;;AAAA;AACA;AAEA;AAMA;AAAuF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEvF,IAAMA,IAAI,GAAG,SAAPA,IAAI,OAYJ;EAAA,IAXJC,QAAQ,QAARA,Q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ACF,QAAQ,CAACJ,WAAW,CAAC,GAAG,SAAS,GAAG,OAAO,EAAG1C,MAAM,CAACuB,KAAK,CAAC,6CACnEpC,MAAM,GAAG,UAAC4C,KAAK;QAAA,OAAK5C,MAAM,CAACoC,KAAK,EAAEQ,KAAK,CAAC;MAAA,IAAGgB,SAAS,+CAClD,kBAAC7B,KAAK;QAAA,OAAKI,YAAY,CAACC,KAAK,EAAEL,KAAK,CAAC;MAAA,+CACtC,iBAACa,KAAK;QAAA,OAAKD,WAAW,CAACP,KAAK,EAAEQ,KAAK,CAAC;MAAA,uBAE/CU,IAAI,KAAK,QAAQ,mCACZD,KAAK;QAAES,OAAO,EAAEjB;MAAY,KACjCe,SAAS,EACb;IACJ,CAAC,CAAC,CACG;EAAA,CACR;EACD;EACA,CAAClE,QAAQ,EAAEa,KAAK,EAAEF,MAAM,EAAER,MAAM,CAAC,CAClC;AACH,CAAC;AAAC;AAEFJ,IAAI,CAAC8D,WAAW,GAAG,gBAAgB;AAEnC9D,IAAI,CAACsE,SAAS,GAAG;EACfrE,QAAQ,EAAEsE,kBAAS,CAACC,IAAI;EACxBtE,QAAQ,EAAEqE,kBAAS,CAACE,MAAM;EAC1BrE,MAAM,EAAEmE,kBAAS,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC;EAC3BrE,UAAU,EAAEkE,kBAAS,CAACI,IAAI;EAC1BrE,eAAe,EAAEiE,kBAAS,CAACI,IAAI;EAC/BpE,MAAM,EAAEgE,kBAAS,CAACK,IAAI;EACtBpE,QAAQ,EAAE+D,kBAAS,CAACK,IAAI;EACxBnE,OAAO,EAAE8D,kBAAS,CAACK,IAAI;EACvBlE,OAAO,EAAE6D,kBAAS,CAACK,IAAI;EACvBjE,QAAQ,EAAE4D,kBAAS,CAACK;AACtB,CAAC"}
|
|
1
|
+
{"version":3,"file":"Form.js","names":["Form","children","debounce","DEFAULT_TIMEOUT_ONCHANGE","schema","showErrors","tag","validateOnMount","onBlur","onChange","onError","onFocus","onSubmit","others","useState","error","setError","initialValue","setInitialValue","touched","setTouched","values","setValues","useEffect","nextValues","getChildrenValues","nextChildrenKeys","Object","keys","sort","length","JSON","stringify","nextError","getChildrenErrors","collision","some","key","groupState","value","timer","setTimeout","clearTimeout","handleChange","field","fieldValue","handleError","DEFAULT_TIMEOUT_ONERROR","hasError","changed","errors","handleFocus","event","handleSubmit","preventDefault","useMemo","React","createElement","Children","map","child","index","props","type","displayName","getField","cloneElement","FIELDS_WITHOUT_ERRORS","includes","undefined","FIELDS_BOOLEAN","onPress","propTypes","PropTypes","node","number","shape","bool","string","func"],"sources":["../../../src/components/Form/Form.jsx"],"sourcesContent":["import PropTypes from 'prop-types';\nimport React, { useEffect, useMemo, useState } from 'react';\n\nimport {\n DEFAULT_TIMEOUT_ONCHANGE,\n DEFAULT_TIMEOUT_ONERROR,\n FIELDS_BOOLEAN,\n FIELDS_WITHOUT_ERRORS,\n} from './Form.constants';\nimport { getChildrenErrors, getChildrenValues, getField, groupState } from './helpers';\n\nconst Form = ({\n children,\n debounce = DEFAULT_TIMEOUT_ONCHANGE,\n schema = {},\n showErrors,\n tag = 'form',\n validateOnMount = false,\n onBlur,\n onChange,\n onError,\n onFocus,\n onSubmit,\n ...others\n}) => {\n const [error, setError] = useState({});\n const [initialValue, setInitialValue] = useState({});\n const [touched, setTouched] = useState({});\n const [values, setValues] = useState({});\n\n useEffect(() => {\n const nextValues = getChildrenValues(children);\n const nextChildrenKeys = Object.keys(nextValues).sort();\n\n if (!Object.keys(nextValues).length) return;\n\n if (JSON.stringify(nextChildrenKeys) !== JSON.stringify(Object.keys(initialValue).sort())) {\n setInitialValue(nextValues);\n setValues(nextValues);\n\n if (validateOnMount) {\n const nextError = getChildrenErrors({ children, schema, values: nextValues });\n setError(nextError);\n onError && onError(nextError);\n } else {\n setError({});\n }\n setTouched({});\n } else {\n const collision = nextChildrenKeys.some((key) => JSON.stringify(values[key]) !== JSON.stringify(nextValues[key]));\n if (collision) setValues(nextValues);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [children]);\n\n useEffect(() => {\n if (!onChange || values === initialValue || !Object.keys(values).length) return;\n\n if (!debounce) return onChange(values, groupState({ initialValue, value: values, touched }));\n\n const timer = setTimeout(() => onChange(values, groupState({ initialValue, value: values, touched })), debounce);\n return () => clearTimeout(timer);\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [values]);\n\n const handleChange = (field, fieldValue) => {\n const nextValues = { ...values, [field]: fieldValue };\n\n setValues(nextValues);\n setTimeout(() => handleError(nextValues), DEFAULT_TIMEOUT_ONERROR);\n };\n\n const handleError = (values) => {\n const nextError = getChildrenErrors({ children, schema, values });\n const hasError = Object.keys(nextError).length > 0;\n const changed = JSON.stringify(error) !== JSON.stringify(nextError);\n\n if (changed) {\n setError(nextError);\n onError && onError(nextError, hasError);\n }\n\n return { changed, errors: nextError, hasError };\n };\n\n const handleFocus = (field, event) => {\n setTouched({ ...touched, [field]: true });\n if (onFocus) onFocus(field, event);\n };\n\n const handleSubmit = (event) => {\n const { errors, hasError } = handleError(values);\n\n if (hasError && onError) onError(errors, hasError);\n else if (onSubmit) onSubmit(values, groupState({ initialValue, value: values, touched }), event);\n event.preventDefault();\n };\n\n return useMemo(\n () =>\n React.createElement(\n tag,\n { ...others, onSubmit: handleSubmit },\n React.Children.map(children, (child, index) => {\n if (!child || child === null) return;\n\n const { props = {}, type: { displayName } = {} } = child || {};\n const { type } = props;\n const field = getField(props);\n\n return React.cloneElement(child, {\n key: index,\n ...(field\n ? {\n ...props,\n ...schema[field],\n error: !FIELDS_WITHOUT_ERRORS.includes(displayName)\n ? props.error || (showErrors && error[field])\n : undefined,\n [FIELDS_BOOLEAN.includes(displayName) ? 'checked' : 'value']: values[field],\n onBlur: onBlur ? (event) => onBlur(field, event) : undefined,\n onChange: (value) => handleChange(field, value),\n onFocus: (event) => handleFocus(field, event),\n }\n : type === 'submit'\n ? { ...props, onPress: handleSubmit }\n : undefined),\n });\n }),\n ),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [children, error, others, schema],\n );\n};\n\nForm.displayName = 'Component:Form';\n\nForm.propTypes = {\n children: PropTypes.node,\n debounce: PropTypes.number,\n schema: PropTypes.shape({}),\n showErrors: PropTypes.bool,\n tag: PropTypes.string,\n validateOnMount: PropTypes.bool,\n onBlur: PropTypes.func,\n onChange: PropTypes.func,\n onError: PropTypes.func,\n onFocus: PropTypes.func,\n onSubmit: PropTypes.func,\n};\n\nexport { Form };\n"],"mappings":";;;;;;;AAAA;AACA;AAEA;AAMA;AAAuF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEvF,IAAMA,IAAI,GAAG,SAAPA,IAAI,OAaJ;EAAA,IAZJC,QAAQ,QAARA,QAAQ;IAAA,qBACRC,QAAQ;IAARA,QAAQ,8BAAGC,8BAAwB;IAAA,mBACnCC,MAAM;IAANA,MAAM,4BAAG,CAAC,CAAC;IACXC,UAAU,QAAVA,UAAU;IAAA,gBACVC,GAAG;IAAHA,GAAG,yBAAG,MAAM;IAAA,4BACZC,eAAe;IAAfA,eAAe,qCAAG,KAAK;IACvBC,MAAM,QAANA,MAAM;IACNC,QAAQ,QAARA,QAAQ;IACRC,OAAO,QAAPA,OAAO;IACPC,OAAO,QAAPA,OAAO;IACPC,QAAQ,QAARA,QAAQ;IACLC,MAAM;EAET,gBAA0B,IAAAC,eAAQ,EAAC,CAAC,CAAC,CAAC;IAAA;IAA/BC,KAAK;IAAEC,QAAQ;EACtB,iBAAwC,IAAAF,eAAQ,EAAC,CAAC,CAAC,CAAC;IAAA;IAA7CG,YAAY;IAAEC,eAAe;EACpC,iBAA8B,IAAAJ,eAAQ,EAAC,CAAC,CAAC,CAAC;IAAA;IAAnCK,OAAO;IAAEC,UAAU;EAC1B,iBAA4B,IAAAN,eAAQ,EAAC,CAAC,CAAC,CAAC;IAAA;IAAjCO,MAAM;IAAEC,SAAS;EAExB,IAAAC,gB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,EAAEwE,kBAAS,CAACC,IAAI;EACxBxE,QAAQ,EAAEuE,kBAAS,CAACE,MAAM;EAC1BvE,MAAM,EAAEqE,kBAAS,CAACG,KAAK,CAAC,CAAC,CAAC,CAAC;EAC3BvE,UAAU,EAAEoE,kBAAS,CAACI,IAAI;EAC1BvE,GAAG,EAAEmE,kBAAS,CAACK,MAAM;EACrBvE,eAAe,EAAEkE,kBAAS,CAACI,IAAI;EAC/BrE,MAAM,EAAEiE,kBAAS,CAACM,IAAI;EACtBtE,QAAQ,EAAEgE,kBAAS,CAACM,IAAI;EACxBrE,OAAO,EAAE+D,kBAAS,CAACM,IAAI;EACvBpE,OAAO,EAAE8D,kBAAS,CAACM,IAAI;EACvBnE,QAAQ,EAAE6D,kBAAS,CAACM;AACtB,CAAC"}
|
|
@@ -38,6 +38,12 @@ exports[`component:<Form> prop:children 1`] = `
|
|
|
38
38
|
</DocumentFragment>
|
|
39
39
|
`;
|
|
40
40
|
|
|
41
|
+
exports[`component:<Form> prop:tag 1`] = `
|
|
42
|
+
<DocumentFragment>
|
|
43
|
+
<fieldset />
|
|
44
|
+
</DocumentFragment>
|
|
45
|
+
`;
|
|
46
|
+
|
|
41
47
|
exports[`component:<Form> renders 1`] = `
|
|
42
48
|
<DocumentFragment>
|
|
43
49
|
<form />
|