Capgemini / dcx-react-library

React Library UI/UX agnostic
https://main--6069a6f47f4b9f002171f8e1.chromatic.com
MIT License
113 stars 7 forks source link

Autocomplete component - changing defaultValue prop has no affect #462

Closed cg-julian-taylor closed 1 year ago

cg-julian-taylor commented 1 year ago

Describe the bug In the <Autocomplete /> component doesn't update if the defaultValue prop is changed.

Suggested fix:

React.useEffect(() => {
    setUserInput(defaultValue);
}, [defaultValue]);

At around line 368.

To Reproduce Steps to reproduce the behavior: Use this component in a form that has its own state. Pass a piece of that state to <Autocomplete /> as its defaultValue prop. Update the state in the form. Autocomplete doesn't update its selected value accordingly.

In the dcx library replace the contents of example/src/components/AutocompleteDemo.tsx with the following:

import * as React from 'react';
import { Autocomplete } from '@capgeminiuk/dcx-react-library';

export const AutocompleteDemo = () => {
  const [defaultValue, setDefaultValue] = React.useState('Apple');
  const handleClick = (value: string) => {
    setDefaultValue(value);
  };

  return (
    <>
      default value = [{defaultValue}]
      <button onClick={() => handleClick('Apple')}>Apple</button>
      <button onClick={() => handleClick('Banana')}>Banana</button>
      <Autocomplete
        options={[
          'Apple',
          'Banana',
          'Coconut',
          'Jambul',
          'Peach',
          'Pomegranate',
          'Pineapple',
          'Mulberry',
          'Lychee',
        ]}
        defaultValue={defaultValue}
        onSelected={(value) => handleClick(value)}
      />
    </>
  );
};

Expected behavior it should update the value passed

Please follow the following instruction to create the new branch:

git checkout release/0.8 git pull git checkout -b 'feature/autocomplete-update-value'

daniele-zurico commented 1 year ago

closed by https://github.com/Capgemini/dcx-react-library/pull/496