Skip to content
This repository was archived by the owner on Nov 22, 2022. It is now read-only.

Commit 6cdcce3

Browse files
Port loop sample widget
1 parent 0da73f3 commit 6cdcce3

File tree

2 files changed

+115
-3
lines changed
  • packages/builder/src/components/ComponentOptions/components/Content/Loop

2 files changed

+115
-3
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import React from 'react'
2+
3+
import { Field, useFormikContext } from 'formik'
4+
import { FormGroup, Label, Col, InputGroup } from 'reactstrap'
5+
6+
import Hint from '../../../../../Hint'
7+
import { Input } from '../../../../../Form'
8+
import { integerOrPlaceholder } from '../../../Behavior/components/Timeline/util'
9+
10+
const switchLabels = ({
11+
templateParameters={ rows: [] },
12+
sample={ n: undefined }
13+
}, labels) => {
14+
const samples = parseInt(sample.n)
15+
const parameters = templateParameters.rows.length
16+
17+
if (samples < parameters) {
18+
return labels[0]
19+
} else if (isNaN(samples) || samples === parameters) {
20+
return labels[1]
21+
} else {
22+
return labels[2]
23+
}
24+
}
25+
26+
export default () => {
27+
const { values } = useFormikContext()
28+
29+
return <FormGroup row>
30+
<Label xs={ 2 } for="sampleN">
31+
Sample
32+
<Hint
33+
title="Sampling"
34+
className="float-right"
35+
placement="left"
36+
>
37+
<p className="font-weight-bold">
38+
Control how many, and which of the above parameter sets are used.
39+
</p>
40+
<p className="text-muted">
41+
The <strong>sample size</strong> defines the number of loop iterations. The <strong>mode</strong> determines how the iterations are drawn from the entries above.
42+
</p>
43+
<dl className="text-muted">
44+
<dt>Sequential</dt>
45+
<dd>Proceed in order, starting from the top. If more samples are requested than entries are available, start over.</dd>
46+
47+
<dt>Random order</dt>
48+
<dd>Present entries in shuffled order<br /> (only available without sampling).</dd>
49+
50+
<dt>Sample without replacement</dt>
51+
<dd>Draw entries at random, starting over when all are exhausted. Shuffles the result for a completely random order.</dd>
52+
53+
<dt>Sample w/o replacement (in blocks)</dt>
54+
<dd>As above, but without shuffling.<br /> If the number of samples exceeds the number of entries, this results in blocks of all entries in random order.</dd>
55+
56+
<dt>Sample with replacement</dt>
57+
<dd>Draw entries without regard to which have been selected before.</dd>
58+
</dl>
59+
</Hint>
60+
</Label>
61+
<Col xs={10}>
62+
<InputGroup>
63+
<Field
64+
name="sample.n"
65+
component={ Input }
66+
pattern={ integerOrPlaceholder }
67+
placeholder={
68+
(values.sample || {}).mode === 'draw-replace'
69+
? 'As many as rows above'
70+
: 'Use all'
71+
}
72+
className="text-monospace"
73+
/>
74+
<Field
75+
name={ 'sample.mode' }
76+
as="select"
77+
className="form-control custom-select text-monospace"
78+
>
79+
<option value="sequential">
80+
In sequence
81+
</option>
82+
<option value="draw-shuffle">
83+
{
84+
switchLabels(values, [
85+
'Sampled without replacement',
86+
'In random order',
87+
'Sampled without replacement (then shuffled)'
88+
])
89+
}
90+
</option>
91+
<option value="draw">
92+
Sampled without replacement (in blocks)
93+
</option>
94+
<option value="draw-replace">
95+
Sampled with replacement
96+
</option>
97+
</Field>
98+
</InputGroup>
99+
</Col>
100+
</FormGroup>
101+
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
import React from 'react'
22

3+
import { CardBody } from 'reactstrap'
4+
35
import Card from '../../../../Card'
6+
import Form from '../../Form'
7+
8+
import SampleWidget from './SampleWidget'
49

5-
export default () =>
6-
<>
10+
export default ({ id, data }) =>
11+
<Form
12+
id={ id } data={ data }
13+
keys={ [ 'sample' ] }
14+
>
715
<Card title="Loop" wrapContent={ false }>
16+
<CardBody>
17+
<SampleWidget />
18+
</CardBody>
819
</Card>
920
<Card title="Further options"
1021
/* ... */
1122
>
1223
</Card>
13-
</>
24+
</Form>

0 commit comments

Comments
 (0)