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

[BUG] - Components/Textarea #1085

Closed
reversoid opened this issue Mar 31, 2023 · 1 comment · Fixed by #1088
Closed

[BUG] - Components/Textarea #1085

reversoid opened this issue Mar 31, 2023 · 1 comment · Fixed by #1088
Labels
🐛 Type: Bug Something isn't working

Comments

@reversoid
Copy link

Describe the bug

Textarea autosize works bad if we pass onChange callback that changes state

Your Example Website or App

https://codesandbox.io/p/sandbox/dreamy-faraday-b9gkdi?file=%2Fsrc%2FApp.tsx&selection=%5B%7B%22endColumn%22%3A14%2C%22endLineNumber%22%3A11%2C%22startColumn%22%3A14%2C%22startLineNumber%22%3A11%7D%5D

Steps to Reproduce the Bug or Issue

  1. Go to provided codesanbox examle
  2. Write some text in two fields, use linebreaks to enlarge textareas

Expected behavior

As a user, I expect textearea with onChange(() => setState) callback enlarge the same way as textearea without it does.

Screenshots or Videos

image

Operating System Version

Windows 10

Browser

Chrome

@reversoid reversoid added the 🐛 Type: Bug Something isn't working label Mar 31, 2023
@nooikko
Copy link

nooikko commented Apr 9, 2023

Hi! New around these parts but decided to look into this. Looks like the main issue is how the textbox handles resizing.

Issue:
When you pass in the value prop, the Textarea component marks itself as controlled using a variable called isControlled. Once that is set, the following code responsible for handling the onChange resizing is disabled:

// ./packages/react/src/textarea.tsx
// Line 99

    const handleChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
      if (!isControlled) {
        resizeTextarea();
      }
      onChange && onChange(event);
    };

This is then followed up by failure to check the prop.value in the resizeTextarea function:

// ./packages/react/src/textarea.tsx
// Line 72
    const resizeTextarea = () => {
      const node = textareaRef.current!;
      const nodeSizingData =
        cacheMeasurements && measurementsCacheRef.current
          ? measurementsCacheRef.current
          : getSizingData(node);

      if (!nodeSizingData) {
        return;
      }

      measurementsCacheRef.current = nodeSizingData;

      const [height, rowHeight] = calculateNodeHeight(
        nodeSizingData,
        // This is the problem line
        node.value || node.placeholder || "x",
        rows || minRows,
        rows || maxRows,
      );

      if (heightRef.current !== height) {
        heightRef.current = height;
        node.style.setProperty("height", `${height}px`, "important");
        onHeightChange && onHeightChange(height, {rowHeight});
      }
    };

I'm pretty new to open-source stuff but I'll see if I'm able to push up a PR shortly that would resolve this. In the meantime, there's no great way to handle this since the resizeTextarea function isn't exported.

nooikko pushed a commit to nooikko/nextui that referenced this issue Apr 9, 2023
jrgarciadev pushed a commit that referenced this issue May 25, 2023
…led (#1088)

Co-authored-by: Emilia Penney <elijahpenney@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants