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

fix(input): input display with hidden type #3174

Merged
merged 9 commits into from
Jun 14, 2024
6 changes: 6 additions & 0 deletions .changeset/selfish-tips-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/input": patch
"@nextui-org/theme": patch
---

Fix input display with hidden type (#3170)
27 changes: 27 additions & 0 deletions packages/components/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,33 @@ describe("Input", () => {

expect(onClear).toHaveBeenCalledTimes(1);
});

it("should not display input with hidden type", async () => {
const wrapper = render(
<>
<Input data-testid="input-1" type="hidden" />
<Input data-testid="input-2" />
</>,
);

const {container} = wrapper;

const inputBaseWrappers = container.querySelectorAll("[data-slot='base']");

expect(inputBaseWrappers).toHaveLength(2);

const inputs = container.querySelectorAll("input");

expect(inputs).toHaveLength(2);

expect(inputBaseWrappers[0]).toHaveAttribute("data-hidden");

expect(inputBaseWrappers[1]).not.toHaveAttribute("data-hidden");

expect(inputs[0]).not.toBeVisible();

expect(inputs[1]).toBeVisible();
});
});

describe("Input with React Hook Form", () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,11 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
const isFilledByDefault = ["date", "time", "month", "week", "range"].includes(type!);
const isFilled = !isEmpty(inputValue) || isFilledByDefault;
const isFilledWithin = isFilled || isFocusWithin;
const baseStyles = clsx(classNames?.base, className, isFilled ? "is-filled" : "");
const isHiddenType = type === "hidden";
const isMultiline = originalProps.isMultiline;

const baseStyles = clsx(classNames?.base, className, isFilled ? "is-filled" : "");

const handleClear = useCallback(() => {
setInputValue("");

Expand Down Expand Up @@ -251,6 +253,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
labelPlacement,
isClearable,
disableAnimation,
isHiddenType,
}),
[
objectToDeps(variantProps),
Expand Down Expand Up @@ -286,6 +289,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
"data-has-helper": dataAttr(hasHelper),
"data-has-label": dataAttr(hasLabel),
"data-has-value": dataAttr(!isPlaceholderShown),
"data-hidden": dataAttr(isHiddenType),
...focusWithinProps,
...props,
};
Expand All @@ -307,6 +311,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
isFilledWithin,
hasPlaceholder,
focusWithinProps,
isHiddenType,
originalProps.isReadOnly,
originalProps.isRequired,
originalProps.isDisabled,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/theme/src/components/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {dataFocusVisibleClasses, groupDataFocusVisibleClasses} from "../utils";
*/
const input = tv({
slots: {
base: "group flex flex-col",
base: "group flex flex-col data-[hidden=true]:hidden",
label: [
"absolute",
"z-10",
Expand Down
Loading