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

[SelectField] currentTarget no longer works #5085

Closed
wlcdesigns opened this issue Aug 29, 2016 · 9 comments
Closed

[SelectField] currentTarget no longer works #5085

wlcdesigns opened this issue Aug 29, 2016 · 9 comments
Labels
component: select This is the name of the generic UI component, not the React module!

Comments

@wlcdesigns
Copy link

wlcdesigns commented Aug 29, 2016

Prior to 0.15.4 I was able to capture custom data from the selected menu items with event.currentTarget.dataset. Now event.currentTarget returns null onChange

Example:

<SelectField value={this.state.value} onChange={this.handleSelect}>
      <MenuItem value={1} data-customData="foo_bar" primaryText="Choose Something" />
      <MenuItem value={1} data-customData="bar_foo" primaryText="Choose Something Else etc." />
</SelectField>
@M1chaelTran
Copy link

+1

@oliviertassinari oliviertassinari changed the title currentTarget no longer works in Select Field [SelectField] currentTarget no longer works Mar 26, 2017
@oliviertassinari oliviertassinari added the component: select This is the name of the generic UI component, not the React module! label Mar 26, 2017
@ghost
Copy link

ghost commented Oct 16, 2019

This is occurring again in v4.5.1.

The reason I default to using currentTarget instead of target is noted here.

The Material-ui docs say to use target for Select, but that does not match with the rest of the inputs and I think it is sending the wrong signal because I attach the onChange handler to the Select.

I'm not sure if anyone will see this comment, so I'll wait a few days before I make a new issue.

Thank you.

@oliviertassinari
Copy link
Member

@wbstow In practice, you would like to see this diff land?

diff --git a/packages/material-ui/src/Select/SelectInput.js b/packages/material-ui/src/Select/SelectInput.js
index 9b42a632c..6b39ab47c 100644
--- a/packages/material-ui/src/Select/SelectInput.js
+++ b/packages/material-ui/src/Select/SelectInput.js
@@ -127,6 +127,7 @@ const SelectInput = React.forwardRef(function SelectInput(props, ref) {

       event.persist();
       event.target = { value: newValue, name };
+      event.currentTarget = event.target;
       onChange(event, child);
     }
   };

@ghost
Copy link

ghost commented Oct 16, 2019

@oliviertassinari It's up to you - with event.currentTarget.value I am getting the number zero (0) no matter what is selected (not the index) and the currentTarget is the select element the selected LI element. With event.target.value I am getting the value of the selected item and target is an object: { value: "TheValue", name: undefined }.

Having access to both might be useful to some, but I think it might be confusing to differentiate accessing index via currentTarget and value via target so another option might be to add the index to the target object if you deem access to index to be important.

I am not sure if this wasn't a breaking change because I could swear that my Select was working before I updated to v4.5.1. If you want - I can roll back to find out.

Thank you.

@ghost
Copy link

ghost commented Oct 16, 2019

If you're trying to keep the signalling of the currentTarget in line with what happens in the DOM, then currentTarget should really be the element that the event handler was attached to, but I don't know if anyone cares about such things.

I corrected my last comment: currentTarget in this situation is the selected LI element, not the Select element.

@ghost
Copy link

ghost commented Oct 16, 2019

Sorry - one more correction: With currentTarget.value it's not even the index of the selected item. It's just a zero (0), no matter which item is selected.

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 16, 2019

I don't think that we change it, event.target should be preferred.

@ghost
Copy link

ghost commented Oct 16, 2019

OK, I had standardized on using currentTarget because I always want to get the value of the thing I attached the onChange handler to. That does seem to be working with all the other @material-ui/core inputs.

Whatever you decide is fine with me, just pointing it out.

Thanks!!

@ghost
Copy link

ghost commented Oct 16, 2019

To make my Selects work, I simply created a different version of my useInputValue hook, which uses currentTarget called useInputTargetValue - so it's no big deal for me to alternate between these as necessary:

export function useInputTargetValue(defaultValue = "") {
  const [value, setValue] = React.useState(defaultValue);
  /** @param {React.SyntheticEvent<HTMLInputElement>} e */
  function onChange(e) {
    setValue(e.target.value);
  }
  return [value, onChange, setValue];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module!
Projects
None yet
Development

No branches or pull requests

3 participants