Skip to content

Commit 5f4253c

Browse files
committed
Port forms scenario
1 parent 1114b94 commit 5f4253c

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

src/scenarios/forms/App.jsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
4+
import Form from "./Form";
5+
import * as c from "./constants";
6+
7+
import { typeTextInRandomInput } from "./inputs";
8+
9+
let slices;
10+
11+
const mapState = state => {
12+
if (!slices) {
13+
slices = Object.keys(state).map(key => Number(key));
14+
//slices.sort();
15+
}
16+
17+
return { slices };
18+
};
19+
20+
//const mapDispatch = { typeTextInRandomInput };
21+
22+
async function infiniteBobRoss() {
23+
while (true) {
24+
await typeTextInRandomInput();
25+
}
26+
}
27+
28+
class App extends React.Component {
29+
render() {
30+
return (
31+
<div>
32+
<button onClick={infiniteBobRoss}>Type Text</button>
33+
<div className="row">
34+
{this.props.slices.map((slice, idx) => {
35+
return (
36+
<div style={{ display: "inline-block", minWidth: 70 }} key={idx}>
37+
<Form id={slice} />
38+
</div>
39+
);
40+
})}
41+
</div>
42+
</div>
43+
);
44+
}
45+
}
46+
App.displayName = "App";
47+
48+
export default connect(mapState)(App);

src/scenarios/forms/Form.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
4+
import { updateInput } from "./inputs";
5+
6+
import * as c from "./constants";
7+
8+
const mapState = (state, ownProps) => {
9+
return {
10+
text: state[ownProps.id]
11+
};
12+
};
13+
14+
const mapDispatch = { updateInput };
15+
16+
class Form extends React.Component {
17+
onChange = e => {
18+
this.props.updateInput({ inputId: this.props.id, text: e.target.value });
19+
};
20+
21+
render() {
22+
const { text, id } = this.props;
23+
24+
const fillers = Array.from({
25+
length: c.NUMBER_OF_CHECKBOXES_PER_FORM
26+
}).map((item, i) => <input type="checkbox" key={i} />);
27+
28+
return (
29+
<React.Fragment>
30+
<form style={{ display: "flex", alignItems: "flex-start" }}>
31+
Form {id}:
32+
<textarea id={`input-${id}`} value={text} onChange={this.onChange} />
33+
</form>
34+
<div>{fillers}</div>
35+
</React.Fragment>
36+
);
37+
}
38+
}
39+
40+
export default connect(
41+
mapState,
42+
mapDispatch
43+
)(Form);

src/scenarios/forms/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const NUMBER_OF_INPUTS = 500;
2+
export const NUMBER_OF_CHECKBOXES_PER_FORM = 50;

src/scenarios/forms/index.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useLayoutEffect } from 'react'
2+
import ReactDOM from 'react-dom'
3+
import { configureStore, AnyAction } from '@reduxjs/toolkit'
4+
5+
import { renderApp } from '../../common'
6+
7+
import App from './App'
8+
import * as c from './constants'
9+
10+
import rootReducer, { initialize, typeTextInRandomInput } from './inputs'
11+
12+
const store = configureStore({
13+
reducer: rootReducer,
14+
middleware: (gdm) =>
15+
gdm({
16+
immutabilityCheck: false,
17+
serializableCheck: false,
18+
}),
19+
})
20+
21+
store.dispatch(initialize({ numberOfInputs: c.NUMBER_OF_INPUTS }))
22+
23+
async function infiniteBobRoss() {
24+
while (true) {
25+
await typeTextInRandomInput()
26+
}
27+
}
28+
29+
const FormsApp = () => {
30+
useLayoutEffect(() => {
31+
setTimeout(infiniteBobRoss, 50)
32+
33+
setTimeout(infiniteBobRoss, 70)
34+
35+
setTimeout(infiniteBobRoss, 90)
36+
37+
setTimeout(infiniteBobRoss, 110)
38+
}, [])
39+
40+
return <App />
41+
}
42+
43+
// @ts-ignore
44+
renderApp(FormsApp, store)

src/scenarios/forms/inputs.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createSlice } from '@reduxjs/toolkit'
2+
import * as c from './constants'
3+
import userEvent from '@testing-library/user-event'
4+
5+
const { reducer, actions } = createSlice({
6+
initialState: {},
7+
name: 'inputs',
8+
reducers: {
9+
initialize(state, action) {
10+
const { numberOfInputs } = action.payload
11+
for (let i = 0; i < numberOfInputs; i++) {
12+
state[i] = ''
13+
}
14+
},
15+
updateInput(state, action) {
16+
const { inputId, text } = action.payload
17+
state[inputId] = text
18+
},
19+
},
20+
})
21+
22+
export const { initialize, updateInput } = actions
23+
24+
const BOB_ROSS_IPSUM = `
25+
Little short strokes. And I know you're saying, 'Oh Bob, you've done it this
26+
time.' And you may be right. You can do anything here. So don't worry about it.
27+
Even the worst thing we can do here is good. Absolutely no pressure. You are
28+
just a whisper floating across a mountain. Isn't that fantastic that you can
29+
create an almighty tree that fast?
30+
`.trim()
31+
32+
export function typeTextInRandomInput() {
33+
const inputId = Math.floor(Math.random() * c.NUMBER_OF_INPUTS)
34+
console.log('Input id: ', inputId)
35+
36+
const input = document.getElementById(`input-${inputId}`)
37+
38+
return userEvent.type(input, BOB_ROSS_IPSUM, { delay: 25 })
39+
}
40+
41+
export default reducer

0 commit comments

Comments
 (0)