forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ConsoleSelector): introduce ConsoleSelector component (patternfl…
…y#606) * feat(ConsoleSelector): introduce ConsoleSelector component affects: @patternfly/react-console The component unifies composition of various console components for server/virtual machine remote access (recently via serial console or VNC). * update paddings * fix(VncConsole): sync .scss and .less and minor fixes Fixes (as top-level Toolbar is gone): - unit tests - padding of VncConsole actions * feat(AccessConsoles): Rename ConsoleSelector to AccessConsoles Based on design-review. Former `ConsoleSelector` component has not been released yet.
- Loading branch information
1 parent
f1d591c
commit d876589
Showing
17 changed files
with
1,900 additions
and
21 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
packages/patternfly-3/react-console/less/console-selector.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.console-selector-pf { | ||
margin-bottom: 0px; | ||
} | ||
|
||
.console-selector-pf-disconnect-switch { | ||
margin-left: 15px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
*/ | ||
@import 'serial-console'; | ||
@import 'vnc-console'; | ||
@import 'console-selector'; |
24 changes: 13 additions & 11 deletions
24
packages/patternfly-3/react-console/less/vnc-console.less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
.vnc-console { | ||
padding-right: 0px; | ||
padding-left: 0px; | ||
|
||
.toolbar-pf-results { | ||
padding: 15px 0; | ||
border-top: none; | ||
margin-top: 0px; | ||
} | ||
.vnc-console-connecting { | ||
background-color: @color-pf-green; | ||
} | ||
|
||
.vnc-console-disconnected { | ||
background-color: @color-pf-red; | ||
} | ||
.toolbar-pf { | ||
border-bottom: none; | ||
} | ||
.toolbar-pf-results { | ||
padding-top: 10px; | ||
} | ||
.toolbar-pf-action-right .dropdown-menu { | ||
min-width: 102px; /* avoid overflow if DropdownButton is used under Toolbar.RightContent */ | ||
|
||
.toolbar-pf-action-right { | ||
padding-bottom: 5px; | ||
|
||
.dropdown-menu { | ||
min-width: 102px; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/patternfly-3/react-console/sass/_console-selector.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.console-selector-pf { | ||
margin-bottom: 0; | ||
} | ||
|
||
.console-selector-pf-disconnect-switch { | ||
margin-left: 15px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/patternfly-3/react-console/src/AccessConsoles/AccessConsoles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { Grid, Row, Col, Form, FormGroup, Dropdown, MenuItem, Checkbox } from 'patternfly-react'; | ||
|
||
import { NONE_TYPE, SERIAL_CONSOLE_TYPE, VNC_CONSOLE_TYPE } from '../common/constants'; | ||
|
||
class AccessConsoles extends React.Component { | ||
state = { | ||
type: NONE_TYPE, | ||
disconnectByChange: this.props.disconnectByChange, | ||
keptConnection: {} // no connection exists when mounted | ||
}; | ||
|
||
onTypeChange(type) { | ||
this.setState(prevState => { | ||
const keptConnection = prevState.disconnectByChange | ||
? { [type]: true } | ||
: { ...prevState.keptConnection, [type]: true }; | ||
|
||
return { | ||
type, | ||
keptConnection | ||
}; | ||
}); | ||
} | ||
|
||
onChangeDisconnectBySwitchClick(target) { | ||
this.setState(prevState => ({ | ||
disconnectByChange: target.checked, | ||
keptConnection: target.checked ? { [prevState.type]: true } : prevState.keptConnection | ||
})); | ||
} | ||
|
||
getSelectedConsole() { | ||
return this.getConsoleForType(this.state.type); | ||
} | ||
|
||
getConsoleForType(type) { | ||
if (!this.props.children) { | ||
return null; | ||
} | ||
|
||
const getChildTypeName = child => (child.props.type ? child.props.type : (child.type && child.type.name) || null); | ||
const isChildOfType = child => getChildTypeName(child) === type; | ||
|
||
// To keep connection, render all consoles but hide those unused | ||
return React.Children.map( | ||
this.props.children, | ||
child => | ||
this.state.keptConnection[getChildTypeName(child)] ? ( | ||
<div key={getChildTypeName(child)} hidden={!isChildOfType(child)}> | ||
{child} | ||
</div> | ||
) : null | ||
); | ||
} | ||
|
||
render() { | ||
const items = { | ||
[NONE_TYPE]: this.props.textSelectConsoleType, | ||
[SERIAL_CONSOLE_TYPE]: this.props.textSerialConsole, | ||
[VNC_CONSOLE_TYPE]: this.props.textVncConsole | ||
}; | ||
|
||
return ( | ||
<Grid fluid> | ||
<Form horizontal> | ||
<FormGroup controlId="console-type" className="console-selector-pf"> | ||
<Col> | ||
<Dropdown id="console-type-selector" disabled={!this.props.children}> | ||
<Dropdown.Toggle> | ||
{this.props.children ? items[this.state.type] : this.props.textEmptyConsoleList} | ||
</Dropdown.Toggle> | ||
<Dropdown.Menu> | ||
{this.getConsoleForType(SERIAL_CONSOLE_TYPE) && ( | ||
<MenuItem eventKey="1" onClick={() => this.onTypeChange(SERIAL_CONSOLE_TYPE)}> | ||
{items[SERIAL_CONSOLE_TYPE]} | ||
</MenuItem> | ||
)} | ||
{this.getConsoleForType(VNC_CONSOLE_TYPE) && ( | ||
<MenuItem eventKey="2" onClick={() => this.onTypeChange(VNC_CONSOLE_TYPE)}> | ||
{items[VNC_CONSOLE_TYPE]} | ||
</MenuItem> | ||
)} | ||
</Dropdown.Menu> | ||
</Dropdown> | ||
{this.state.type !== NONE_TYPE && ( | ||
<Checkbox | ||
className="console-selector-pf-disconnect-switch" | ||
inline | ||
defaultChecked={this.props.disconnectByChange} | ||
onChange={e => this.onChangeDisconnectBySwitchClick(e.target)} | ||
> | ||
{this.props.textDisconnectByChange} | ||
</Checkbox> | ||
)} | ||
</Col> | ||
</FormGroup> | ||
</Form> | ||
<Row> | ||
<Col>{this.getSelectedConsole()}</Col> | ||
</Row> | ||
</Grid> | ||
); | ||
} | ||
} | ||
|
||
const validChildrenTypes = [SERIAL_CONSOLE_TYPE, VNC_CONSOLE_TYPE]; | ||
const childElementValidator = propValue => { | ||
if (propValue) { | ||
const children = Array.isArray(propValue) ? propValue : [propValue]; | ||
if ( | ||
!children.every( | ||
child => | ||
(child.type && validChildrenTypes.indexOf(child.type.name) >= 0) || | ||
(child.props && validChildrenTypes.indexOf(child.props.type) >= 0) | ||
) | ||
) { | ||
return new Error('AccessConsoles child validation failed'); | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
AccessConsoles.propTypes = { | ||
/** | ||
* Child element can be either | ||
* - <SerialConsole> or <VncConsole> | ||
* - or has a property "type" of value either SERIAL_CONSOLE_TYPE or VNC_CONSOLE_TYPE (useful when wrapping (composing) basic console components | ||
*/ | ||
children: PropTypes.oneOfType([PropTypes.objectOf(childElementValidator), PropTypes.arrayOf(childElementValidator)]), | ||
|
||
textSelectConsoleType: PropTypes.string /** Internationalization */, | ||
textSerialConsole: PropTypes.string /** Internationalization */, | ||
textVncConsole: PropTypes.string /** Internationalization */, | ||
textDisconnectByChange: PropTypes.string /** Internationalization */, | ||
textEmptyConsoleList: PropTypes.string /** Internationalization */, | ||
|
||
disconnectByChange: | ||
PropTypes.bool /** Initial value of "Disconnect before switching" checkbox, "false" to disconnect when console type changed */ | ||
}; | ||
|
||
AccessConsoles.defaultProps = { | ||
children: null, | ||
|
||
textSelectConsoleType: 'Select Console Type', | ||
textSerialConsole: 'Serial Console', | ||
textVncConsole: 'VNC Console', | ||
textDisconnectByChange: 'Disconnect before switching', | ||
textEmptyConsoleList: 'No console available', | ||
|
||
disconnectByChange: true /** By default, console is unmounted (disconnected) when switching to other type */ | ||
}; | ||
|
||
export default AccessConsoles; |
44 changes: 44 additions & 0 deletions
44
packages/patternfly-3/react-console/src/AccessConsoles/AccessConsoles.stories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
import { inlineTemplate } from 'storybook/decorators/storyTemplates'; | ||
import { name } from '../../package.json'; | ||
import { storybookPackageName } from 'storybook/constants/siteConstants'; | ||
|
||
import { noop } from 'patternfly-react'; | ||
import { AccessConsoles, VncConsole } from '../index'; | ||
import { SerialConsoleConnector } from '../SerialConsole/SerialConsole.stories'; // contains mock backend | ||
import { DISCONNECTED } from '../SerialConsole/constants'; | ||
import { SERIAL_CONSOLE_TYPE } from '../common/constants'; | ||
|
||
const stories = storiesOf(`${storybookPackageName(name)}/AccessConsoles`, module); | ||
|
||
stories.add( | ||
'AccessConsoles', | ||
withInfo()(() => { | ||
const story = ( | ||
<AccessConsoles> | ||
<SerialConsoleConnector onConnect={noop} onDisconnect={noop} status={DISCONNECTED} type={SERIAL_CONSOLE_TYPE} /> | ||
<VncConsole | ||
host="foo.bar.host" | ||
textDisconnected="Disconnected as expected - VncConsole component is not connected to a real backend" | ||
/> | ||
</AccessConsoles> | ||
); | ||
return inlineTemplate({ | ||
story, | ||
title: 'AccessConsoles' | ||
}); | ||
}) | ||
); | ||
|
||
stories.add( | ||
'AccessConsoles - empty', | ||
withInfo()(() => { | ||
const story = <AccessConsoles />; | ||
return inlineTemplate({ | ||
story, | ||
title: 'AccessConsoles - empty' | ||
}); | ||
}) | ||
); |
Oops, something went wrong.