Reputation: 1362
I'm struggling to enter a value whenever a user enters a value when edit is selected, it will not let me enter a value.
I suspect it should be something to do with the onChange
method, I'm not sure what I should do. I think the onChange
method is right.
I read a question similar to this, but doesn't have the solution I'm looking for.
PostList.js
import React, { Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import moment from 'moment';
import {connect} from 'react-redux';
import {DeletePost} from '../actions/';
import PostItem from './PostItem';
const Styles = {
myPaper: {
margin: '20px 0px',
padding: '20px'
}
}
class PostList extends Component{
constructor(props){
super(props);
this.state ={
isEditing:false,
isEditingId:null
}
}
// Return a new function. Otherwise the DeletePost action will be dispatch each
// time the Component rerenders.
removePost = (id) => () => {
this.props.DeletePost(id);
}
// this will be onChange used for the <Editable component/>
onChange = (e) => {
e.preventDefault();
this.setState({
[e.target.title]: e.target.value
})
}
formEditing = (id) => ()=> {
this.setState({
isEditingId: id
});
}
render(){
const {posts, editForm, isEditing, editChange} = this.props;
return (
<div>
{posts.map((post, i) => (
<Paper key={post.id} style={Styles.myPaper}>
{/* {...post} prevents us from writing all of the properties out */}
<PostItem editChange={this.onChange} editForm={this.formEditing} isEditing={this.state.isEditingId === post.id} removePost={this.removePost} {...post} />
</Paper>
))}
</div>
)
}
}
const mapDispatchToProps = (dispatch) => ({
// Pass id to the DeletePost functions.
DeletePost: (id) => dispatch(DeletePost(id))
});
export default connect(null, mapDispatchToProps)(PostList);
Editable.js
import React from 'react';
import Paper from '@material-ui/core/Paper';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import TextField from '@material-ui/core/TextField';
const Editable = (props) => (
<div>
<TextField
id="outlined-name"
label="Title"
style={{width: 560}}
name="title"
value={props.editField}
onChange={props.editChange}
margin="normal"
variant="outlined"/>
</div>
)
export default Editable;
PostItem.js
import React, { Component } from 'react';
import Paper from '@material-ui/core/Paper';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import moment from 'moment';
import Editable from './Editable';
const Styles = {
myPaper: {
margin: '20px 0px',
padding: '20px'
}
}
// editChange will passed in as an argument. So the <Editable/> component can
//use it
const PostItem = ({ title, id, removePost, createdAt, post_content, username, editForm, isEditing, editChange}) => {
return(
<div>
<Typography variant="h6" component="h3">
{/* if else teneray operator */}
{isEditing ? (
<Editable editField={title} editChange={editChange}/>
): (
<div>
{title}
</div>
)}
</Typography>
<Typography component="p">
{post_content}
<h5>
by: {username}</h5>
<Typography color="textSecondary">{moment(createdAt).calendar()}</Typography>
</Typography>
{!isEditing ? (
<Button variant="outlined" type="submit" onClick={editForm(id)}>
Edit
</Button>
):(
<Button variant="outlined" type="submit" onClick={editForm(null)}>
Update
</Button>
)}
<Button
variant="outlined"
color="primary"
type="submit"
onClick={removePost(id)}>
Remove
</Button>
</div>
)
}
export default PostItem;
update()
I want to keep the current title value instead of deleting the value.
Upvotes: 0
Views: 502
Reputation: 138
I think you are not passing down your title state correctly. Add these to your state to initialize to empty:
this.state ={
isEditing:false,
isEditingId:null,
title: ""
}
onChange to this
onChange = (e) => {
e.preventDefault();
this.setState({
title: e.target.value
})
}
PostItem to this
const PostItem = ({ title, id, removePost, createdAt, post_content, username, editForm, isEditing, editChange }) => {
return(
<div>
<Typography variant="h6" component="h3">
{/* if else teneray operator */}
{isEditing ? (
<Editable editField={title} editChange={editChange}/>
): (
<div>
{title}
</div>
)}
</Typography>
<Typography component="p">
{post_content}
<h5>
by: {username}</h5>
<Typography color="textSecondary">{moment(createdAt).calendar()}</Typography>
</Typography>
{!isEditing ? (
<Button variant="outlined" type="submit" onClick={editForm(id)}>
Edit
</Button>
):(
<Button variant="outlined" type="submit" onClick={editForm(null)}>
Update
</Button>
)}
<Button
variant="outlined"
color="primary"
type="submit"
onClick={removePost(id)}>
Remove
</Button>
</div>
)
}
export default PostItem;
Then pass down title to your PostItem as props,
for example
<PostItem title={this.state.title} editChange={this.onChange} editForm={this.formEditing} isEditing={this.state.isEditingId === post.id} removePost={this.removePost} {...post} />
update
change this
<Editable editField={myTitle} editChange={editChange}/>
to(this makes it so the title is appended in the values while you can still edit the value feel free to improve this) it acts a little buggy or wierd but it work.
<Editable editField={title} editChange={editChange}/>
Upvotes: 2