From a9b64cadffc30aaff1212da5593ec245f5b5e5aa Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Tue, 22 Mar 2022 14:38:11 +0000 Subject: [PATCH] fix(textarea): allow row and expandable props to be used together Fixes: #4959 --- cypress/fixtures/commonComponents/textarea.json | 8 ++++++-- cypress/integration/common/textarea.feature | 10 ++++++++++ cypress/support/step-definitions/text-area-steps.js | 4 ++++ src/components/textarea/textarea.component.js | 11 ++++++++--- src/components/textarea/textarea.style.js | 4 +++- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/cypress/fixtures/commonComponents/textarea.json b/cypress/fixtures/commonComponents/textarea.json index 3efe1fcb40..1dbe8799bb 100644 --- a/cypress/fixtures/commonComponents/textarea.json +++ b/cypress/fixtures/commonComponents/textarea.json @@ -1,10 +1,14 @@ -{ +{ "expandable": { "expandable": true }, "expandableFalse": { "expandable": false }, + "expandableWithRows": { + "expandable": true, + "rows": 7 + }, "cols1": { "cols": 1 }, @@ -149,4 +153,4 @@ "characterLimit": 5, "enforceCharacterLimit": false } -} \ No newline at end of file +} diff --git a/cypress/integration/common/textarea.feature b/cypress/integration/common/textarea.feature index 6e4f26b165..deaf64c7e7 100644 --- a/cypress/integration/common/textarea.feature +++ b/cypress/integration/common/textarea.feature @@ -39,6 +39,16 @@ Feature: Textarea component | 100 | rows100 | | 300 | rows300 | + @positive + Scenario Outline: Set expandable and rows to + When I open default "Textarea Test" component with "textarea" json from "commonComponents" using "" object name + Then rows is set to "" + And Textarea height is "119px" + Examples: + | rows | nameOfObject | + | 7 | expandableWithRows | + + @positive Scenario Outline: Set placeholder to When I open default "Textarea Test" component with "textarea" json from "commonComponents" using "" object name diff --git a/cypress/support/step-definitions/text-area-steps.js b/cypress/support/step-definitions/text-area-steps.js index fe661e2206..f95aa58802 100644 --- a/cypress/support/step-definitions/text-area-steps.js +++ b/cypress/support/step-definitions/text-area-steps.js @@ -12,6 +12,10 @@ Then("Textarea component is not expandable", () => { textareaChildren().should("not.have.css", "height", "85px"); }); +Then("Textarea height is {string}", (height) => { + textareaChildren().should("have.css", "height", height); +}); + Then("cols is set to {string}", (colsValue) => { textareaChildren().should("have.attr", "cols", colsValue); }); diff --git a/src/components/textarea/textarea.component.js b/src/components/textarea/textarea.component.js index fae44a58a4..0653391407 100644 --- a/src/components/textarea/textarea.component.js +++ b/src/components/textarea/textarea.component.js @@ -10,7 +10,7 @@ import { InputBehaviour } from "../../__internal__/input-behaviour"; import { filterStyledSystemMarginProps } from "../../style/utils"; import InputIconToggle from "../../__internal__/input-icon-toggle"; import guid from "../../__internal__/utils/helpers/guid"; -import StyledTextarea from "./textarea.style"; +import StyledTextarea, { MIN_HEIGHT } from "./textarea.style"; import LocaleContext from "../../__internal__/i18n-context"; import { TooltipProvider } from "../../__internal__/tooltip-provider"; import useInputAccessibility from "../../hooks/__internal__/useInputAccessibility"; @@ -72,13 +72,12 @@ const Textarea = ({ const inputRef = useRef(); - const minHeight = useRef(0); + const minHeight = useRef(MIN_HEIGHT); const expandTextarea = () => { const textarea = inputRef.current; if (textarea.scrollHeight > minHeight.current) { - // Reset height to zero - IE specific textarea.style.height = "0px"; // Set the height so all content is shown textarea.style.height = `${Math.max( @@ -103,6 +102,12 @@ const Textarea = ({ fieldHelp, }); + useEffect(() => { + if (rows) { + minHeight.current = inputRef.current.scrollHeight; + } + }, [rows]); + useEffect(() => { if (expandable) { expandTextarea(); diff --git a/src/components/textarea/textarea.style.js b/src/components/textarea/textarea.style.js index 61e41327fe..64362b4e2d 100644 --- a/src/components/textarea/textarea.style.js +++ b/src/components/textarea/textarea.style.js @@ -6,12 +6,14 @@ import { StyledLabelContainer } from "../../__internal__/label/label.style"; import InputIconToggleStyle from "../../__internal__/input-icon-toggle/input-icon-toggle.style"; import BaseTheme from "../../style/themes/base"; +export const MIN_HEIGHT = 40; + const StyledTextarea = styled.div` ${margin}; ${StyledInput} { resize: none; - min-height: 40px; + min-height: ${MIN_HEIGHT}px; margin-top: 5px; margin-bottom: 5px; }