clauderic / react-sortable-hoc

A set of higher-order components to turn any list into an animated, accessible and touch-friendly sortable list✌️
https://clauderic.github.io/react-sortable-hoc/
MIT License
10.82k stars 982 forks source link

Needed Help to Make Redux-Form Field Arrays Sortable #340

Closed navotaras closed 5 years ago

navotaras commented 6 years ago

Hello, I am trying to wire up redux-form array fields ( see example below ) and make it sortable using react-sortable-hoc components. I am not able to make any progress. Need help and guidance.

https://redux-form.com/7.2.3/examples/fieldarrays/

navotaras commented 6 years ago

image

The first one shows the picture of how the array fields are rendering fine. The below picture shows that when I click on something to move everything vanishes. I have given the code below for both my component and store initialization

image

import React, { Component } from 'react'; import { Field, FieldArray, reduxForm } from 'redux-form'; import { SortableContainer, SortableElement, arrayMove } from 'react-sortable-hoc';

const renderField = ({ input, label, type, meta: { touched, error } }) => (

{touched && error && {error}}

);

const SortableItem = SortableElement(({member, fields, itr:index}) => { return (

  • )});

    const AmpedSortableItem = WrappedSortableItemComponent => { return ({ member, fields, itr }) =>

    <WrappedSortableItemComponent {...{ member, fields, itr }} />
    ; };

    export const NewSortableItem = AmpedSortableItem(SortableItem);

    const renderMembers = SortableContainer( ({ fields, onSortEnd, meta: { error, submitFailed } }) => {

        onSortEnd = ({ oldIndex, newIndex }) => {
                arrayMove(fields, oldIndex, newIndex)
        };
    
        return (
        <ul>
            <li>
                <button type="button" onClick={() => fields.push({})}>
                    Add Member
                </button>
                {submitFailed && error && <span>{error}</span>}
            </li>
            {fields.map((member, index, fields) => {
                console.log(member, index, fields);
                return <NewSortableItem key={`item-${index}`} itr={index} fields={fields} member={member} />;
            })}
        </ul>
    )}

    );

    const SortableFieldArraysForm = props => { const { handleSubmit, pristine, reset, submitting } = props; return (

    );

    };

    export default reduxForm({ form: 'fieldArraysSortable' // a unique identifier for this form })(SortableFieldArraysForm);


    import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { Values } from 'redux-form-website-template'; import store from './playground/store'; import showResults from './playground/showResults'; import SortableFieldArraysForm from './playground/sortablelistwithreduxform' import registerServiceWorker from './registerServiceWorker';

    // Playground Examples import { RenderFields, NewRenderFields } from './playground/hocexample'; const rootEl = document.getElementById('root');

    ReactDOM.render(

    , rootEl ); registerServiceWorker();
    mrchief commented 6 years ago

    Maybe you can adapt from here: https://codesandbox.io/s/6zxjpv31pn