Register form hook finally completed
This commit is contained in:
parent
f7d733b407
commit
2db0e8f68a
@ -186,64 +186,65 @@ export function useUserProfileForm(params: ParamsOfUseUserProfileForm): ReturnTy
|
|||||||
|
|
||||||
const [state, dispatchFormAction] = useReducer(
|
const [state, dispatchFormAction] = useReducer(
|
||||||
function reducer(state: internal.State, params: FormAction): internal.State {
|
function reducer(state: internal.State, params: FormAction): internal.State {
|
||||||
if (params.action === "add value to multi-valued attribute") {
|
const formFieldState = state.formFieldStates.find(({ attribute }) => attribute.name === params.name);
|
||||||
const formFieldStates = state.formFieldStates.filter(({ name }) => name === params.name);
|
|
||||||
|
|
||||||
state.formFieldStates.splice(state.formFieldStates.indexOf(formFieldStates[formFieldStates.length - 1]) + 1, 0, {
|
|
||||||
"index": formFieldStates.length,
|
|
||||||
"name": params.name,
|
|
||||||
"value": "",
|
|
||||||
"errors": getErrors({
|
|
||||||
"name": params.name,
|
|
||||||
"index": formFieldStates.length,
|
|
||||||
"fieldValues": state.formFieldStates
|
|
||||||
}),
|
|
||||||
"hasLostFocusAtLeastOnce": false,
|
|
||||||
"attribute": formFieldStates[0].attribute
|
|
||||||
});
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
const formFieldState = state.formFieldStates.find(({ name, index }) => name === params.name && index === params.index);
|
|
||||||
|
|
||||||
assert(formFieldState !== undefined);
|
assert(formFieldState !== undefined);
|
||||||
|
|
||||||
switch (params.action) {
|
(() => {
|
||||||
case "focus lost":
|
switch (params.action) {
|
||||||
formFieldState.hasLostFocusAtLeastOnce = true;
|
case "update":
|
||||||
return state;
|
case "update multi-valued":
|
||||||
case "update value":
|
(() => {
|
||||||
update_password_confirm: {
|
switch (params.action) {
|
||||||
if (params.name !== "password") {
|
case "update":
|
||||||
break update_password_confirm;
|
assert("value" in formFieldState);
|
||||||
}
|
formFieldState.value = params.value;
|
||||||
|
return;
|
||||||
|
case "update multi-valued":
|
||||||
|
assert("values" in formFieldState);
|
||||||
|
formFieldState.values = params.values;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert<Equals<typeof params, never>>(false);
|
||||||
|
})();
|
||||||
|
|
||||||
if (doMakeUserConfirmPassword) {
|
formFieldState.errors = getErrors({
|
||||||
break update_password_confirm;
|
"attributeName": params.name,
|
||||||
}
|
"fieldValues": state.formFieldStates
|
||||||
|
|
||||||
state = reducer(state, {
|
|
||||||
"action": "update value",
|
|
||||||
"name": "password-confirm",
|
|
||||||
"index": 0,
|
|
||||||
"newValue": params.newValue
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
formFieldState.value = params.newValue;
|
update_password_confirm: {
|
||||||
formFieldState.errors = getErrors({
|
if (doMakeUserConfirmPassword) {
|
||||||
"name": params.name,
|
break update_password_confirm;
|
||||||
"index": params.index,
|
}
|
||||||
"fieldValues": state.formFieldStates
|
|
||||||
});
|
|
||||||
return state;
|
|
||||||
case "remove value from multi-valued attribute":
|
|
||||||
state.formFieldStates.splice(state.formFieldStates.indexOf(formFieldState), 1);
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert<Equals<typeof params, never>>(false);
|
if (params.name !== "password") {
|
||||||
|
break update_password_confirm;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(params.action === "update");
|
||||||
|
|
||||||
|
state = reducer(state, {
|
||||||
|
"action": "update",
|
||||||
|
"name": "password-confirm",
|
||||||
|
"value": params.value
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
case "focus lost":
|
||||||
|
assert(typeof formFieldState.hasLostFocusAtLeastOnce === "boolean");
|
||||||
|
formFieldState.hasLostFocusAtLeastOnce = true;
|
||||||
|
return;
|
||||||
|
case "multi-valued text input focus lost":
|
||||||
|
assert(formFieldState.hasLostFocusAtLeastOnce instanceof Array);
|
||||||
|
formFieldState.hasLostFocusAtLeastOnce[params.fieldIndex] = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert<Equals<typeof params, never>>(false);
|
||||||
|
})();
|
||||||
|
|
||||||
|
return state;
|
||||||
},
|
},
|
||||||
useMemo(function getInitialState(): internal.State {
|
useMemo(function getInitialState(): internal.State {
|
||||||
const initialFormFieldValues = (() => {
|
const initialFormFieldValues = (() => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user