canonical / react-components

A set of components based on Vanilla Framework
https://canonical.github.io/react-components
82 stars 49 forks source link

Textarea doesn't update value when it's an empty string #1068

Closed andesol closed 2 months ago

andesol commented 3 months ago

Consider this simplified example:

import { useState } from 'react';
import { Button, Textarea } from "@canonical/react-components";

function App() {
  const [data, setData] = useState("Default text");

  return (
      <div>
        <Button onClick={() => setData("")}>
          Show nothing
        </Button>
        <p>Current data is: {data}</p>
        <Textarea
          value={data}
          onChange={(e) => setData(e.target.value)}
        />
      </div>
    );
}

This is the default scenario:

image

I'd expect clicking the button to clear the value in the Textarea. However, while the state is correctly updated (to an empty string), the Textarea still shows the previous value.

image

andesol commented 3 months ago

It looks like this line should be

props.value ?? innerValue
bartaz commented 3 months ago

Good catch @andesol. Care to propose a PR? I guess it would be good to have a test case that catches this as well.

andesol commented 3 months ago

@bartaz Indeed, here's the PR https://github.com/canonical/react-components/pull/1070