How to define an on change listener for material-ui ?












-1















I'm using the React dashboard built by Creative Tim. My confusion is how do I define an onChange listener for a



The custom input class is defined as follows:



import React from "react";
import classNames from "classnames";
import PropTypes from "prop-types";
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import FormControl from "@material-ui/core/FormControl";
import InputLabel from "@material-ui/core/InputLabel";
import Input from "@material-ui/core/Input";
// @material-ui/icons
import Clear from "@material-ui/icons/Clear";
import Check from "@material-ui/icons/Check";
// core components
import customInputStyle from "assets/jss/material-dashboard-react/components/customInputStyle.jsx";

function CustomInput({ ...props }) {
const {
classes,
formControlProps,
labelText,
id,
labelProps,
inputProps,
error,
success
} = props;

const labelClasses = classNames({
[" " + classes.labelRootError]: error,
[" " + classes.labelRootSuccess]: success && !error
});
const underlineClasses = classNames({
[classes.underlineError]: error,
[classes.underlineSuccess]: success && !error,
[classes.underline]: true
});
const marginTop = classNames({
[classes.marginTop]: labelText === undefined
});
return (
<FormControl
{...formControlProps}
className={formControlProps.className + " " + classes.formControl}
>
{labelText !== undefined ? (
<InputLabel
className={classes.labelRoot + labelClasses}
htmlFor={id}
{...labelProps}
>
{labelText}
</InputLabel>
) : null}
<Input
classes={{
root: marginTop,
disabled: classes.disabled,
underline: underlineClasses
}}
id={id}
{...inputProps}
/>
{error ? (
<Clear className={classes.feedback + " " + classes.labelRootError} />
) : success ? (
<Check className={classes.feedback + " " + classes.labelRootSuccess} />
) : null}
</FormControl>
);
}

CustomInput.propTypes = {
classes: PropTypes.object.isRequired,
labelText: PropTypes.node,
labelProps: PropTypes.object,
id: PropTypes.string,
inputProps: PropTypes.object,
formControlProps: PropTypes.object,
error: PropTypes.bool,
success: PropTypes.bool
};

export default withStyles(customInputStyle)(CustomInput);


My usage of the CustomInput class is as follows:



    renderInput(key) {
return (
<GridItem xs={20} sm={20} md={12}>
<CustomInput
labelText={key}
id={key}
inputRef={() => console.log("value changed!")}
formControlProps={{
fullWidth: true
}}
inputProps={{
disabled: false
}}
/>
</GridItem>
);
}


When I type something into the input bar I want the message "value changed!" to appear in the console. How do I accomplish this?










