Skip to content

Commit

Permalink
Merge pull request #3012 from BluePandaCard/1785-changing-text-field-…
Browse files Browse the repository at this point in the history
…hidden-input-has-no-effect

1785 changing text field hidden input has no effect
  • Loading branch information
GCHQ-Developer-299 authored Jan 15, 2025
2 parents b95e6d1 + 28538e7 commit 6c4ea23
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/react/src/component-tests/IcTextField/IcTextField.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Controlled,
Uncontrolled,
TextFieldWithMaxLengthMessage,
HiddenInput,
} from "./IcTextFieldTestData";
import { setThresholdBasedOnEnv } from "../../../cypress/utils/helpers";

Expand Down Expand Up @@ -342,6 +343,18 @@ describe("IcTextField end-to-end tests", () => {

input.should(HAVE_ATTR, "title", "new-input-title");
});

it("should remove the hidden input when hiddenInput prop is toggled", () => {
mount(<HiddenInput />);

cy.get(IC_TEXTFIELD).invoke("prop", "hiddenInput", true);

cy.get(IC_TEXTFIELD).find('input[type="hidden"]').should("exist");

cy.get(IC_TEXTFIELD).invoke("prop", "hiddenInput", false);

cy.get(IC_TEXTFIELD).find('input[type="hidden"]').should("not.exist");
});
});

describe("IcTextField visual regression tests", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,9 @@ export const CustomSizeTextField = (): ReactElement => (
></IcTextField>
</div>
);

export const HiddenInput = (): ReactElement => (
<div style={style}>
<IcTextField label="my label" />
</div>
);
23 changes: 22 additions & 1 deletion packages/react/src/stories/ic-text-field.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ArgsTable,
Description,
} from "@storybook/addon-docs";
import { IcTextField } from "../components";
import { IcTextField, IcButton } from "../components";
import {SlottedSVG} from "../react-component-lib/slottedSVG";
import readme from "../../../web-components/src/components/ic-text-field/readme.md";

Expand Down Expand Up @@ -561,6 +561,27 @@ export const Uncontrolled = () => {
</Story>
</Canvas>

### Hidden input

export const HiddenInput = () => {
const [renderHidden, setRenderHidden] = useState(true);
const handleChange = () => {
setRenderHidden(!renderHidden);
};
return (
<>
<IcTextField label="my label" hiddenInput={renderHidden} />
<IcButton onClick={() => handleChange()}>Toggle hidden input</IcButton>
</>
);
};

<Canvas>
<Story name="Hidden input">
<HiddenInput />
</Story>
</Canvas>

### Playground

export const defaultArgs = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
isSlotUsed,
removeDisabledFalse,
checkSlotInChildMutations,
removeHiddenInput,
} from "../../utils/helpers";
import { IC_INHERITED_ARIA } from "../../utils/constants";
import {
Expand Down Expand Up @@ -575,9 +576,10 @@ export class TextField {

const invalid = `${currentStatus === IcInformationStatus.Error}`;

if (hiddenInput) {
renderHiddenInput(true, this.el, name, value, disabledMode);
}
hiddenInput
? renderHiddenInput(true, this.el, name, value, disabledMode)
: removeHiddenInput(this.el);

return (
<Host class={{ ["fullwidth"]: fullWidth }}>
<ic-input-container readonly={readonly} disabled={disabledMode}>
Expand Down

0 comments on commit 6c4ea23

Please sign in to comment.