gpt4 book ai didi

javascript - 使用 ReactJs 和 react-dnd 拖放可排序列表时出现问题

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:30 27 4
gpt4 key购买 nike

使用ReactJsreact-dnd

我希望用户能够对表单字段进行排序(又名 properties)

我设置的代码几乎与 source code 相同对于 simple sort demo 中的卡片.没有控制台警告或错误,我不明白为什么这行不通。我不能拖放任何东西。

它的样子:

Example dummy-seeded form

代码:

应用程序.js

import EditForm from './Forms/EditForm.js';

var id = $('#form').data('id');
var source = `/api/forms/${id}?include=type,properties.type`;

React.render(
<EditForm source={source} />,
document.getElementById('form')
);

EditForm.js

import React from 'react/addons';
import update from 'react/lib/update';
import Property from './Property.js';

var EditForm = React.createClass({

mixins: [ React.addons.LinkedStateMixin ],

getInitialState: function() {
return {
id: null,
name: null,
slug: null,
properties: []
}
},

componentDidMount: function() {
this.getFormFromServer();
},

getFormFromServer: function () {
$.get(this.props.source, (result) => {
if (this.isMounted()) {
this.setState({
id: result.id,
name: result.name,
slug: result.slug,
properties: result.properties.data
});
}
});
},

moveProperty: function(id, afterId) {
const { properties } = this.state;

const property = properties.filter(p => p.id === id)[0];
const afterProperty = properties.filter(p => p.id === afterId)[0];
const propertyIndex = properties.indexOf(property);
const afterIndex = properties.indexOf(afterProperty);

this.setState(update(this.state, {
properties: {
$splice: [
[propertyIndex, 1],
[afterIndex, 0, property]
]
}
}));
},

render: function() {
const { properties } = this.state;

var propertiesList = properties.map((property, i) => {
return (
<Property
key={property.id}
id={property.id}
type={property.type.name}
name={property.name}
moveProperty={this.moveProperty} />
);
});

return (
<div>
<h1>Form</h1>
<form>
<div className="form-group">
<label>Name:</label>
<input type="text" name="name" valueLink={this.linkState('name')} className="form-control" />
</div>
<div className="form-group">
<label>Properties:</label>
<div className="list-group properties-list">
{propertiesList}
</div>
</div>
</form>
</div>
);
}

});

export default EditForm;

属性.js

import React, { PropTypes } from 'react/addons';
import { DragDropMixin } from 'react-dnd';
import ItemTypes from './ItemTypes';

const dragSource = {
beginDrag(component) {
return {
item: {
id: component.props.id
}
};
}
};

const dropTarget = {
over(component, item) {
component.props.moveProperty(item.id, component.props.id);
}
};

var Property = React.createClass({

mixins: [ React.addons.LinkedStateMixin, DragDropMixin ],

propTypes: {
id: PropTypes.any.isRequired,
type: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
moveProperty: PropTypes.func.isRequired
},

statics: {
configureDragDrop(register) {
register(ItemTypes.PROPERTY, {
dragSource,
dropTarget
});
}
},

render: function () {
const { type } = this.props;
const { name } = this.props;
const { isDragging } = this.getDragState(ItemTypes.PROPERTY);
const opacity = isDragging ? 0 : 1;

return (
<a className="list-group-item"
{...this.dragSourceFor(ItemTypes.PROPERTY)}
{...this.dropTargetFor(ItemTypes.PROPERTY)}>
{type}: {name}
</a>
);
}
});

export default Property;

ItemTypes.js

module.exports = {
PROPERTY: 'property'
};

如果有人能提供帮助,我将不胜感激。我花了多少时间试图解决这个问题,这有点令人难过。

引用链接:

最佳答案

在花了一天多的时间尝试让拖放工作正常后,我用 one single line of code 修复了它.

import React from 'react/addons';

如果没有它,它是如何编译和渲染的,我什至不知道。

关于javascript - 使用 ReactJs 和 react-dnd 拖放可排序列表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29684672/

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