share|improve this question



























    -1















    I'm using the React dashboard built by Creative Tim. My confusion is how do I define an onChange listener for a



    The custom input class is defined as follows:



    import React from "react";
    import classNames from "classnames";
    import PropTypes from "prop-types";
    // @material-ui/core components
    import withStyles from "@material-ui/core/styles/withStyles";
    import FormControl from "@material-ui/core/FormControl";
    import InputLabel from "@material-ui/core/InputLabel";
    import Input from "@material-ui/core/Input";
    // @material-ui/icons
    import Clear from "@material-ui/icons/Clear";
    import Check from "@material-ui/icons/Check";
    // core components
    import customInputStyle from "assets/jss/material-dashboard-react/components/customInputStyle.jsx";

    function CustomInput({ ...props }) {
    const {
    classes,
    formControlProps,
    labelText,
    id,
    labelProps,
    inputProps,
    error,
    success
    } = props;

    const labelClasses = classNames({
    [" " + classes.labelRootError]: error,
    [" " + classes.labelRootSuccess]: success && !error
    });
    const underlineClasses = classNames({
    [classes.underlineError]: error,
    [classes.underlineSuccess]: success && !error,
    [classes.underline]: true
    });
    const marginTop = classNames({
    [classes.marginTop]: labelText === undefined
    });
    return (
    <FormControl
    {...formControlProps}
    className={formControlProps.className + " " + classes.formControl}
    >
    {labelText !== undefined ? (
    <InputLabel
    className={classes.labelRoot + labelClasses}
    htmlFor={id}
    {...labelProps}
    >
    {labelText}
    </InputLabel>
    ) : null}
    <Input
    classes={{
    root: marginTop,
    disabled: classes.disabled,
    underline: underlineClasses
    }}
    id={id}
    {...inputProps}
    />
    {error ? (
    <Clear className={classes.feedback + " " + classes.labelRootError} />
    ) : success ? (
    <Check className={classes.feedback + " " + classes.labelRootSuccess} />
    ) : null}
    </FormControl>
    );
    }

    CustomInput.propTypes = {
    classes: PropTypes.object.isRequired,
    labelText: PropTypes.node,
    labelProps: PropTypes.object,
    id: PropTypes.string,
    inputProps: PropTypes.object,
    formControlProps: PropTypes.object,
    error: PropTypes.bool,
    success: PropTypes.bool
    };

    export default withStyles(customInputStyle)(CustomInput);


    My usage of the CustomInput class is as follows:



        renderInput(key) {
    return (
    <GridItem xs={20} sm={20} md={12}>
    <CustomInput
    labelText={key}
    id={key}
    inputRef={() => console.log("value changed!")}
    formControlProps={{
    fullWidth: true
    }}
    inputProps={{
    disabled: false
    }}
    />
    </GridItem>
    );
    }


    When I type something into the input bar I want the message "value changed!" to appear in the console. How do I accomplish this?










    share|improve this question

























      -1












      -1








      -1








      I'm using the React dashboard built by Creative Tim. My confusion is how do I define an onChange listener for a



      The custom input class is defined as follows:



      import React from "react";
      import classNames from "classnames";
      import PropTypes from "prop-types";
      // @material-ui/core components
      import withStyles from "@material-ui/core/styles/withStyles";
      import FormControl from "@material-ui/core/FormControl";
      import InputLabel from "@material-ui/core/InputLabel";
      import Input from "@material-ui/core/Input";
      // @material-ui/icons
      import Clear from "@material-ui/icons/Clear";
      import Check from "@material-ui/icons/Check";
      // core components
      import customInputStyle from "assets/jss/material-dashboard-react/components/customInputStyle.jsx";

      function CustomInput({ ...props }) {
      const {
      classes,
      formControlProps,
      labelText,
      id,
      labelProps,
      inputProps,
      error,
      success
      } = props;

      const labelClasses = classNames({
      [" " + classes.labelRootError]: error,
      [" " + classes.labelRootSuccess]: success && !error
      });
      const underlineClasses = classNames({
      [classes.underlineError]: error,
      [classes.underlineSuccess]: success && !error,
      [classes.underline]: true
      });
      const marginTop = classNames({
      [classes.marginTop]: labelText === undefined
      });
      return (
      <FormControl
      {...formControlProps}
      className={formControlProps.className + " " + classes.formControl}
      >
      {labelText !== undefined ? (
      <InputLabel
      className={classes.labelRoot + labelClasses}
      htmlFor={id}
      {...labelProps}
      >
      {labelText}
      </InputLabel>
      ) : null}
      <Input
      classes={{
      root: marginTop,
      disabled: classes.disabled,
      underline: underlineClasses
      }}
      id={id}
      {...inputProps}
      />
      {error ? (
      <Clear className={classes.feedback + " " + classes.labelRootError} />
      ) : success ? (
      <Check className={classes.feedback + " " + classes.labelRootSuccess} />
      ) : null}
      </FormControl>
      );
      }

      CustomInput.propTypes = {
      classes: PropTypes.object.isRequired,
      labelText: PropTypes.node,
      labelProps: PropTypes.object,
      id: PropTypes.string,
      inputProps: PropTypes.object,
      formControlProps: PropTypes.object,
      error: PropTypes.bool,
      success: PropTypes.bool
      };

      export default withStyles(customInputStyle)(CustomInput);


      My usage of the CustomInput class is as follows:



          renderInput(key) {
      return (
      <GridItem xs={20} sm={20} md={12}>
      <CustomInput
      labelText={key}
      id={key}
      inputRef={() => console.log("value changed!")}
      formControlProps={{
      fullWidth: true
      }}
      inputProps={{
      disabled: false
      }}
      />
      </GridItem>
      );
      }


      When I type something into the input bar I want the message "value changed!" to appear in the console. How do I accomplish this?










      share|improve this question














      I'm using the React dashboard built by Creative Tim. My confusion is how do I define an onChange listener for a



      The custom input class is defined as follows:



      import React from "react";
      import classNames from "classnames";
      import PropTypes from "prop-types";
      // @material-ui/core components
      import withStyles from "@material-ui/core/styles/withStyles";
      import FormControl from "@material-ui/core/FormControl";
      import InputLabel from "@material-ui/core/InputLabel";
      import Input from "@material-ui/core/Input";
      // @material-ui/icons
      import Clear from "@material-ui/icons/Clear";
      import Check from "@material-ui/icons/Check";
      // core components
      import customInputStyle from "assets/jss/material-dashboard-react/components/customInputStyle.jsx";

      function CustomInput({ ...props }) {
      const {
      classes,
      formControlProps,
      labelText,
      id,
      labelProps,
      inputProps,
      error,
      success
      } = props;

      const labelClasses = classNames({
      [" " + classes.labelRootError]: error,
      [" " + classes.labelRootSuccess]: success && !error
      });
      const underlineClasses = classNames({
      [classes.underlineError]: error,
      [classes.underlineSuccess]: success && !error,
      [classes.underline]: true
      });
      const marginTop = classNames({
      [classes.marginTop]: labelText === undefined
      });
      return (
      <FormControl
      {...formControlProps}
      className={formControlProps.className + " " + classes.formControl}
      >
      {labelText !== undefined ? (
      <InputLabel
      className={classes.labelRoot + labelClasses}
      htmlFor={id}
      {...labelProps}
      >
      {labelText}
      </InputLabel>
      ) : null}
      <Input
      classes={{
      root: marginTop,
      disabled: classes.disabled,
      underline: underlineClasses
      }}
      id={id}
      {...inputProps}
      />
      {error ? (
      <Clear className={classes.feedback + " " + classes.labelRootError} />
      ) : success ? (
      <Check className={classes.feedback + " " + classes.labelRootSuccess} />
      ) : null}
      </FormControl>
      );
      }

      CustomInput.propTypes = {
      classes: PropTypes.object.isRequired,
      labelText: PropTypes.node,
      labelProps: PropTypes.object,
      id: PropTypes.string,
      inputProps: PropTypes.object,
      formControlProps: PropTypes.object,
      error: PropTypes.bool,
      success: PropTypes.bool
      };

      export default withStyles(customInputStyle)(CustomInput);


      My usage of the CustomInput class is as follows:



          renderInput(key) {
      return (
      <GridItem xs={20} sm={20} md={12}>
      <CustomInput
      labelText={key}
      id={key}
      inputRef={() => console.log("value changed!")}
      formControlProps={{
      fullWidth: true
      }}
      inputProps={{
      disabled: false
      }}
      />
      </GridItem>
      );
      }


      When I type something into the input bar I want the message "value changed!" to appear in the console. How do I accomplish this?







      javascript reactjs material-ui eslint






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 22:00









      Raees Sharif-AamirRaees Sharif-Aamir

      54117




      54117
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can use the input's onChange handler.



          Try the snippet below. I hope it helps.






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          https://reactjs.org/docs/forms.html






          share|improve this answer
























          • How can I achieve this using the material-ui classes?

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • According to the documentation something like onChange={e => console.log("value changed!")} should work

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:20











          • onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          • But nothing appears in the console

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309376%2fhow-to-define-an-on-change-listener-for-material-ui-custominput%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use the input's onChange handler.



          Try the snippet below. I hope it helps.






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          https://reactjs.org/docs/forms.html






          share|improve this answer
























          • How can I achieve this using the material-ui classes?

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • According to the documentation something like onChange={e => console.log("value changed!")} should work

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:20











          • onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          • But nothing appears in the console

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21
















          1














          You can use the input's onChange handler.



          Try the snippet below. I hope it helps.






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          https://reactjs.org/docs/forms.html






          share|improve this answer
























          • How can I achieve this using the material-ui classes?

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • According to the documentation something like onChange={e => console.log("value changed!")} should work

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:20











          • onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          • But nothing appears in the console

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21














          1












          1








          1







          You can use the input's onChange handler.



          Try the snippet below. I hope it helps.






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          https://reactjs.org/docs/forms.html






          share|improve this answer













          You can use the input's onChange handler.



          Try the snippet below. I hope it helps.






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          https://reactjs.org/docs/forms.html






          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          const Input = () => 
          <input onChange={e => console.log(e.target.value)}/>

          ReactDOM.render( <Input /> , document.getElementById('root'));

          <div id="root"></div>

          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 22:45









          Dan KreigerDan Kreiger

          3,3622917




          3,3622917













          • How can I achieve this using the material-ui classes?

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • According to the documentation something like onChange={e => console.log("value changed!")} should work

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:20











          • onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          • But nothing appears in the console

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21



















          • How can I achieve this using the material-ui classes?

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

            – Raees Sharif-Aamir
            Nov 14 '18 at 22:54











          • According to the documentation something like onChange={e => console.log("value changed!")} should work

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:20











          • onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21











          • But nothing appears in the console

            – Raees Sharif-Aamir
            Nov 14 '18 at 23:21

















          How can I achieve this using the material-ui classes?

          – Raees Sharif-Aamir
          Nov 14 '18 at 22:54





          How can I achieve this using the material-ui classes?

          – Raees Sharif-Aamir
          Nov 14 '18 at 22:54













          Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

          – Raees Sharif-Aamir
          Nov 14 '18 at 22:54





          Adding onClick={() => console.log("value changed!")} to my <CustomInput ...> doesn't work

          – Raees Sharif-Aamir
          Nov 14 '18 at 22:54













          According to the documentation something like onChange={e => console.log("value changed!")} should work

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:20





          According to the documentation something like onChange={e => console.log("value changed!")} should work

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:20













          onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:21





          onChange func Callback fired when the value is changed. Signature: function(event: object) => void event: The event source of the callback. You can pull out the new value by accessing event.target.value.

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:21













          But nothing appears in the console

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:21





          But nothing appears in the console

          – Raees Sharif-Aamir
          Nov 14 '18 at 23:21


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309376%2fhow-to-define-an-on-change-listener-for-material-ui-custominput%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong