This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
DisclosureComponent.jsx
124 lines (107 loc) · 3.77 KB
/
DisclosureComponent.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames/bind';
import Button from 'terra-button';
import ContentContainer from 'terra-content-container';
import Input from 'terra-form-input';
import ActionHeader from 'terra-action-header';
import { withDisclosureManager, disclosureManagerShape } from 'terra-disclosure-manager';
import styles from './SlidePanelManager.module.scss';
const cx = classNames.bind(styles);
const propTypes = {
name: PropTypes.string,
disclosureType: PropTypes.string,
disclosureManager: disclosureManagerShape,
};
const defaultProps = {
name: 'Disclosure Component',
};
class DisclosureComponent extends React.Component {
constructor(props) {
super(props);
this.checkLockState = this.checkLockState.bind(this);
this.state = {
text: undefined,
};
}
componentDidMount() {
const { disclosureManager } = this.props;
if (disclosureManager && disclosureManager.registerDismissCheck) {
disclosureManager.registerDismissCheck(this.checkLockState);
}
}
checkLockState() {
if (this.state.text && this.state.text.length) {
return new Promise((resolve, reject) => {
// eslint-disable-next-line no-restricted-globals
if (!confirm(`${this.props.name} has unsaved changes that will be lost. Do you wish to continue?`)) { // eslint-disable-line no-alert
reject();
return;
}
resolve();
});
}
return Promise.resolve();
}
render() {
const { disclosureManager, name, disclosureType } = this.props;
return (
<ContentContainer
fill
header={(
<ActionHeader
title={`Disclosure - ${name}`}
onClose={disclosureManager.closeDisclosure}
onBack={disclosureManager.goBack}
onMaximize={disclosureManager.maximize}
onMinimize={disclosureManager.minimize}
/>
)}
>
<div className={cx('content-wrapper')}>
<h3>{name}</h3>
<p>The disclosed component can disclose content within the same panel.</p>
<p>It can also render a header (like above) that implements the various DisclosureManager control functions.</p>
<Button
text="Dismiss"
onClick={() => {
disclosureManager.dismiss()
.catch(() => {
console.log('Dismiss failed. A lock must be in place.'); // eslint-disable-line no-console
});
}}
/>
<Button
text="Disclose Again"
onClick={() => {
disclosureManager.disclose({
preferredType: disclosureType,
size: 'small',
content: {
key: `Nested ${name}`,
component: <WrappedDisclosureComponent name={`Nested ${name}`} disclosureType={disclosureType} />,
},
});
}}
/>
<br />
<br />
<p>The disclosed component can register a dismiss check function that can interrupt and prevent dismissal. This component will prompt the user if text is detected in the input field below.</p>
<Input
value={this.state.text || ''}
onChange={(event) => {
this.setState({
text: event.target.value,
});
}}
/>
{this.state.text && this.state.text.length ? <p>Component has unsaved changes!</p> : null}
</div>
</ContentContainer>
);
}
}
DisclosureComponent.propTypes = propTypes;
DisclosureComponent.defaultProps = defaultProps;
const WrappedDisclosureComponent = withDisclosureManager(DisclosureComponent);
export default withDisclosureManager(WrappedDisclosureComponent);