@altiore/form
Advanced tools
Comparing version 0.3.18 to 0.3.19
import { MutableRefObject } from 'react'; | ||
import { FieldMeta, ValidateFuncType } from '../types'; | ||
export declare const useValidateInput: (inputRef: MutableRefObject<HTMLInputElement>, validators: Array<ValidateFuncType>, field?: FieldMeta) => string[]; | ||
declare type ValidateInputRes = { | ||
errors: string[]; | ||
setErrors: (errors: string[]) => void; | ||
}; | ||
export declare const useValidateInput: (inputRef: MutableRefObject<HTMLInputElement>, validators: Array<ValidateFuncType>, field?: FieldMeta) => ValidateInputRes; | ||
export {}; |
@@ -5,4 +5,4 @@ import { useCallback, useEffect, useState } from 'react'; | ||
export var useValidateInput = function (inputRef, validators, field) { | ||
var _a; | ||
var _b = useState([]), errors = _b[0], setErrors = _b[1]; | ||
var _a, _b; | ||
var _c = useState([]), errors = _c[0], setErrors = _c[1]; | ||
var handleDebounceFn = useCallback(function (e) { | ||
@@ -49,3 +49,6 @@ e.preventDefault(); | ||
}, [inputRef, validators]); | ||
return (_a = field === null || field === void 0 ? void 0 : field.errors) !== null && _a !== void 0 ? _a : errors; | ||
return { | ||
errors: (_a = field === null || field === void 0 ? void 0 : field.errors) !== null && _a !== void 0 ? _a : errors, | ||
setErrors: (_b = field === null || field === void 0 ? void 0 : field.setErrors) !== null && _b !== void 0 ? _b : setErrors, | ||
}; | ||
}; |
@@ -7,9 +7,10 @@ import React, { useCallback } from 'react'; | ||
var list = _a.list; | ||
var cb = useCallback(function (_a) { | ||
var key = _a.key, remove = _a.remove; | ||
return (React.createElement("div", { key: key }, | ||
React.createElement(Field, { label: 'Tag', name: "tag" }), | ||
React.createElement("button", { onClick: remove }, "remove tag"))); | ||
}, []); | ||
return (React.createElement("div", null, | ||
list.map(function (_a) { | ||
var key = _a.key, remove = _a.remove; | ||
return (React.createElement("div", { key: key }, | ||
React.createElement(Field, { label: 'Tag', name: "tag" }), | ||
React.createElement("button", { onClick: remove }, "remove tag"))); | ||
}), | ||
list.map(cb), | ||
React.createElement("button", { onClick: list.add, type: "button" }, "\u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0422\u044D\u0433"))); | ||
@@ -16,0 +17,0 @@ }); |
@@ -12,3 +12,3 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
import React, { useMemo, useRef } from 'react'; | ||
import React, { useCallback, useMemo, useRef } from 'react'; | ||
import { useValidateList } from '../../@common/hooks/use-validate-list'; | ||
@@ -20,25 +20,22 @@ import { add, map, remove, useList } from './validated-field-array.utils'; | ||
var stateList = useList(name); | ||
var addHandler = useCallback(function (fieldName, field, index) { | ||
return setItems(fieldName, function (items) { | ||
return add(items, fieldName, field, index); | ||
}); | ||
}, [setItems]); | ||
var removeHandler = useCallback(function (fieldName, index) { | ||
return setItems(fieldName, function (items) { return remove(items, fieldName, index); }); | ||
}, [setItems]); | ||
var list = useMemo(function () { | ||
if (typeof setItems === 'function') { | ||
var fieldName_1 = fieldMeta.name; | ||
var addHandler = function (field, index) { | ||
return setItems(fieldName_1, function (items) { | ||
return add(items, fieldName_1, field, index); | ||
}); | ||
}; | ||
var removeHandler = function (index) { | ||
return setItems(fieldName_1, function (items) { | ||
return remove(items, fieldName_1, index); | ||
}); | ||
}; | ||
var fieldName = fieldMeta.name; | ||
var items = fieldMeta.items || []; | ||
var mapHandler = map.bind({}, addHandler, removeHandler, items, fieldName_1); | ||
return { | ||
add: addHandler, | ||
map: mapHandler, | ||
remove: removeHandler, | ||
add: addHandler.bind({}, fieldName), | ||
map: map.bind({}, addHandler, removeHandler, items, fieldName), | ||
remove: removeHandler.bind({}, fieldName), | ||
}; | ||
} | ||
return stateList; | ||
}, [fieldMeta, setItems, stateList]); | ||
}, [addHandler, fieldMeta, removeHandler, setItems, stateList]); | ||
var errors = useValidateList(listRef, validators, fieldMeta); | ||
@@ -45,0 +42,0 @@ return React.createElement(component, __assign(__assign({}, componentProps), { errors: errors, list: list, listRef: listRef, name: name })); |
@@ -13,8 +13,19 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
var Item = React.memo(function (_a) { | ||
var add = _a.add, cb = _a.cb, remove = _a.remove, name = _a.name, index = _a.index; | ||
var add = _a.add, cb = _a.cb, fieldName = _a.fieldName, remove = _a.remove, name = _a.name, index = _a.index; | ||
var handleRemove = useCallback(function () { | ||
remove(fieldName, index); | ||
}, [fieldName, index, remove]); | ||
var append = useCallback(function (object) { | ||
// TODO: уточнить реализацию | ||
add(fieldName, object, index); | ||
}, [add, fieldName, index]); | ||
var prepend = useCallback(function (object) { | ||
// TODO: уточнить реализацию | ||
add(fieldName, object, index); | ||
}, [add, fieldName, index]); | ||
return (React.createElement(FieldArrayContext.Provider, { key: name, value: { name: name } }, cb({ | ||
append: function () { return add({}, index); }, | ||
append: append, | ||
key: name, | ||
prepend: function () { return add({}, index - 1); }, | ||
remove: function () { return remove(index); }, | ||
prepend: prepend, | ||
remove: handleRemove, | ||
}, index))); | ||
@@ -25,3 +36,3 @@ }); | ||
var name = fieldName + "." + index; | ||
return (React.createElement(Item, { key: name, add: add, cb: callback, remove: remove, name: name, index: index })); | ||
return (React.createElement(Item, { key: name, add: add, cb: callback, remove: remove, name: name, fieldName: fieldName, index: index })); | ||
}); | ||
@@ -28,0 +39,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { ValidateFuncType } from '../@common/types'; | ||
import { FieldMeta, ValidateFuncType } from '../@common/types'; | ||
import { InternalFieldProps } from './validated-field'; | ||
@@ -33,2 +33,2 @@ export declare type FieldProps = { | ||
*/ | ||
export declare const createField: <T extends FieldProps>(component: (props: Omit<T, "validators"> & InternalFieldProps) => JSX.Element) => (props: T) => JSX.Element; | ||
export declare const createField: <T extends FieldProps>(component: (props: Omit<T, "validators"> & InternalFieldProps & FieldMeta) => JSX.Element) => (props: T) => JSX.Element; |
import React from 'react'; | ||
import { createField } from '..'; | ||
export var Field = createField(function (_a) { | ||
var errors = _a.errors, inputRef = _a.inputRef, label = _a.label, name = _a.name; | ||
console.log('Field.render'); | ||
export var Field = createField(function (props) { | ||
var errors = props.errors, inputRef = props.inputRef, label = props.label, name = props.name; | ||
console.log('Field.render', { | ||
props: props, | ||
}); | ||
return (React.createElement("div", null, | ||
@@ -7,0 +9,0 @@ React.createElement("span", null, label), |
import { MutableRefObject } from 'react'; | ||
import { FieldMeta, ValidateFuncType } from '../../@common/types'; | ||
export declare type InternalFieldProps = { | ||
defaultValue?: any; | ||
inputRef: MutableRefObject<HTMLInputElement>; | ||
errors: string[]; | ||
}; | ||
export interface ValidatedFieldProps<T> { | ||
component: (props: Omit<T, 'validators'> & InternalFieldProps) => JSX.Element; | ||
component: (props: Omit<T, 'validators'> & InternalFieldProps & FieldMeta) => JSX.Element; | ||
componentProps: T; | ||
@@ -11,0 +9,0 @@ field: FieldMeta; |
@@ -18,5 +18,5 @@ var __assign = (this && this.__assign) || function () { | ||
var inputRef = useInput(); | ||
var errors = useValidateInput(inputRef, validators, field); | ||
return React.createElement(component, __assign(__assign(__assign({}, componentProps), (field !== null && field !== void 0 ? field : {})), { errors: errors, inputRef: inputRef, name: name })); | ||
var _b = useValidateInput(inputRef, validators, field), errors = _b.errors, setErrors = _b.setErrors; | ||
return React.createElement(component, __assign(__assign(__assign({}, componentProps), (field !== null && field !== void 0 ? field : {})), { errors: errors, inputRef: inputRef, name: name, setErrors: setErrors })); | ||
}; | ||
export var ValidatedField = React.memo(ValidatedFieldComponent); |
{ | ||
"name": "@altiore/form", | ||
"version": "0.3.18", | ||
"version": "0.3.19", | ||
"description": "Form helper for building powerful forms", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51134
1074