How to create a dynamic input type component
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
add a comment |
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
How do you want to configure thetype
? You could usethis.props.type
instead and let the user ofInputField
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
add a comment |
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
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
javascript reactjs react-component
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 thetype
? You could usethis.props.type
instead and let the user ofInputField
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
add a comment |
How do you want to configure thetype
? You could usethis.props.type
instead and let the user ofInputField
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
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
<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
add a comment |
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.
add a comment |
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?
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 13 '18 at 13:05
answered Nov 13 '18 at 12:54
CharlieCharlie
1999
1999
add a comment |
add a comment |
<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
add a comment |
<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
add a comment |
<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
<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
answered Nov 13 '18 at 12:51
Chris ParsonageChris Parsonage
735
735
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 12:51
Gurdeep SinghGurdeep Singh
6111
6111
add a comment |
add a comment |
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?
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
add a comment |
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?
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
add a comment |
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?
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>
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 do you want to configure the
type
? You could usethis.props.type
instead and let the user ofInputField
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