-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathRequestBodyWrapper.jsx
More file actions
75 lines (66 loc) · 2.14 KB
/
Copy pathRequestBodyWrapper.jsx
File metadata and controls
75 lines (66 loc) · 2.14 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
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
const RequestBodyWrapper = (Original) => {
const RequestBody = ({ requestBody, isExecute, onChange, ...restProps }) => {
const { onChangeIncludeEmpty } = restProps;
const isRequired = requestBody && requestBody.get('required') === true;
const isActive = isExecute || restProps.tryItOutEnabled;
const showToggle = !isRequired && isActive;
const [includeBody, setIncludeBody] = useState(true);
const handleToggle = (e) => {
const { checked } = e.target;
setIncludeBody(checked);
if (!checked) {
if (typeof onChange === 'function') {
onChange(undefined);
}
if (typeof onChangeIncludeEmpty === 'function') {
onChangeIncludeEmpty(false);
}
} else if (typeof onChangeIncludeEmpty === 'function') {
onChangeIncludeEmpty(true);
}
};
return (
<div className="swagger-editor__request-body-wrapper">
{showToggle && (
<label htmlFor="request-body-toggle" className="swagger-editor__request-body-toggle">
<input
id="request-body-toggle"
type="checkbox"
checked={includeBody}
onChange={handleToggle}
/>
<span>Send request body</span>
</label>
)}
{(!showToggle || includeBody) && (
<Original
// eslint-disable-next-line react/jsx-props-no-spreading
{...restProps}
requestBody={requestBody}
isExecute={isExecute}
onChange={onChange}
/>
)}
</div>
);
};
RequestBody.propTypes = {
requestBody: ImmutablePropTypes.map,
isExecute: PropTypes.bool,
tryItOutEnabled: PropTypes.bool,
onChange: PropTypes.func,
onChangeIncludeEmpty: PropTypes.func,
};
RequestBody.defaultProps = {
requestBody: null,
isExecute: false,
tryItOutEnabled: false,
onChange: () => {},
onChangeIncludeEmpty: () => {},
};
return RequestBody;
};
export default RequestBodyWrapper;