Skip to content

Commit dd3151f

Browse files
author
Clauderic Demers
committed
docs: add stress test story for interactive elements
1 parent d64c8cf commit dd3151f

File tree

6 files changed

+168
-0
lines changed

6 files changed

+168
-0
lines changed

src/.stories/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import random from 'lodash/random';
1515
import classNames from 'classnames';
1616

1717
import GroupedItems from './grouping-items';
18+
import InteractiveElements from './interactive-elements-stress-test';
1819

1920
function getItems(count, height) {
2021
var heights = [65, 110, 140, 65, 90, 65];
@@ -753,3 +754,12 @@ storiesOf('Advanced examples | Re-rendering before sorting', module)
753754
</div>
754755
);
755756
});
757+
758+
storiesOf('Stress Testing | Nested elements', module).add(
759+
'Interactive elements',
760+
() => (
761+
<div className={style.root}>
762+
<InteractiveElements />
763+
</div>
764+
),
765+
);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import {sortableElement} from '../../../../src';
3+
4+
import styles from './Item.scss';
5+
6+
function Item(props) {
7+
const {children} = props;
8+
9+
return <div className={styles.root}>{children}</div>;
10+
}
11+
12+
export default sortableElement(Item);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
$color: #333;
2+
$white: #fff;
3+
$backgroundColor: $white;
4+
5+
$boxShadow: 0 5px 5px -5px rgba(0, 0, 0, 0.2);
6+
7+
$fontWeight-regular: 400;
8+
$fontWeight-bold: 600;
9+
10+
$borderRadius: 3px;
11+
$borderWidth: 1px;
12+
$borderColor: #efefef;
13+
14+
.root {
15+
display: block;
16+
width: 250px;
17+
padding: 20px;
18+
background-color: $backgroundColor;
19+
border-bottom: $borderWidth solid #efefef;
20+
box-sizing: border-box;
21+
user-select: none;
22+
23+
color: $color;
24+
font-family: sans-serif;
25+
font-weight: $fontWeight-regular;
26+
27+
> * {
28+
display: block;
29+
width: 100%;
30+
font-size: 14px;
31+
}
32+
33+
> input,
34+
> textarea {
35+
padding: 5px;
36+
border: 1px solid #e0e0e0;
37+
box-sizing: border-box;
38+
}
39+
40+
label {
41+
input {
42+
margin-right: 0.5em;
43+
}
44+
}
45+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Item from './Item';
2+
3+
export default Item;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import {sortableContainer} from '../../../src';
3+
4+
import Item from './Item';
5+
6+
function List({items}) {
7+
return (
8+
<div>
9+
{items.map(([key, children], index) => {
10+
return (
11+
<Item key={key} index={index}>
12+
{children}
13+
</Item>
14+
);
15+
})}
16+
</div>
17+
);
18+
}
19+
20+
export default sortableContainer(List);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import React from 'react';
2+
import arrayMove from 'array-move';
3+
4+
import SortableList from './List';
5+
6+
const items = {
7+
input: <input placeholder="Regular text input" />,
8+
textarea: <textarea placeholder="Textarea input" />,
9+
select: (
10+
<select>
11+
<option>Option 1</option>
12+
<option>Option 2</option>
13+
<option>Option 3</option>
14+
</select>
15+
),
16+
checkbox: (
17+
<>
18+
<label>
19+
<input type="checkbox" name="checkbox" />
20+
Checkbox 1
21+
</label>
22+
<label>
23+
<input type="checkbox" name="checkbox" />
24+
Checkbox 2
25+
</label>
26+
</>
27+
),
28+
radio: (
29+
<>
30+
<label>
31+
<input type="radio" name="option" />
32+
Option 1
33+
</label>
34+
<label>
35+
<input type="radio" name="option" />
36+
Option 2
37+
</label>
38+
</>
39+
),
40+
range: <input type="range" min="1" max="100" />,
41+
contentEditable: (
42+
<div
43+
contentEditable
44+
dangerouslySetInnerHTML={{
45+
__html: 'Lorem ipsum <strong>dolor sit</strong> amet',
46+
}}
47+
/>
48+
),
49+
};
50+
51+
export default class InteractiveElements extends React.Component {
52+
state = {
53+
items: Object.entries(items),
54+
};
55+
56+
render() {
57+
return (
58+
<SortableList
59+
// The distance prop isn't strictly required for this example, but it is recommended
60+
// to set it to a low value for sortable items with nested interactive elements
61+
// such as clickable labels for checkbox / radio inputs
62+
distance={2}
63+
items={this.state.items}
64+
onSortEnd={this.onSortEnd}
65+
/>
66+
);
67+
}
68+
69+
onSortEnd = ({oldIndex, newIndex}) => {
70+
if (oldIndex === newIndex) {
71+
return;
72+
}
73+
74+
this.setState(({items}) => ({
75+
items: arrayMove(items, oldIndex, newIndex),
76+
}));
77+
};
78+
}

0 commit comments

Comments
 (0)