Skip to content

Commit fd90750

Browse files
authored
LG-3330: password input controlled value (#1942)
* update hook to remove the controlled value from state * remove expect * address comments
1 parent 56459cd commit fd90750

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

.changeset/late-ants-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/hooks': major
3+
---
4+
5+
Updates `useControlledValue` hook to remove the controlled value from internal state. Instead the controlled value is consumed directly.

packages/hooks/src/useControlledValue/useControlledValue.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ChangeEvent } from 'react';
2-
import { act } from '@testing-library/react';
2+
import { act } from 'react-test-renderer';
33
import { renderHook } from '@testing-library/react-hooks';
44

55
import { useControlledValue } from './useControlledValue';
@@ -16,7 +16,7 @@ describe('packages/lib/useControlledValue', () => {
1616

1717
act(() => {
1818
current.handleChange({ target: { value: 'banana' } } as ChangeEvent<any>);
19-
current.setInternalValue('banana');
19+
current.setUncontrolledValue('banana');
2020
});
2121
expect(handler).toHaveBeenCalledWith(
2222
expect.objectContaining({ target: { value: 'banana' } }),

packages/hooks/src/useControlledValue/useControlledValue.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeEventHandler, useEffect, useState } from 'react';
1+
import { ChangeEventHandler, useState } from 'react';
22
import isUndefined from 'lodash/isUndefined';
33

44
interface ControlledValueReturnObject<T extends string> {
@@ -15,7 +15,7 @@ interface ControlledValueReturnObject<T extends string> {
1515
* A setter for the internal value.
1616
* Does not change the controlled value if the provided value has not changed.
1717
*/
18-
setInternalValue: React.Dispatch<React.SetStateAction<T>>;
18+
setUncontrolledValue: React.Dispatch<React.SetStateAction<T>>;
1919
}
2020

2121
/**
@@ -30,29 +30,22 @@ export const useControlledValue = <T extends string>(
3030
): ControlledValueReturnObject<T> => {
3131
const isControlled = !isUndefined(controlledValue);
3232

33-
// Keep track of state internally, initializing it to the controlled value
34-
const [value, setInternalValue] = useState<T>(controlledValue ?? ('' as T));
35-
36-
// If the controlled value changes, update the internal state variable
37-
useEffect(() => {
38-
if (!isUndefined(controlledValue)) {
39-
setInternalValue(controlledValue);
40-
}
41-
}, [controlledValue]);
33+
// Keep track of the uncontrolled value state internally
34+
const [uncontrolledValue, setUncontrolledValue] = useState<T>('' as T);
4235

4336
// Create a change event handler that either updates the internal state
4437
// or fires an external change handler
4538
const handleChange: ChangeEventHandler<any> = e => {
4639
changeHandler?.(e);
4740
if (!isControlled) {
48-
setInternalValue(e.target.value as T);
41+
setUncontrolledValue(e.target.value as T);
4942
}
5043
};
5144

5245
return {
5346
isControlled,
54-
value,
47+
value: isControlled ? controlledValue : uncontrolledValue,
5548
handleChange,
56-
setInternalValue,
49+
setUncontrolledValue,
5750
};
5851
};

0 commit comments

Comments
 (0)