gpt4 book ai didi

javascript - 动态添加表单域——ReactJs

转载 作者:行者123 更新时间:2023-11-29 15:11:35 24 4
gpt4 key购买 nike

我的要求是动态添加表单域。单击+添加标题按钮,我可以动态添加表单组。

在该表单组中,我有一个按钮+添加内容,单击该按钮应将新的输入元素添加到组中。

我能够相应地修改 JSON 并记录相同的内容并更新状态。但我的 View 未按预期呈现。

我们将不胜感激任何帮助。

堆栈 Blitz :https://stackblitz.com/edit/react-utjwsu?embed=1&file=index.js

/相同的代码/

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Form, FormGroup, Label, Progress,Button } from 'reactstrap';
import Hello from './Hello';
import './style.css';


const byPropKey = (propertyName, value) => () => ({
[propertyName]: value,
});
class App extends Component {
constructor(props) {
super(props);
this.state = {
name: 'React',
Information:'',
ImageUrl:'',
isUploading: false,
isUploaded: true,
showProgress: false,
progress: 0,
avatarUrl:'',
ref:'WhitePapersItems',
formValid: false,
content: [{Heading: "", Content: [{value:""}]}]
};

this.AddContentBox = this.AddContentBox.bind(this);
this.handleChange = this.handleChange.bind(this);
this.AddContentInput = this.AddContentInput.bind(this);
}

AddContentBox(){
return this.state.content.map((element,i)=>(
<div className="header-content" key={i} >
<div className="heading-content-wrapper">
<FormGroup>
<Label className="set-label-pos upload-img" for="Heading">Heading</Label>
<input className="form-control" onChange={this.handleChange.bind(this, i)}
type="text"/>
</FormGroup>
<FormGroup>
<Label className="set-label-pos upload-img" for="Heading">Content</Label>
<input className="form-control" onChange={this.handleChange.bind(this, i)}
type="text"/>
</FormGroup>
{/* {this.AddContentInput} */}
<FormGroup>
<Button color="success" onClick={this.AddContentInput.bind(this,i)}>+ Add Content</Button>
</FormGroup>
</div>
</div>
))
}

handleChange(i, e) {
const { Heading, value } = e.target;
let content = [...this.state.content];
content[i] = {...content[i], [Heading]: value};
this.setState({ content });
}

AddContentInput(index, val){
console.log('Add ContentInput triggered',val,index);
const contents = this.state.content;
contents[index].Content.push({value:''});
this.setState({content:contents});
const item = this.state.content[index].Content;
console.log('item', item)
return item.map((element, i)=>(
<FormGroup>
<Label className="set-label-pos upload-img" for="Heading">Content</Label>
<input className="form-control" type="text"/>
</FormGroup>
))

// console.log(this.state)
// this.addContentField(index);
}

addHeading(event){
event.preventDefault();
this.setState(prevState => ({
content: [...prevState.content, { Heading: "", Content: [{value:""}] }]
}))
this.AddContentBox();
}

render() {
return (
<div>


<Form onSubmit={this.onSubmit}>

<FormGroup>
<Label className="set-label-pos upload-img" for="Information">Information</Label>
<textarea className="form-control" rows="10" onChange={event => this.setState(byPropKey('Information', event.target.value))}
type="text"/>
</FormGroup>
{this.AddContentBox()}
<div className="add-heading-button">
<Button color="primary" onClick={this.addHeading.bind(this)}>+ Add Heading</Button>
</div>
<FormGroup>
<input type="hidden" value={this.state.ImageUrl} onChange={event => this.setState(byPropKey('ImageUrl', event.target.value))}/>
</FormGroup>

<Button className="btn btn-danger" size="lg" type="submit">+ Add White Papers Item</Button>
</Form>

</div>
);
}
}

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

最佳答案

为可能遇到此类情况的人回答我自己的问题。

更新我的 stackblitz:https://stackblitz.com/edit/react-utjwsu?embed=1&file=index.js

我试图只渲染一个内容字段,而不是渲染所有内容字段。

/代码在这里/

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Form, FormGroup, Label, Progress,Button } from 'reactstrap';
import Hello from './Hello';
import './style.css';


const byPropKey = (propertyName, value) => () => ({
[propertyName]: value,
});
class App extends Component {
constructor(props) {
super(props);
this.state = {
name: 'React',
Information:'',
ImageUrl:'',
isUploading: false,
isUploaded: true,
showProgress: false,
progress: 0,
avatarUrl:'',
ref:'WhitePapersItems',
formValid: false,
content: [{Heading: "", Content: [{value:""}]}]
};

this.AddContentBox = this.AddContentBox.bind(this);
this.AddContentInput = this.AddContentInput.bind(this);
//this.addContentTextBox = this.addContentTextBox.bind(this);
}

AddContentInputFields(element) {
return element.Content.map(content => {
return (
<FormGroup>
<Label className="set-label-pos upload-img" for="Heading">Content</Label>
<input className="form-control" type="text"/>
</FormGroup>
);
})
}

AddContentBox(){
return this.state.content.map((element,i)=>(
<div className="header-content" key={i} >
<div className="heading-content-wrapper">
<FormGroup>
<Label className="set-label-pos upload-img" for="Heading">Heading</Label>
<input className="form-control" type="text"/>
</FormGroup>
{ this.AddContentInputFields(element) }
<FormGroup>
<Button color="success" onClick={this.AddContentInput.bind(this,i)}>+ Add Content</Button>
</FormGroup>
</div>
</div>
))
}

handleChange(i, e) {
const { Heading, value } = e.target;
let content = [...this.state.content];
content[i] = {...content[i], [Heading]: value};
this.setState({ content });
}

AddContentInput(index, val){
console.log('Add ContentInput triggered',val,index);
const contents = _.cloneDeep(this.state.content);
contents[index].Content.push({value:''});

const item = contents[index].Content;
console.log('item', item);
this.setState({content:contents});
//this.addContentTextBox(item);

}

// addContentTextBox(item){
// return item.map((element, i)=>(
// <FormGroup>
// <Label className="set-label-pos upload-img" for="Heading">Content</Label>
// <input className="form-control" type="text"/>
// </FormGroup>
// ));
// }

addHeading(event){
event.preventDefault();
this.setState(prevState => ({
content: [...prevState.content, { Heading: "", Content: [{value:""}] }]
}))
this.AddContentBox();
}

render() {
return (
<div>


<Form onSubmit={this.onSubmit}>

<FormGroup>
<Label className="set-label-pos upload-img" for="Information">Information</Label>
<textarea className="form-control" rows="10" onChange={event => this.setState(byPropKey('Information', event.target.value))}
type="text"/>
</FormGroup>
{this.AddContentBox()}
<div className="add-heading-button">
<Button color="primary" onClick={this.addHeading.bind(this)}>+ Add Heading</Button>
</div>
<FormGroup>
<input type="hidden" value={this.state.ImageUrl} onChange={event => this.setState(byPropKey('ImageUrl', event.target.value))}/>
</FormGroup>

<Button className="btn btn-danger" size="lg" type="submit">+ Add White Papers Item</Button>
</Form>

</div>
);
}
}

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

关于javascript - 动态添加表单域——ReactJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53976924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com