How to create a dynamic input type component










2















I'm developing a dynamic component where the input can be used for several types: text, password, number, date, etc. The idea is to use this input, no matter the type and where to implement it, as long its adaptable. I thought using state was a nice idea, but I have no clue how to do this. Any thoughts?



import React, Component from 'react';
import './styles.css';

export default class InputField extends Component
constructor(props)
super(props);

this.state =
name: '',
password: false,
type: ''



render ()
return (
<div>
<label className='f-size'>this.state.name</label>
<input
className='input'
name=this.state.name
placeholder=this.state.name
value=this.props.value
type=this.state.type
onChange=this.props.onChange
/>
<span className="errorMessage">this.props.error</span>
<span className="errorMessage">this.props.missField</span>

</div>

)




Thank you!










share|improve this question
























  • How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

    – Tholle
    Nov 13 '18 at 12:46











  • Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

    – Charlie
    Nov 13 '18 at 12:49















2















I'm developing a dynamic component where the input can be used for several types: text, password, number, date, etc. The idea is to use this input, no matter the type and where to implement it, as long its adaptable. I thought using state was a nice idea, but I have no clue how to do this. Any thoughts?



import React, Component from 'react';
import './styles.css';

export default class InputField extends Component
constructor(props)
super(props);

this.state =
name: '',
password: false,
type: ''



render ()
return (
<div>
<label className='f-size'>this.state.name</label>
<input
className='input'
name=this.state.name
placeholder=this.state.name
value=this.props.value
type=this.state.type
onChange=this.props.onChange
/>
<span className="errorMessage">this.props.error</span>
<span className="errorMessage">this.props.missField</span>

</div>

)




Thank you!










share|improve this question
























  • How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

    – Tholle
    Nov 13 '18 at 12:46











  • Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

    – Charlie
    Nov 13 '18 at 12:49













2












2








2


1






I'm developing a dynamic component where the input can be used for several types: text, password, number, date, etc. The idea is to use this input, no matter the type and where to implement it, as long its adaptable. I thought using state was a nice idea, but I have no clue how to do this. Any thoughts?



import React, Component from 'react';
import './styles.css';

export default class InputField extends Component
constructor(props)
super(props);

this.state =
name: '',
password: false,
type: ''



render ()
return (
<div>
<label className='f-size'>this.state.name</label>
<input
className='input'
name=this.state.name
placeholder=this.state.name
value=this.props.value
type=this.state.type
onChange=this.props.onChange
/>
<span className="errorMessage">this.props.error</span>
<span className="errorMessage">this.props.missField</span>

</div>

)




Thank you!










share|improve this question
















I'm developing a dynamic component where the input can be used for several types: text, password, number, date, etc. The idea is to use this input, no matter the type and where to implement it, as long its adaptable. I thought using state was a nice idea, but I have no clue how to do this. Any thoughts?



import React, Component from 'react';
import './styles.css';

export default class InputField extends Component
constructor(props)
super(props);

this.state =
name: '',
password: false,
type: ''



render ()
return (
<div>
<label className='f-size'>this.state.name</label>
<input
className='input'
name=this.state.name
placeholder=this.state.name
value=this.props.value
type=this.state.type
onChange=this.props.onChange
/>
<span className="errorMessage">this.props.error</span>
<span className="errorMessage">this.props.missField</span>

</div>

)




Thank you!







javascript reactjs react-component






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 13:39









Spara

1




1










asked Nov 13 '18 at 12:35









RCohenRCohen

23711




23711












  • How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

    – Tholle
    Nov 13 '18 at 12:46











  • Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

    – Charlie
    Nov 13 '18 at 12:49

















  • How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

    – Tholle
    Nov 13 '18 at 12:46











  • Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

    – Charlie
    Nov 13 '18 at 12:49
















How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

– Tholle
Nov 13 '18 at 12:46





How do you want to configure the type? You could use this.props.type instead and let the user of InputField configure it.

