Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Archnet #60 - Drag drop handle #35

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions src/semantic-ui/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,25 @@ class DataTable extends Component<Props, State> {
*/
onDrag(dragIndex: number, hoverIndex: number) {
this.setState((state) => {
const columns = [...state.columns];
const column = columns[dragIndex];
const columns = [];
const anchoredColumns = [];

// Preserve the index of any unlabeled columns
_.each(state.columns, (column, index) => {
if (column.label && column.label.length) {
columns.push(column);
} else {
anchoredColumns.push({ index, column });
}
});

const column = columns[dragIndex];
columns.splice(dragIndex, 1);
columns.splice(hoverIndex, 0, column);

// Add the unlabeled columns back in
_.each(anchoredColumns, (c) => columns.splice(c.index, 0, c.column));

return { columns };
});
}
Expand Down Expand Up @@ -514,25 +527,27 @@ class DataTable extends Component<Props, State> {
simple
>
<Dropdown.Menu>
{ this.state.columns.map((c, index) => (
<Draggable
id={c.name}
index={index}
key={c.name}
onDrag={this.onDrag.bind(this)}
>
<Dropdown.Item>
<Icon
name='bars'
/>
<Checkbox
checked={!c.hidden}
label={c.label}
onClick={this.onColumnCheckbox.bind(this, c)}
/>
</Dropdown.Item>
</Draggable>
))}
{ this.state.columns
.filter((c) => c.label && c.label.length)
.map((c, index) => (
<Draggable
id={c.name}
index={index}
key={c.name}
onDrag={this.onDrag.bind(this)}
>
<Dropdown.Item>
<Icon
name='bars'
/>
<Checkbox
checked={!c.hidden}
label={c.label}
onClick={this.onColumnCheckbox.bind(this, c)}
/>
</Dropdown.Item>
</Draggable>
))}
</Dropdown.Menu>
</Dropdown>
);
Expand Down
12 changes: 10 additions & 2 deletions stories/components/semantic-ui/EmbeddedList.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from 'react';
import { withA11y } from '@storybook/addon-a11y';
import { action } from '@storybook/addon-actions';
import { withKnobs, optionsKnob as options } from '@storybook/addon-knobs';
import { Button, Container } from 'semantic-ui-react';
import { Button, Container, Icon } from 'semantic-ui-react';
import AddModal from '../AddModal';
import Colors from '../../services/Colors';
import EmbeddedList from '../../../src/semantic-ui/EmbeddedList';
Expand Down Expand Up @@ -169,7 +169,15 @@ export const DragAndDropRows = useDragDrop(() => {
<EmbeddedList
actions={actions}
onDelete={action('delete')}
columns={columns}
columns={[{
name: 'drag-drop',
label: '',
render: () => (
<div
style={{ textAlign: 'center'}}>
<Icon name='bars' />
</div>)
}, ...columns]}
items={list}
onDrag={(dragIndex, hoverIndex) => {
const temp = [...list];
Expand Down