Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TextField): add a controlled variant #2180

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions packages/core/src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import cx from "classnames";
import React, { forwardRef, useCallback, useEffect, useMemo, useRef, useState } from "react";
import React, {
ChangeEvent,
ChangeEventHandler,
forwardRef,
useCallback,
useEffect,
useMemo,
useRef,
useState
} from "react";
import useDebounceEvent from "../../hooks/useDebounceEvent";
import Icon from "../Icon/Icon";
import { backwardCompatibilityForProperties } from "../../helpers/backwardCompatibilityForProperties";
Expand Down Expand Up @@ -86,6 +95,10 @@ export interface TextFieldProps extends VibeComponentProps {
* Apply new style for read only, use along with `readonly` prop
*/
withReadOnlyStyle?: boolean;
/**
* When true, component is controlled by an external state
*/
controlled?: boolean;
}

const TextField: VibeComponent<TextFieldProps, unknown> & {
Expand Down Expand Up @@ -138,7 +151,8 @@ const TextField: VibeComponent<TextFieldProps, unknown> & {
tabIndex,
underline = false,
name,
withReadOnlyStyle
withReadOnlyStyle,
controlled = false
},
ref
) => {
Expand Down Expand Up @@ -171,13 +185,28 @@ const TextField: VibeComponent<TextFieldProps, unknown> & {
[onChange, isRequiredAndEmpty]
);

const { inputValue, onEventChanged, clearValue } = useDebounceEvent({
const {
inputValue: uncontrolledInput,
talkor marked this conversation as resolved.
Show resolved Hide resolved
onEventChanged,
clearValue
} = useDebounceEvent({
delay: debounceRate,
onChange: onChangeCallback,
initialStateValue: value,
trim
});

const inputValue = useMemo(() => {
return controlled ? value : uncontrolledInput;
}, [controlled, value, uncontrolledInput]);

const handleChange = useCallback<ChangeEventHandler>(
(event: ChangeEvent<Partial<HTMLInputElement>>) => {
controlled ? onChangeCallback(event.target.value) : onEventChanged(event);
},
[controlled, onChangeCallback, onEventChanged]
);

const currentStateIconName = useMemo(() => {
if (secondaryIconName) {
return inputValue ? secondaryIconName : iconName;
Expand All @@ -196,10 +225,10 @@ const TextField: VibeComponent<TextFieldProps, unknown> & {
}
// Do it cause otherwise the value is not cleared in target object
inputRef.current.value = "";
clearValue();
controlled ? onChangeCallback("") : clearValue();
}
onIconClick(currentStateIconName);
}, [inputRef, disabled, clearOnIconClick, onIconClick, currentStateIconName, clearValue]);
}, [disabled, clearOnIconClick, onIconClick, currentStateIconName, controlled, onChangeCallback, clearValue]);

const validationClass = useMemo(() => {
if ((!validation || !validation.status) && !isRequiredAndEmpty) {
Expand Down Expand Up @@ -256,7 +285,7 @@ const TextField: VibeComponent<TextFieldProps, unknown> & {
placeholder={placeholder}
autoComplete={autoComplete}
value={inputValue}
onChange={onEventChanged}
onChange={handleChange}
disabled={disabled}
readOnly={readonly}
ref={mergedRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,19 @@ describe("TextField Tests", () => {
});
expect(input.value).toBe(value.trim());
});

describe("controlled", () => {
it("should call onChange with the new value when controlled is true", () => {
const handleChange = jest.fn();
render(<TextField placeholder="Enter text" onChange={handleChange} value="value" controlled />);

const input = screen.getByPlaceholderText("Enter text");
expect(input.value).toBe("value");

fireEvent.change(input, { target: { value: "new value" } });

expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange).toHaveBeenCalledWith("new value", expect.anything());
});
});
});