– Tholle
Nov 13 '18 at 12:46













Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

– Charlie
Nov 13 '18 at 12:49





Is there any reason why you want the state in the input. Surely, you'd want the value of the input set on it's parent?

– Charlie
Nov 13 '18 at 12:49












4 Answers
4






active

oldest

votes


















2














I personally think you should control this via props, seeing as the value will only be meaningful to the Input's parent.



I used this



const InputField = (
name,
placeholder,
value,
type,
onChange,
error,
missField
) => (
<div>
<label className="f-size">name</label>
<input
className="input"
name=name
placeholder=placeholder
value=value
type=type
onChange=onChange
/>
<span className="errorMessage">error</span>
<span className="errorMessage">missField</span>
</div>
);


Parent component:



class App extends React.Component 
constructor(props)
super(props);

this.handleChange = this.handleChange.bind(this);

state =
value: '',
password: '',
;

handleChange(event)
this.setState( [event.target.name]: event.target.value );

render()
return (
<div className="App">
<InputField
value=this.state.value
type="number"
name="value"
onChange=this.handleChange
/>
<InputField
value=this.state.password
type="password"
name="password"
onChange=this.handleChange
/>
</div>
);




Code Sandbox: https://codesandbox.io/s/y4ljv75k9



Edited to used a stateless component. Not sure if you want state to handle error messages but from your example, this is a valid solution.






