Skip to content

Commit

Permalink
fix(elements): ensure updated provided values are sent to the machine (
Browse files Browse the repository at this point in the history
…#3702)

Co-authored-by: Tom Milewski <[email protected]>
  • Loading branch information
alexcarpenter and tmilewski authored Jul 15, 2024
1 parent 6e4d0f8 commit 2e6afd1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-chairs-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/elements": patch
---

Ensure updated provided values to controlled inputs are sent to the machine
14 changes: 7 additions & 7 deletions packages/elements/src/react/common/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const useField = ({ name }: Partial<Pick<FieldDetails, 'name'>>) => {

const useInput = ({
name: inputName,
value: initialValue,
value: providedValue,
type: inputType,
onChange: onChangeProp,
onBlur: onBlurProp,
Expand Down Expand Up @@ -265,7 +265,7 @@ const useInput = ({
return;
}

ref.send({ type: 'FIELD.ADD', field: { name, value: initialValue } });
ref.send({ type: 'FIELD.ADD', field: { name, value: providedValue } });

return () => ref.send({ type: 'FIELD.REMOVE', field: { name } });
}, [ref]); // eslint-disable-line react-hooks/exhaustive-deps
Expand All @@ -274,15 +274,15 @@ const useInput = ({
const onChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
onChangeProp?.(event);
if (!name || initialValue) {
if (!name) {
return;
}
ref.send({ type: 'FIELD.UPDATE', field: { name, value: event.target.value } });
if (shouldValidatePassword) {
validatePassword(event.target.value);
}
},
[ref, name, onChangeProp, initialValue, shouldValidatePassword, validatePassword],
[ref, name, onChangeProp, shouldValidatePassword, validatePassword],
);

const onBlur = React.useCallback(
Expand All @@ -306,11 +306,11 @@ const useInput = ({
);

React.useEffect(() => {
if (!initialValue || !name) {
if (!name) {
return;
}
ref.send({ type: 'FIELD.UPDATE', field: { name, value: initialValue } });
}, [name, ref, initialValue]);
ref.send({ type: 'FIELD.UPDATE', field: { name, value: providedValue } });
}, [name, ref, providedValue]);

// TODO: Implement clerk-js utils
const shouldBeHidden = false;
Expand Down

0 comments on commit 2e6afd1

Please sign in to comment.