@altiore/form
Advanced tools
Comparing version 1.1.4 to 1.1.5
@@ -7,3 +7,3 @@ import { FieldArrayState, FieldMeta, FieldType, FormContextState } from '../types'; | ||
}; | ||
export declare const useRegisterField: (fieldArrayState: FieldArrayState, formState: FormContextState, providedName: string, fieldType?: FieldType, isArray?: boolean) => ResType; | ||
export declare const useRegisterField: (fieldArrayState: FieldArrayState, formState: FormContextState, providedName: string, fieldType?: FieldType, isArray?: boolean, defaultValue?: unknown) => ResType; | ||
export {}; |
import { useEffect, useMemo } from 'react'; | ||
import { FieldType, } from '../types'; | ||
export var useRegisterField = function (fieldArrayState, formState, providedName, fieldType, isArray) { | ||
export var useRegisterField = function (fieldArrayState, formState, providedName, fieldType, isArray, defaultValue) { | ||
var fieldName = useMemo(function () { | ||
@@ -14,3 +14,3 @@ return (fieldArrayState === null || fieldArrayState === void 0 ? void 0 : fieldArrayState.name) && | ||
if (isInsideForm) { | ||
return registerField(fieldName, fieldType !== null && fieldType !== void 0 ? fieldType : (isArray ? FieldType.ARRAY : undefined)); | ||
return registerField(fieldName, fieldType !== null && fieldType !== void 0 ? fieldType : (isArray ? FieldType.ARRAY : undefined), defaultValue); | ||
} | ||
@@ -23,3 +23,10 @@ else { | ||
} | ||
}, [fieldName, fieldType, isArray, isInsideForm, registerField]); | ||
}, [ | ||
defaultValue, | ||
fieldName, | ||
fieldType, | ||
isArray, | ||
isInsideForm, | ||
registerField, | ||
]); | ||
var fields = useMemo(function () { return formState === null || formState === void 0 ? void 0 : formState.fields; }, [formState === null || formState === void 0 ? void 0 : formState.fields]); | ||
@@ -26,0 +33,0 @@ var field = useMemo(function () { return fields === null || fields === void 0 ? void 0 : fields[fieldName]; }, [fields, fieldName]); |
@@ -34,3 +34,3 @@ import { MouseEventHandler, MutableRefObject } from 'react'; | ||
}; | ||
export declare type RegisterField = (fieldName: string, fieldType?: FieldType) => void; | ||
export declare type RegisterField = (fieldName: string, fieldType?: FieldType, defaultValue?: any) => void; | ||
export declare type SetErrors = (name: string, errors: string[] | undefined) => void; | ||
@@ -44,2 +44,3 @@ export declare type FieldMeta<ValueType = any> = { | ||
type?: FieldType; | ||
isUntouched?: boolean; | ||
isInvalid: boolean; | ||
@@ -46,0 +47,0 @@ error?: string; |
/// <reference types="react" /> | ||
import { NamedFieldProps } from '../../@common/types'; | ||
import { ValidatedFieldProps } from './validated-field'; | ||
export declare const NamedField: <T, Input extends HTMLElement = HTMLInputElement>({ fieldArrayState, formState, providedName, type, ...rest }: NamedFieldProps<ValidatedFieldProps<T, Input>, "name" | "field">) => JSX.Element; | ||
export declare const NamedField: <T extends { | ||
defaultValue?: any; | ||
}, Input extends HTMLElement = HTMLInputElement>({ fieldArrayState, formState, providedName, type, componentProps, ...rest }: NamedFieldProps<ValidatedFieldProps<T, Input>, "name" | "field">) => JSX.Element; |
@@ -27,8 +27,8 @@ var __assign = (this && this.__assign) || function () { | ||
export var NamedField = function (_a) { | ||
var fieldArrayState = _a.fieldArrayState, formState = _a.formState, providedName = _a.providedName, type = _a.type, rest = __rest(_a, ["fieldArrayState", "formState", "providedName", "type"]); | ||
var _b = useRegisterField(fieldArrayState, formState, providedName, type), field = _b.field, isInsideForm = _b.isInsideForm, name = _b.name; | ||
var fieldArrayState = _a.fieldArrayState, formState = _a.formState, providedName = _a.providedName, type = _a.type, componentProps = _a.componentProps, rest = __rest(_a, ["fieldArrayState", "formState", "providedName", "type", "componentProps"]); | ||
var _b = useRegisterField(fieldArrayState, formState, providedName, type, false, componentProps.defaultValue), field = _b.field, isInsideForm = _b.isInsideForm, name = _b.name; | ||
if (isInsideForm && !field) { | ||
return null; | ||
} | ||
return (React.createElement(ValidatedField, __assign({}, rest, { formRef: formState === null || formState === void 0 ? void 0 : formState.formRef, field: field, name: name, type: type }))); | ||
return (React.createElement(ValidatedField, __assign({}, rest, { componentProps: componentProps, formRef: formState === null || formState === void 0 ? void 0 : formState.formRef, field: field, name: name, type: type }))); | ||
}; |
@@ -5,3 +5,4 @@ import { ButtonHTMLAttributes } from 'react'; | ||
isSubmitting: boolean; | ||
isUntouched: boolean; | ||
} | ||
export declare function createSubmitButton<T extends any = HTMLButtonElement>(component: (props: InternalSubmitButtonProps<T>) => JSX.Element): (props: Omit<InternalSubmitButtonProps, 'isInvalid' | 'isSubmitting' | 'type'>) => JSX.Element; |
@@ -21,3 +21,6 @@ var __assign = (this && this.__assign) || function () { | ||
var isSubmitting = useMemo(function () { return formState === null || formState === void 0 ? void 0 : formState.isSubmitting; }, [formState]); | ||
return React.createElement(component, __assign(__assign({}, componentProps), { isInvalid: isInvalid, isSubmitting: isSubmitting, type: 'submit' })); | ||
var isUntouched = useMemo(function () { | ||
return fields ? Object.values(fields).some(function (el) { return el.isUntouched; }) : false; | ||
}, [fields]); | ||
return React.createElement(component, __assign(__assign({}, componentProps), { isInvalid: isInvalid, isSubmitting: isSubmitting, isUntouched: isUntouched, type: 'submit' })); | ||
}; | ||
@@ -24,0 +27,0 @@ export function createSubmitButton(component) { |
@@ -70,3 +70,3 @@ var __assign = (this && this.__assign) || function () { | ||
} | ||
return __assign(__assign({}, s), (_a = {}, _a[fieldName] = __assign(__assign({}, s[fieldName]), { error: errors === null || errors === void 0 ? void 0 : errors[0], errors: errors, isInvalid: Boolean(errors === null || errors === void 0 ? void 0 : errors.length) }), _a)); | ||
return __assign(__assign({}, s), (_a = {}, _a[fieldName] = __assign(__assign({}, s[fieldName]), { error: errors === null || errors === void 0 ? void 0 : errors[0], errors: errors, isInvalid: Boolean(errors === null || errors === void 0 ? void 0 : errors.length), isUntouched: false }), _a)); | ||
}); | ||
@@ -83,6 +83,6 @@ }, [setFields]); | ||
}, [setFields]); | ||
var registerField = useCallback(function (fieldName, fieldType) { | ||
var registerField = useCallback(function (fieldName, fieldType, fieldDefaultValue) { | ||
setFields(function (s) { | ||
var _a; | ||
var _b, _c; | ||
var _b, _c, _d; | ||
var fieldNameArr = fieldName.split('.'); | ||
@@ -95,3 +95,3 @@ var dynamicDefault = undefined; | ||
} | ||
var defaultValue = dynamicDefault !== null && dynamicDefault !== void 0 ? dynamicDefault : get(defaultValues, fieldNameArr); | ||
var defaultValue = (_d = dynamicDefault !== null && dynamicDefault !== void 0 ? dynamicDefault : get(defaultValues, fieldNameArr)) !== null && _d !== void 0 ? _d : fieldDefaultValue; | ||
return __assign(__assign({}, s), (_a = {}, _a[fieldName] = { | ||
@@ -102,2 +102,3 @@ defaultValue: defaultValue, | ||
isInvalid: false, | ||
isUntouched: defaultValue === undefined || defaultValue === null, | ||
items: fieldType === FieldType.ARRAY ? [] : undefined, | ||
@@ -104,0 +105,0 @@ name: fieldName, |
{ | ||
"name": "@altiore/form", | ||
"version": "1.1.4", | ||
"version": "1.1.5", | ||
"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
63280
1127