share|improve this answer
































    1














    <InputField type="text" />
    <InputField type="password" />

    <input
    className='input'
    name=this.state.name
    placeholder=this.state.name
    value=this.props.value
    type=this.props.type
    onChange=this.props.onChange
    />


    I would use props to change the type and manage the component.
    You could then control the component from a form definition






    share|improve this answer






























      0














      You should use props not state, so you can pass



      <InputType type="text" />
      <InputType type="password" />
      <InputType type="number" />


      and for the other params you can use props also.






      share|improve this answer






























        0














        You could use this.props.type but the standard jsx input component is already dynamic as you can see from my example below :






        var root = document.getElementById('root');
        class InputField extends React.Component
        render()
        return (
        <div>
        <input type=this.props.type />
        </div>
        )



        class App extends React.Component
        render()
        return (
        <div>
        <input type='date' />
        <InputField type='password'/>
        </div>
        )



        ReactDOM.render(<App />, root)

        <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>
        <div id='root'></div>





        Is there a reason why you would like to use a custom input component?






        share|improve this answer























        • The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

          – RCohen
          Nov 13 '18 at 13:28










        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%2f53281125%2fhow-to-create-a-dynamic-input-type-component%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        I personally think you should control this via props, seeing as the value will only be meaningful to the Input's parent.



        I used this



        const InputField = (
        name,
        placeholder,
        value,
        type,
        onChange,
        error,
        missField
        ) => (
        <div>
        <label className="f-size">name</label>
        <input
        className="input"
        name=name
        placeholder=placeholder
        value=value
        type=type
        onChange=onChange
        />
        <span className="errorMessage">error</span>
        <span className="errorMessage">missField</span>
        </div>
        );


        Parent component:



        class App extends React.Component 
        constructor(props)
        super(props);

        this.handleChange = this.handleChange.bind(this);

        state =
        value: '',
        password: '',
        ;

        handleChange(event)
        this.setState( [event.target.name]: event.target.value );

        render()
        return (
        <div className="App">
        <InputField
        value=this.state.value
        type="number"
        name="value"
        onChange=this.handleChange
        />
        <InputField
        value=this.state.password
        type="password"
        name="password"
        onChange=this.handleChange
        />
        </div>
        );




        Code Sandbox: https://codesandbox.io/s/y4ljv75k9



        Edited to used a stateless component. Not sure if you want state to handle error messages but from your example, this is a valid solution.






        share|improve this answer





























          2














          I personally think you should control this via props, seeing as the value will only be meaningful to the Input's parent.



          I used this



          const InputField = (
          name,
          placeholder,
          value,
          type,
          onChange,
          error,
          missField
          ) => (
          <div>
          <label className="f-size">name</label>
          <input
          className="input"
          name=name
          placeholder=placeholder
          value=value
          type=type
          onChange=onChange
          />
          <span className="errorMessage">error</span>
          <span className="errorMessage">missField</span>
          </div>
          );


          Parent component:



          class App extends React.Component 
          constructor(props)
          super(props);

          this.handleChange = this.handleChange.bind(this);

          state =
          value: '',
          password: '',
          ;

          handleChange(event)
          this.setState( [event.target.name]: event.target.value );

          render()
          return (
          <div className="App">
          <InputField
          value=this.state.value
          type="number"
          name="value"
          onChange=this.handleChange
          />
          <InputField
          value=this.state.password
          type="password"
          name="password"
          onChange=this.handleChange
          />
          </div>
          );




          Code Sandbox: https://codesandbox.io/s/y4ljv75k9



          Edited to used a stateless component. Not sure if you want state to handle error messages but from your example, this is a valid solution.






          share|improve this answer



























            2












            2








            2







            I personally think you should control this via props, seeing as the value will only be meaningful to the Input's parent.



            I used this



            const InputField = (
            name,
            placeholder,
            value,
            type,
            onChange,
            error,
            missField
            ) => (
            <div>
            <label className="f-size">name</label>
            <input
            className="input"
            name=name
            placeholder=placeholder
            value=value
            type=type
            onChange=onChange
            />
            <span className="errorMessage">error</span>
            <span className="errorMessage">missField</span>
            </div>
            );


            Parent component:



            class App extends React.Component 
            constructor(props)
            super(props);

            this.handleChange = this.handleChange.bind(this);

            state =
            value: '',
            password: '',
            ;

            handleChange(event)
            this.setState( [event.target.name]: event.target.value );

            render()
            return (
            <div className="App">
            <InputField
            value=this.state.value
            type="number"
            name="value"
            onChange=this.handleChange
            />
            <InputField
            value=this.state.password
            type="password"
            name="password"
            onChange=this.handleChange
            />
            </div>
            );




            Code Sandbox: https://codesandbox.io/s/y4ljv75k9



            Edited to used a stateless component. Not sure if you want state to handle error messages but from your example, this is a valid solution.






            share|improve this answer















            I personally think you should control this via props, seeing as the value will only be meaningful to the Input's parent.



            I used this



            const InputField = (
            name,
            placeholder,
            value,
            type,
            onChange,
            error,
            missField
            ) => (
            <div>
            <label className="f-size">name</label>
            <input
            className="input"
            name=name
            placeholder=placeholder
            value=value
            type=type
            onChange=onChange
            />
            <span className="errorMessage">error</span>
            <span className="errorMessage">missField</span>
            </div>
            );


            Parent component:



            class App extends React.Component 
            constructor(props)
            super(props);

            this.handleChange = this.handleChange.bind(this);

            state =
            value: '',
            password: '',
            ;

            handleChange(event)
            this.setState( [event.target.name]: event.target.value );

            render()
            return (
            <div className="App">
            <InputField
            value=this.state.value
            type="number"
            name="value"
            onChange=this.handleChange
            />
            <InputField
            value=this.state.password
            type="password"
            name="password"
            onChange=this.handleChange
            />
            </div>
            );




            Code Sandbox: https://codesandbox.io/s/y4ljv75k9



            Edited to used a stateless component. Not sure if you want state to handle error messages but from your example, this is a valid solution.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 13:05

























            answered Nov 13 '18 at 12:54









            CharlieCharlie

            1999




            1999























                1














                <InputField type="text" />
                <InputField type="password" />

                <input
                className='input'
                name=this.state.name
                placeholder=this.state.name
                value=this.props.value
                type=this.props.type
                onChange=this.props.onChange
                />


                I would use props to change the type and manage the component.
                You could then control the component from a form definition






                share|improve this answer



























                  1














                  <InputField type="text" />
                  <InputField type="password" />

                  <input
                  className='input'
                  name=this.state.name
                  placeholder=this.state.name
                  value=this.props.value
                  type=this.props.type
                  onChange=this.props.onChange
                  />


                  I would use props to change the type and manage the component.
                  You could then control the component from a form definition






                  share|improve this answer

























                    1












                    1








                    1







                    <InputField type="text" />
                    <InputField type="password" />

                    <input
                    className='input'
                    name=this.state.name
                    placeholder=this.state.name
                    value=this.props.value
                    type=this.props.type
                    onChange=this.props.onChange
                    />


                    I would use props to change the type and manage the component.
                    You could then control the component from a form definition






                    share|improve this answer













                    <InputField type="text" />
                    <InputField type="password" />

                    <input
                    className='input'
                    name=this.state.name
                    placeholder=this.state.name
                    value=this.props.value
                    type=this.props.type
                    onChange=this.props.onChange
                    />


                    I would use props to change the type and manage the component.
                    You could then control the component from a form definition







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 '18 at 12:51









                    Chris ParsonageChris Parsonage

                    735




                    735





















                        0














                        You should use props not state, so you can pass



                        <InputType type="text" />
                        <InputType type="password" />
                        <InputType type="number" />


                        and for the other params you can use props also.






                        share|improve this answer



























                          0














                          You should use props not state, so you can pass



                          <InputType type="text" />
                          <InputType type="password" />
                          <InputType type="number" />


                          and for the other params you can use props also.






                          share|improve this answer

























                            0












                            0








                            0







                            You should use props not state, so you can pass



                            <InputType type="text" />
                            <InputType type="password" />
                            <InputType type="number" />


                            and for the other params you can use props also.






                            share|improve this answer













                            You should use props not state, so you can pass



                            <InputType type="text" />
                            <InputType type="password" />
                            <InputType type="number" />


                            and for the other params you can use props also.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 '18 at 12:51









                            Gurdeep SinghGurdeep Singh

                            6111




                            6111





















                                0














                                You could use this.props.type but the standard jsx input component is already dynamic as you can see from my example below :






                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>





                                Is there a reason why you would like to use a custom input component?






                                share|improve this answer























                                • The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                  – RCohen
                                  Nov 13 '18 at 13:28















                                0














                                You could use this.props.type but the standard jsx input component is already dynamic as you can see from my example below :






                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>





                                Is there a reason why you would like to use a custom input component?






                                share|improve this answer























                                • The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                  – RCohen
                                  Nov 13 '18 at 13:28













                                0












                                0








                                0







                                You could use this.props.type but the standard jsx input component is already dynamic as you can see from my example below :






                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>





                                Is there a reason why you would like to use a custom input component?






                                share|improve this answer













                                You could use this.props.type but the standard jsx input component is already dynamic as you can see from my example below :






                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>





                                Is there a reason why you would like to use a custom input component?






                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>





                                var root = document.getElementById('root');
                                class InputField extends React.Component
                                render()
                                return (
                                <div>
                                <input type=this.props.type />
                                </div>
                                )



                                class App extends React.Component
                                render()
                                return (
                                <div>
                                <input type='date' />
                                <InputField type='password'/>
                                </div>
                                )



                                ReactDOM.render(<App />, root)

                                <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>
                                <div id='root'></div>






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 13 '18 at 13:24









                                Lokman BoukhouldaLokman Boukhoulda

                                294




                                294












                                • The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                  – RCohen
                                  Nov 13 '18 at 13:28

















                                • The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                  – RCohen
                                  Nov 13 '18 at 13:28
















                                The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                – RCohen
                                Nov 13 '18 at 13:28





                                The idea was to build a small react components library, so I can build forms without changing the props in the parent component.

                                – RCohen
                                Nov 13 '18 at 13:28

















                                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%2f53281125%2fhow-to-create-a-dynamic-input-type-component%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







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto