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

Propagate value of selected item as onChange event on input #70

Open
mazeForGit opened this issue Nov 17, 2023 · 3 comments
Open

Propagate value of selected item as onChange event on input #70

mazeForGit opened this issue Nov 17, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@mazeForGit
Copy link

mazeForGit commented Nov 17, 2023

Problem
I can get changes on input via onChange event and changes on item-selection via onSelect.
But on item-selection the input.value is changed as well.
Event onChange is therefore a combination of input.value change from user input as well as item selection.

Solution
onSelect should trigger onChange with new value as well

used environment
"bootstrap": "^5.3.1",
"react-bootstrap": "^2.8.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-datalist-input": "^3.2.1"

@andrelandgraf andrelandgraf self-assigned this Nov 17, 2023
@andrelandgraf andrelandgraf added the bug Something isn't working label Nov 17, 2023
@andrelandgraf
Copy link
Owner

Hey @mazeForGit, thanks for opening the issues. Yes, onChange should fire whenever the input field changes, including on selection. Let me have a look later today and maybe add a fix! :)

@andrelandgraf
Copy link
Owner

andrelandgraf commented Nov 17, 2023

Hey! Just looked into this and I think you probably want to use the value and setValue states instead of onChange:

export default function Index() {
  const [inputValue, setInputValue] = useState('');
  const { value, setValue } = useComboboxControls();

  // onChange is only updated on input change by the user
  console.log('onChange value', inputValue);
  // `value`/`setValue` will update `value` both on input change and on select
  console.log('setValue value', value);

  return (
    <DatalistInput
    label="Select your favorite flavor"
    placeholder="Chocolate"
    items={items}
    value={value}
    // setValue receives the latest string value of the input field
    setValue={setValue}
    onSelect={(i) => console.log("onSelect", i)}
    inputProps={{
      // onChange called on input change, receives the change `Event`
      onChange: (e) => setInputValue(e.target.value),
    }}
  />);
}

@andrelandgraf
Copy link
Owner

I agree that this is confusing. The inputProps are passed down to the underlying input element, and the onChange on input is not triggered when programmatically updating the input value. That's why setValue exists; it is called both onChange and onSelect to update the input value accordingly.

So no bug, just a bit confusing. In the next iteration of this package, I may want to remove value and setValue in favor for value and onChange input props to trigger/update on select, as it does when using the native HTML input & datalist elements.

@andrelandgraf andrelandgraf added enhancement New feature or request and removed bug Something isn't working labels Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants