Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 758eb21

Browse files
feat: Support booleans for parameters input (#3437)
1 parent f28cd15 commit 758eb21

File tree

3 files changed

+80
-16
lines changed

3 files changed

+80
-16
lines changed

site/src/components/ParameterInput/ParameterInput.stories.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ Basic.args = {
4242
}),
4343
}
4444

45+
export const Boolean = Template.bind({})
46+
Boolean.args = {
47+
schema: createParameterSchema({
48+
name: "disable_docker",
49+
description: "Disable Docker?",
50+
validation_value_type: "bool",
51+
default_source_value: "false",
52+
}),
53+
}
54+
4555
export const Contains = Template.bind({})
4656
Contains.args = {
4757
schema: createParameterSchema({

site/src/components/ParameterInput/ParameterInput.tsx

+58-16
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@ import Radio from "@material-ui/core/Radio"
33
import RadioGroup from "@material-ui/core/RadioGroup"
44
import { makeStyles } from "@material-ui/core/styles"
55
import TextField from "@material-ui/core/TextField"
6+
import { Stack } from "components/Stack/Stack"
67
import { FC } from "react"
78
import { ParameterSchema } from "../../api/typesGenerated"
89
import { MONOSPACE_FONT_FAMILY } from "../../theme/constants"
910

11+
const isBoolean = (schema: ParameterSchema) => {
12+
return schema.validation_value_type === "bool"
13+
}
14+
15+
const ParameterLabel: React.FC<{ schema: ParameterSchema }> = ({ schema }) => {
16+
const styles = useStyles()
17+
18+
return (
19+
<label className={styles.label} htmlFor={schema.name}>
20+
<strong>var.{schema.name}</strong>
21+
{schema.description && <span className={styles.labelDescription}>{schema.description}</span>}
22+
</label>
23+
)
24+
}
25+
1026
export interface ParameterInputProps {
1127
disabled?: boolean
1228
schema: ParameterSchema
@@ -15,23 +31,22 @@ export interface ParameterInputProps {
1531

1632
export const ParameterInput: FC<ParameterInputProps> = ({ disabled, onChange, schema }) => {
1733
const styles = useStyles()
34+
1835
return (
19-
<div className={styles.root}>
20-
<div className={styles.title}>
21-
<h2>var.{schema.name}</h2>
22-
{schema.description && <span>{schema.description}</span>}
23-
</div>
36+
<Stack direction="column" className={styles.root}>
37+
<ParameterLabel schema={schema} />
2438
<div className={styles.input}>
2539
<ParameterField disabled={disabled} onChange={onChange} schema={schema} />
2640
</div>
27-
</div>
41+
</Stack>
2842
)
2943
}
3044

3145
const ParameterField: React.FC<ParameterInputProps> = ({ disabled, onChange, schema }) => {
3246
if (schema.validation_contains && schema.validation_contains.length > 0) {
3347
return (
3448
<RadioGroup
49+
id={schema.name}
3550
defaultValue={schema.default_source_value}
3651
onChange={(event) => {
3752
onChange(event.target.value)
@@ -50,11 +65,37 @@ const ParameterField: React.FC<ParameterInputProps> = ({ disabled, onChange, sch
5065
)
5166
}
5267

68+
if (isBoolean(schema)) {
69+
return (
70+
<RadioGroup
71+
id={schema.name}
72+
defaultValue={schema.default_source_value}
73+
onChange={(event) => {
74+
onChange(event.target.value)
75+
}}
76+
>
77+
<FormControlLabel
78+
disabled={disabled}
79+
value="true"
80+
control={<Radio color="primary" size="small" disableRipple />}
81+
label="True"
82+
/>
83+
<FormControlLabel
84+
disabled={disabled}
85+
value="false"
86+
control={<Radio color="primary" size="small" disableRipple />}
87+
label="False"
88+
/>
89+
</RadioGroup>
90+
)
91+
}
92+
5393
// A text field can technically handle all cases!
5494
// As other cases become more prominent (like filtering for numbers),
5595
// we should break this out into more finely scoped input fields.
5696
return (
5797
<TextField
98+
id={schema.name}
5899
size="small"
59100
disabled={disabled}
60101
placeholder={schema.default_source_value}
@@ -67,25 +108,26 @@ const ParameterField: React.FC<ParameterInputProps> = ({ disabled, onChange, sch
67108

68109
const useStyles = makeStyles((theme) => ({
69110
root: {
70-
display: "flex",
71-
flexDirection: "column",
72111
fontFamily: MONOSPACE_FONT_FAMILY,
73112
paddingTop: theme.spacing(2),
74113
paddingBottom: theme.spacing(2),
75114
},
76-
title: {
115+
label: {
77116
display: "flex",
78117
flexDirection: "column",
79-
"& h2": {
80-
margin: 0,
81-
},
82-
"& span": {
83-
paddingTop: theme.spacing(1),
84-
},
118+
fontSize: 21,
119+
},
120+
labelDescription: {
121+
fontSize: 14,
122+
marginTop: theme.spacing(1),
85123
},
86124
input: {
87-
marginTop: theme.spacing(2),
88125
display: "flex",
89126
flexDirection: "column",
90127
},
128+
checkbox: {
129+
display: "flex",
130+
alignItems: "center",
131+
gap: theme.spacing(1),
132+
},
91133
}))

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.stories.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ Parameters.args = {
6464
description: "How large should you instance be?",
6565
validation_contains: ["Small", "Medium", "Big"],
6666
}),
67+
createParameterSchema({
68+
name: "instance_size",
69+
default_source_value: "Big",
70+
description: "How large should your instance be?",
71+
validation_contains: ["Small", "Medium", "Big"],
72+
}),
73+
createParameterSchema({
74+
name: "disable_docker",
75+
description: "Disable Docker?",
76+
validation_value_type: "bool",
77+
default_source_value: "false",
78+
}),
6779
],
6880
createWorkspaceErrors: {},
6981
}

0 commit comments

Comments
 (0)