-
Notifications
You must be signed in to change notification settings - Fork 614
Expand file tree
/
Copy pathWithHelpText.tsx
More file actions
175 lines (163 loc) · 3.99 KB
/
WithHelpText.tsx
File metadata and controls
175 lines (163 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import * as React from "react"
import { styled } from '@mui/material/styles';
import clsx from "classnames"
import Typography from "@mui/material/Typography"
import {PropsWithChildren} from 'react';
const PREFIX = 'WithHelpText';
const classes = {
withHelpText: `${PREFIX}-withHelpText`,
helpText: `${PREFIX}-helpText`,
shrunk: `${PREFIX}-shrunk`,
notchedContainer: `${PREFIX}-notchedContainer`,
label: `${PREFIX}-label`,
legend: `${PREFIX}-legend`,
fieldset: `${PREFIX}-fieldset`,
notchedChild: `${PREFIX}-notchedChild`,
hr: `${PREFIX}-hr`,
hrChildren: `${PREFIX}-hrChildren`,
verticalHr: `${PREFIX}-verticalHr`
};
const Root = styled('div')((
{
theme
}
) => ({
[`& .${classes.withHelpText}`]: {
marginTop: theme.spacing(1.5),
display: "flex",
},
[`& .${classes.helpText}`]: {
...theme.typography.caption,
marginTop: theme.spacing(0.5),
paddingBottom: theme.spacing(2),
color: "rgba(0, 0, 0, 0.54)",
marginLeft: theme.spacing(2),
padding: "unset",
},
[`& .${classes.shrunk}`]: {
flexShrink: 1,
},
[`& .${classes.notchedContainer}`]: {
position: "relative",
width: "100%",
},
[`& .${classes.label}`]: {
color: "rgba(0, 0, 0, 0.54)",
...theme.typography.caption,
marginBottom: theme.spacing(1),
},
[`& .${classes.legend}`]: {
color: "rgba(0, 0, 0, 0.54)",
...theme.typography.caption,
},
[`& .${classes.fieldset}`]: {
position: "absolute",
top: theme.spacing(-1.5),
left: 0,
right: 0,
bottom: 0,
color: "rgba(0, 0, 0, 0.54)",
borderColor: "rgba(0, 0, 0, 0.23)",
borderRadius: "4px",
borderStyle: "solid",
borderWidth: "1px",
padding: theme.spacing(0, 1),
margin: theme.spacing(0),
pointerEvents: "none",
"&> legend": {
...theme.typography.caption,
},
},
[`& .${classes.notchedChild}`]: {
padding: theme.spacing(1, 1.75),
},
[`& .${classes.verticalHr}`]: {
display: "flex",
"&> hr": {
marginRight: theme.spacing(1),
},
"&> :not(:first-child)": {
flexGrow: 1,
},
},
[`&.${classes.hr}`]: {
width: "100%",
},
[`& .${classes.hrChildren}`]: {
"&> :last-child": {
paddingBottom: theme.spacing(2),
},
}
}));
interface WithHelpTextProps {
label?: string | JSX.Element | undefined
helpText?: string | JSX.Element
afterHelp?: JSX.Element
className?: string
notched?: boolean
shrink?: boolean
hrGroup?: boolean
}
const WithHelpText: React.FC<PropsWithChildren<WithHelpTextProps>> = ({
label,
children,
helpText,
afterHelp,
notched,
shrink,
className,
hrGroup,
}) => {
if (hrGroup) {
return (
<Root className={clsx(classes.hr, className)}>
<div className={classes.verticalHr}>
<hr />
<div>
<legend className={classes.legend}>{label}</legend>
<div className={classes.hrChildren}>
{children}
{helpText !== undefined && (
<div>
<Typography className={classes.helpText}>
{helpText}
</Typography>
{afterHelp}
</div>
)}
</div>
</div>
</div>
</Root>
);
}
return (
<Root className={clsx(classes.withHelpText, className)}>
<div
className={clsx({
[classes.notchedContainer]: notched,
[classes.shrunk]: shrink,
})}
>
{!notched && <label className={classes.label}>{label}</label>}
<div className={clsx({ [classes.notchedChild]: notched })}>
{children}
</div>
{notched && (
<fieldset className={classes.fieldset}>
<legend>
<span>{label}</span>
</legend>
</fieldset>
)}
</div>
{helpText !== undefined && (
<div>
<Typography className={classes.helpText}>{helpText}</Typography>
{afterHelp}
</div>
)}
</Root>
)
}
export default WithHelpText