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

Character counter on Textfield component does not update with initial value #2042

Closed
paal2707 opened this issue May 21, 2024 · 4 comments · Fixed by #2056
Closed

Character counter on Textfield component does not update with initial value #2042

paal2707 opened this issue May 21, 2024 · 4 comments · Fixed by #2056
Labels
🐛 bug Something isn't working

Comments

@paal2707
Copy link
Contributor

Description of the bug

When I write a character in the Textfield component the character counter shows the number of positions left correctly. But after I have saved the value and refreshed the page, The text shows up in the input but the character counter shows the incorrect message "Du har 9 av 9 tegn igjen" (the field is already full). If I start editing in the opened field again, it updates after the first character change and behaves as expected.

Steps To Reproduce

  1. Add initial text to Textfield with character counter
  2. Load page

Additional Information

image
@Barsnes
Copy link
Member

Barsnes commented May 22, 2024

Hey, thanks for the bug report! 🐛
Are you using defaultValue to set the initial value of the input?
I tested with defaultValue, and it seems to work fine. However, I get the same bug you are describing when using just value for the initial input.

@paal2707
Copy link
Contributor Author

I am not using defaultValue because i want the component to be controlled. When page is reloaded this occurs.

@Barsnes
Copy link
Member

Barsnes commented May 23, 2024

I am not using defaultValue because i want the component to be controlled. When page is reloaded this occurs.

I understand, would something like this work for you?
codesandbox

const DEFAULT_VALUE = "default";

export default function Controlled() {
  const [value, setValue] = useState(DEFAULT_VALUE);

  return (
    <Textfield
      value={value}
      onChange={(e) => setValue(e.target.value)}
      defaultValue={DEFAULT_VALUE}
      characterLimit={{
        maxCount: 9,
      }}
    />
  );
}

edit: I see that this throws a console error 🤔

@paal2707
Copy link
Contributor Author

I am not using defaultValue because i want the component to be controlled. When page is reloaded this occurs.

I understand, would something like this work for you? codesandbox

const DEFAULT_VALUE = "default";

export default function Controlled() {
  const [value, setValue] = useState(DEFAULT_VALUE);

  return (
    <Textfield
      value={value}
      onChange={(e) => setValue(e.target.value)}
      defaultValue={DEFAULT_VALUE}
      characterLimit={{
        maxCount: 9,
      }}
    />
  );
}

edit: I see that this throws a console error 🤔

This wont fix my problem because if you reload the page the state is beeing preserved but not the defaultValue. Also if i set the defaultValue initially to my value, then it throws the error you are referring to. I have added a pull-request with a possible solution. #2056

@github-project-automation github-project-automation bot moved this from 👀 Ready for review to ✅ Done in Team Designsystemet May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants