Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | 1x 1x 1x 1x 41x 1x 40x 1x 40x 40x 40x 3x 3x 37x 1x 1x 75x 8x 8x 67x 71x 71x 71x 71x 75x 75x 40x 1x 1x 1x 2x 2x 40x 1x 1x 1x 2x 2x 40x 12x 24x 40x 13x 40x 63x 40x 103x 40x 3x 6x 3x 40x 8x 8x 8x 40x 40x 12x 12x 12x 1x 22x 22x 40x 2x 2x 2x 1x 1x 40x 2x 2x 2x 1x 1x 40x 99x 99x 99x 99x 40x 31x 31x 31x 31x 62x 62x 31x 40x 143x 40x 34x 24x 40x 274x 274x 1x | import * as React from 'react';
import {
Form,
FormContext,
FormApi,
FormComponentState,
FormProps,
} from '../Form';
import { ValidatorData } from '../../types';
import { BoundComponent, BoundComponentProps } from '../bind';
import { OutsideFormError } from '../../errors';
import { isCallable } from '../../utils';
export type NestedFormChildrenRenderFunc = (renderProps: {
validatorData: ValidatorData;
}) => React.ReactNode;
export interface NestedFormProps extends BoundComponentProps {
name: string;
children: React.ReactNode | NestedFormChildrenRenderFunc;
valueTransformer?: FormProps<any>['valuesTransformer'];
}
export class NestedForm
extends React.Component<NestedFormProps>
implements BoundComponent
{
//#region Private variables
// tslint:disable:variable-name
/**
* Reference to the nearest Form ancestor provided via the context API
*/
_parentFormApi: FormApi;
/**
* Reference to the wrapped Form
*/
_wrappedFormRef: React.RefObject<Form<any>> = React.createRef();
// tslint:enable:variable-name
//#endregion
public componentDidMount() {
this.logCall('componentDidMount');
const { name } = this.props;
if (!this._parentFormApi) {
console.error(
`NestedForm "${name}" form component must be a descendant of a <Form/>.`,
);
return;
}
this._parentFormApi.onComponentMount(name, this);
}
public componentWillUnmount() {
this.logCall('componentWillUnmount');
const { name } = this.props;
return (
this._parentFormApi && this._parentFormApi.onComponentUnmount(name)
);
}
renderChildren(children: NestedFormProps['children']) {
if (isCallable(children)) {
const validatorData = this.getValidatorData();
return (children as NestedFormChildrenRenderFunc)({
validatorData,
});
}
return children;
}
render() {
this.logCall('render');
const { name, children, valueTransformer } = this.props;
return (
<FormContext.Consumer>
{(api: FormApi) => {
this._parentFormApi = api;
return (
<Form
ref={this._wrappedFormRef}
onChange={this._handleChange}
onBlur={this._handleBlur}
onFocus={this._handleFocus}
onUpdate={this._handleUpdate}
valuesTransformer={valueTransformer}
initialValues={api?.initialValues[name]}
debug={api?.debug}
virtual
>
{this.renderChildren(children)}
</Form>
);
}}
</FormContext.Consumer>
);
}
clear: BoundComponent['clear'] = async () => {
this.logCall('clear');
const { name } = this.props;
Iif (!this._parentFormApi) {
throw new OutsideFormError(`handle clear for "${name}"`);
}
await this._wrappedFormRef.current.clear();
await this._parentFormApi.onComponentUpdate(name);
};
reset: BoundComponent['reset'] = async () => {
this.logCall('reset');
const { name } = this.props;
Iif (!this._parentFormApi) {
throw new OutsideFormError(`handle reset for "${name}"`);
}
await this._wrappedFormRef.current.reset();
await this._parentFormApi.onComponentUpdate(name);
};
validate: BoundComponent['validate'] = async () => {
this.logCall('validate');
await this._wrappedFormRef.current.validate();
};
isValid: BoundComponent['isValid'] = () => {
return this._wrappedFormRef.current.isValid();
};
isPristine: BoundComponent['isPristine'] = () => {
return this._wrappedFormRef.current.isPristine();
};
getValue: BoundComponent['getValue'] = () => {
return this._wrappedFormRef.current.getValues();
};
setValue: BoundComponent['setValue'] = async (value, pristine) => {
this.logCall('setValue', { value, pristine });
await this._wrappedFormRef.current.setValues(
this._transformValue(value),
pristine,
);
return undefined;
};
getValidatorData: BoundComponent['getValidatorData'] = () => {
const { name } = this.props;
Iif (!this._parentFormApi) {
throw new OutsideFormError(`get validator data for for "${name}"`);
}
return this._parentFormApi.getValidatorData(name, this.props);
};
setValidatorData: BoundComponent['setValidatorData'] = (validatorData) => {
this.logCall('setValidatorData', { validatorData });
const { name } = this.props;
if (!this._parentFormApi) {
throw new OutsideFormError(`set validator data for for "${name}"`);
}
return this._parentFormApi.setValidatorData(name, validatorData);
};
//#region Private functions
// tslint:disable:variable-name
_handleChange = async (componentName: string, value: any) => {
this.logCall('_handleChange', { componentName, value });
const { name } = this.props;
if (!this._parentFormApi) {
throw new OutsideFormError(
`handle change for "${name}:${componentName}"`,
);
}
await this._parentFormApi.setValue(name, {
...this._wrappedFormRef.current.getValues(),
[componentName]: value,
});
await this._parentFormApi.onComponentUpdate(name);
};
_handleBlur = (componentName: string, event: any) => {
this.logCall('_handleBlur', { componentName, event });
const { name } = this.props;
if (!this._parentFormApi) {
throw new OutsideFormError(
`handle blur for "${name}:${componentName}"`,
);
}
return this._parentFormApi.onComponentBlur(name, event);
};
_handleFocus = (componentName: string, event: any) => {
this.logCall('_handleFocus', { componentName, event });
const { name } = this.props;
if (!this._parentFormApi) {
throw new OutsideFormError(
`handle focus for "${name}:${componentName}"`,
);
}
return this._parentFormApi.onComponentFocus(name, event);
};
_handleUpdate = (componentName: string) => {
this.logCall('_handleUpdate', { componentName });
const { name } = this.props;
Iif (!this._parentFormApi) {
throw new OutsideFormError(
`handle update for "${name}:${componentName}"`,
);
}
return this._parentFormApi.onComponentUpdate(name);
};
_update = async (componentState: FormComponentState) => {
this.logCall('_update', { componentState });
const { value, pristine } = componentState;
const { name } = this.props;
Iif (!this._parentFormApi) {
throw new OutsideFormError(`get validator data for "${name}"`);
}
await this._wrappedFormRef.current.setValues(
this._transformValue(value),
pristine,
true,
);
// TODO: trigger the component to render if the validatorDate changes
await new Promise((resolve) => {
this.forceUpdate(() => resolve(undefined));
});
};
_isRecursive = () => {
return true;
};
/**
* If we're setting the value to undefined or null, we need to map
* the new value to each nested form component. Otherwise, providing
* this value to setValues will have no impact.
*/
_transformValue = (value: any) => {
return value === null || value === undefined
? (this._wrappedFormRef.current as any).getComponentNames().reduce(
(values: any, key: string) => ({
...values,
[key]: value,
}),
{},
)
: value;
};
// tslint:enable:variable-name
//#endregion
logCall = (
functionName: string,
args?: {
[argName: string]: any;
},
) => {
Eif (!this._parentFormApi || !this._parentFormApi.debug) {
return;
}
const logPrefix = `🟧 NestedForm['${this.props.name}'].${functionName}`;
if (args) {
console.debug(`${logPrefix}:`, args);
} else {
console.debug(logPrefix);
}
};
}
|