我在点击Item
中的按钮后尝试预先填充表单我有能够更新项目的操作,问题是能够预先填充表单正确的项目,以便能够发送该行动。
-----------------
| + List | << Name of the list
-----------------
| SKU: 12345 | << Item
| ITEM: Bananas |
| PRICE: $1.00 |
| |
| <edit> | << Button triggering the modal and form. Need to pre-populate the form onClick
-----------------
/components/List.jsx
export class List extends React.Component {
constructor(props) {
super(props)
this.state = {
isModalOpen: false
}
}
toggleModal () {
this.setState({ isModalOpen: !this.state.isModalOpen })
}
...
render () {
const { list, ...props } = this.props
const listId = list.id
return (
...
<Items
items={props.listItems}
openModal={this.toggleModal.bind(this)}>
</Items>
<Modal
className='list-add-item'
openModal={this.state.isModalOpen}>
<ItemForm
itemActions={this.props.itemActions}
listActions={this.props.listActions}
listId={listId}>
</ItemForm>
</Modal>
</div>
)
}
}
/components/ItemForm.jsx
import React from 'react'
import uuid from 'node-uuid'
class ItemForm extends React.Component {
constructor(props) {
super(props)
this.state = {
isEditing: false
}
}
handleSubmit = (event) => {
event.preventDefault()
let sku = this.refs.sku.value.trim()
let text = this.refs.text.value.trim()
let price = this.refs.price.value.trim()
const item = { id: uuid.v4(), sku, text, price }
// don't do anything if any field is left blank
if (!item.sku || !item.text || !item.price) { return }
this.props.itemActions.createItem(item)
this.props.listActions.connectToList(this.props.listId, item.id)
// reset the form after submission
this.refs.itemForm.reset()
}
render() {
const { ...props } = this.props
return (
<form ref="itemForm" onSubmit={this.handleSubmit}>
<label>SKU</label>
<input
type="text"
placeholder="SKU"
autoFocus={true}
ref='sku'
defaultValue={this.props.sku}
onBlur={this.finishEdit}
onKeyPress={this.checkEnter}></input>
<label>Item</label>
<input
type="text"
placeholder="Item"
autoFocus={true}
ref='text'
defaultValue={this.props.text}
onBlur={this.finishEdit}
onKeyPress={this.checkEnter}></input>
<label>Price</label>
<input
type="text"
placeholder="Price"
autoFocus={true}
ref='price'
defaultValue={this.props.price}
onBlur={this.finishEdit}
onKeyPress={this.checkEnter}></input>
<button type="submit">{ this.state.isEditing ? 'Edit Item' : 'Add Item' }</button>
</form>
)
}
}
export default ItemForm
/components/Items.jsx
export default class Items extends React.Component {
render () {
const {items, onEdit, ...props} = this.props
return (
<ul className='items'>{items.map((item) =>
<Item
className='item'
key={item.id}
id={item.id}
sku={item.sku}
text={item.text}
price={item.price}
openModal={this.props.openModal}>
</Item>
)}</ul>
)
}
}
/components/Item.jsx
export default class Item extends React.Component {
render () {
const { openModal, ...props } = this.props
return (
<span>
<li>SKU: {this.props.sku}</li>
<li>ITEM: {this.props.text}</li>
<li>PRICE: {this.props.price}</li>
<button onClick={this.props.openModal}>Edit Item</button>
</span>
)
}
}
答案 0 :(得分:0)
有不同的方法可以做到这一点。例如,您可以按照以下步骤操作:
selectedItem
作为道具传递给itemForm
。创建将更新该状态的方法selectItem
。将此selectItem
方法作为道具传递给您的组件/项目。请记住bind
它到组件。onClick
处理程序添加,selectItem
作为道具传递给组件。constructor
创建一个状态,其中包含在props中传递的项目的信息。添加willReceiveProps
并在每次收到一些可能是新选择项目的更新道具时更新组件的内部状态。我希望您发现这有用并解决您的问题。
答案 1 :(得分:0)
我最终使用了redux-form,我的应用程序设置的方式使我很难解决我遇到的问题。工作版本为here