react-sortable-hoc#arrayMove JavaScript Examples

The following examples show how to use react-sortable-hoc#arrayMove. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Roster.js    From zengm-legacy with Apache License 2.0 6 votes vote down vote up
async handleReorderDrag({oldIndex, newIndex}) {
        const pids = this.props.players.map((p) => p.pid);
        const sortedPids = arrayMove(pids, oldIndex, newIndex);
        this.setState({
            sortedPids,
        });
        await toWorker('reorderRosterDrag', sortedPids);
        realtimeUpdate(['playerMovement']);
    }
Example #2
Source File: BuildPageModal.js    From ice-electron with MIT License 4 votes vote down vote up
BuildPageModal = ({
  on, onCancel, onOk, page, intl,
}) => {
  const { blocks = [] } = page || {};
  const {
    on: onSaveModal,
    setModal: setSaveModal,
  } = useModal();
  const [selectedBlocks, setSelectedBlocks] = useState([]);
  const [isSorting, setIsSorting] = useState(false);
  const [materialSources, setMaterialSources] = useState([]);
  const [progress, material, project] = stores.useStores(['progress', 'material', 'project']);
  useEffect(() => {
    material.getResources({ type: project.dataSource.type });
  }, []);
  const { dataSource } = material;
  const { resource } = dataSource;
  const newMaterialSources = resource.official
    .concat(resource.custom)
    .filter(item => item.type === project.dataSource.type);
  if (newMaterialSources.length !== materialSources.length) {
    setMaterialSources(newMaterialSources);
  }

  function onCloseSaveModal() {
    setSaveModal(false);
  }

  async function onCreateOk() {
    // If has page, it is represented in edit mode
    if (page) {
      try {
        await onOk(selectedBlocks);
      } catch (error) {
        showMessage(error);
      }
    } else {
      setSaveModal(true);
    }
  }

  async function onSaveOk(data) {
    try {
      await onOk({
        blocks: selectedBlocks,
        ...data,
      });
    } catch (error) {
      showMessage(error);
    }
    
    setSaveModal(false);
  }

  function generateBlockName(value, count = 0) {
    const setName = upperCamelCase(value);
    const newName = !count ? setName : `${setName}${count}`;
    const isConflict = blocks.concat(selectedBlocks).some(({ name }) => upperCamelCase(name) === newName);
    if (isConflict) {
      return generateBlockName(setName, count + 1);
    }
    return newName;
  }

  function onSelect(block) {
    setSelectedBlocks(selectedBlocks.concat([{ ...block, name: generateBlockName(block.name) }]));
  }

  function onDelete(targetIndex) {
    setSelectedBlocks(selectedBlocks.filter((block, index) => index !== targetIndex));
  }

  function onNameChange(name, targetIndex) {
    selectedBlocks[targetIndex].name = name;
    setSelectedBlocks([...selectedBlocks]);
  }

  function onSortStart() {
    setIsSorting(true);
  }

  function onSortEnd({ oldIndex, newIndex }) {
    setIsSorting(false);
    setSelectedBlocks([...arrayMove(selectedBlocks, oldIndex, newIndex)]);
  }

  useSocket('adapter.page.create.status', ({ text, percent }) => {
    progress.show({ statusText: text, percent });
  });

  useEffect(() => {
    material.getResources({ type: project.dataSource.type });
  }, []);

  const titleId = page ? 'iceworks.project.panel.page.update.title' : 'iceworks.project.panel.page.create.title';

  return (
    [
      <Modal
        title={<FormattedMessage id={titleId} />}
        visible={on}
        onCancel={onCancel}
        onOk={onCreateOk}
        key="createModal"
      >
        <div className={styles.wrap}>
          <div className={styles.main}>
            <div className={styles.existed}>
              {
                blocks.map(({ name }, index) => {
                  return (
                    <div key={index} className={styles.item}>
                      {name}
                    </div>
                  );
                })
              }
            </div>
            <SelectedBlocks
              helperClass={styles.blockIsDraging}
              blocks={selectedBlocks}
              onDelete={onDelete}
              onNameChange={onNameChange}
              useDragHandle
              lockAxis="y"
              onSortStart={onSortStart}
              onSortEnd={onSortEnd}
              isSorting={isSorting}
            />
          </div>
          <div className={styles.material}>
            {
              !material.getResources.error && materialSources.length ?
                <MaterialSelect
                  resources={materialSources}
                  onSelect={onSelect}
                /> :
                null
            }
            <ActionStatus
              store={stores}
              config={[
                {
                  storeName: 'material',
                  actions: [
                    {
                      actionName: 'getResources',
                      showLoading: true,
                      showError: true,
                    },
                  ],
                },
              ]}
            />
          </div>
        </div>
      </Modal>,
      <SavePageModal
        on={onSaveModal}
        onCancel={onCloseSaveModal}
        onOk={onSaveOk}
        intl={intl}
        key="saveModal"
      />,
    ]
  );
}