MCPcopy Create free account
hub / github.com/react-hook-form/react-hook-form / register

Function register

src/logic/createFormControl.ts:1266–1365  ·  view source on GitHub ↗
(name, options = {})

Source from the content-addressed store, hash-verified

1264 };
1265
1266 const register: UseFormRegister<TFieldValues> = (name, options = {}) => {
1267 let field = get(_fields, name);
1268 const disabledIsDefined =
1269 isBoolean(options.disabled) || isBoolean(_options.disabled);
1270 const shouldRevalidateRemount =
1271 !_names.registerName.has(name) && field && field._f && !field._f.mount;
1272
1273 set(_fields, name, {
1274 ...(field || {}),
1275 _f: {
1276 ...(field && field._f ? field._f : { ref: { name } }),
1277 name,
1278 mount: true,
1279 ...options,
1280 },
1281 });
1282 _names.mount.add(name);
1283
1284 if (field && !shouldRevalidateRemount) {
1285 _setDisabledField({
1286 disabled: isBoolean(options.disabled)
1287 ? options.disabled
1288 : _options.disabled,
1289 name,
1290 });
1291 } else {
1292 updateValidAndValue(name, true, options.value);
1293 }
1294
1295 return {
1296 ...(disabledIsDefined
1297 ? { disabled: options.disabled || _options.disabled }
1298 : {}),
1299 ...(_options.progressive
1300 ? {
1301 required: !!options.required,
1302 min: getRuleValue(options.min),
1303 max: getRuleValue(options.max),
1304 minLength: getRuleValue<number>(options.minLength) as number,
1305 maxLength: getRuleValue(options.maxLength) as number,
1306 pattern: getRuleValue(options.pattern) as string,
1307 }
1308 : {}),
1309 name,
1310 onChange,
1311 onBlur: onChange,
1312 ref: (ref: HTMLInputElement | null): void => {
1313 if (ref) {
1314 _names.registerName.add(name);
1315 register(name, options);
1316 _names.registerName.delete(name);
1317 field = get(_fields, name);
1318
1319 const fieldRef = isUndefined(ref.value)
1320 ? ref.querySelectorAll
1321 ? (ref.querySelectorAll('input,select,textarea')[0] as Ref) || ref
1322 : ref
1323 : ref;

Callers 15

ComponentFunction · 0.85
AppFunction · 0.85
ComponentFunction · 0.85
AppFunction · 0.85
TestComponentFunction · 0.85
ChildFunction · 0.85
AppFunction · 0.85
WatchAppFunction · 0.85
ParentFunction · 0.85
ChildSecondFunction · 0.85
ComponentFunction · 0.85
ItemFunction · 0.85

Calls 2

_setDisabledFieldFunction · 0.85
updateValidAndValueFunction · 0.85

Tested by 15

ComponentFunction · 0.68
AppFunction · 0.68
ComponentFunction · 0.68
AppFunction · 0.68
TestComponentFunction · 0.68
ChildFunction · 0.68
AppFunction · 0.68
WatchAppFunction · 0.68
ParentFunction · 0.68
ChildSecondFunction · 0.68
ComponentFunction · 0.68
ItemFunction · 0.68