Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): Move react-select into generic SelectField component #2543

Merged
merged 2 commits into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions app/src/components/RobotSettings/SelectNetwork/SelectSsid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// @flow
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component pulled out of app/src/components/RobotSettings/connection.js into its own file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good now we can find it again 😉

import * as React from 'react'
import map from 'lodash/map'

import {Icon} from '@opentrons/components'
import SelectField from '../../SelectField'
import styles from './styles.css'

import type {IconName} from '@opentrons/components'
import type {WifiNetwork, WifiNetworkList} from '../../../http-api-client'
import type {OptionType} from '../../SelectField'

type Props = {
list: ?WifiNetworkList,
value: ?string,
disabled?: boolean,
onValueChange: (name: string, ssid: string) => mixed,
}

export default function SelectSsid (props: Props) {
const {value, list, disabled, onValueChange} = props

return (
<SelectField
name="ssid"
value={value}
options={map(list, makeNetworkOption)}
onValueChange={onValueChange}
placeholder="Select network"
className={styles.wifi_dropdown}
disabled={disabled}
/>
)
}

function makeNetworkOption (nw: WifiNetwork): OptionType {
const value = nw.ssid
const connectedIcon = nw.active ? (
<Icon name="check" className={styles.wifi_option_icon} />
) : (
<span className={styles.wifi_option_icon} />
)

const securedIcon =
nw.securityType !== 'none' ? (
<Icon name="lock" className={styles.wifi_option_icon_right} />
) : (
<span className={styles.wifi_option_icon_right} />
)

let signalIconName: IconName
if (nw.signal <= 25) {
signalIconName = 'ot-wifi-0'
} else if (nw.signal <= 50) {
signalIconName = 'ot-wifi-1'
} else if (nw.signal <= 75) {
signalIconName = 'ot-wifi-2'
} else {
signalIconName = 'ot-wifi-3'
}
const signalIcon = (
<Icon name={signalIconName} className={styles.wifi_option_icon_right} />
)

const label = (
<div className={styles.wifi_option}>
{connectedIcon}
<span className={styles.wifi_name}>{value}</span>
{securedIcon}
{signalIcon}
</div>
)

return {value, label}
}
38 changes: 20 additions & 18 deletions app/src/components/RobotSettings/SelectNetwork/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ import {

import {IntervalWrapper, SpinnerModal} from '@opentrons/components'
import {Portal} from '../../portal'
import {NetworkDropdown} from '../connection'
import ConnectModal from './ConnectModal'
import ConnectForm from './ConnectForm'
import SelectSsid from './SelectSsid'

import type {State, Dispatch} from '../../../types'
import type {ViewableRobot} from '../../../discovery'
import type {
WifiNetwork,
WifiNetworkList,
WifiSecurityType,
WifiEapOption,
Expand Down Expand Up @@ -60,21 +59,24 @@ class SelectNetwork extends React.Component<Props, SelectNetworkState> {
this.state = {ssid: this.getActiveSsid(), securityType: null}
}

onChange = (network: WifiNetwork) => {
const nextState: $Shape<SelectNetworkState> = {
ssid: network.ssid,
securityType: network.securityType,
}
setCurrentSsid = (_: string, ssid: string) => {
const network = find(this.props.list, {ssid})

// TODO(mc, 2018-10-22): pass network security type direct
if (network.securityType === NO_SECURITY) {
this.props.configure({ssid: network.ssid})
} else if (network.securityType === WPA_EAP_SECURITY) {
this.props.fetchEapOptions()
}
// TODO(mc, 2018-10-18): handle hidden network
if (network) {
const nextState: $Shape<SelectNetworkState> = {
ssid,
securityType: network.securityType,
}

if (network.securityType === NO_SECURITY) {
this.props.configure({ssid})
} else if (network.securityType === WPA_EAP_SECURITY) {
this.props.fetchEapOptions()
}
// TODO(mc, 2018-10-18): handle hidden network

this.setState(nextState)
this.setState(nextState)
}
}

closeConnectForm = () => this.setState({securityType: null})
Expand All @@ -98,11 +100,11 @@ class SelectNetwork extends React.Component<Props, SelectNetworkState> {

return (
<IntervalWrapper refresh={fetchList} interval={LIST_REFRESH_MS}>
<NetworkDropdown
list={list}
<SelectSsid
value={ssid}
list={list}
disabled={connectingTo != null}
onChange={this.onChange}
onValueChange={this.setCurrentSsid}
/>
<Portal>
{connectingTo && (
Expand Down
33 changes: 33 additions & 0 deletions app/src/components/RobotSettings/SelectNetwork/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,39 @@
margin: 0 1rem 1rem;
}

.wifi_dropdown {
max-width: 16.875rem;
}

.wifi_option {
@apply --font-body-1-dark;

width: 100%;
display: flex;
}

.wifi_name {
flex-basis: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

.wifi_option_icon,
.wifi_option_icon_right {
flex: none;
height: 1rem;
width: 1rem;
}

.wifi_option_icon {
padding-right: 0.25rem;
}

.wifi_option_icon_right {
padding-left: 0.25rem;
}

.form_table {
display: table;
width: 80%;
Expand Down
133 changes: 1 addition & 132 deletions app/src/components/RobotSettings/connection.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
// @flow
// UI components for displaying connection info
import * as React from 'react'
import Select, {components} from 'react-select'
import find from 'lodash/find'
import {Icon} from '@opentrons/components'
import {CardContentHalf} from '../layout'
import styles from './styles.css'

import type {IconName} from '@opentrons/components'
import type {
InternetStatus,
NetworkInterface,
WifiNetworkList,
WifiNetwork,
} from '../../http-api-client'
import type {InternetStatus, NetworkInterface} from '../../http-api-client'

type ConnectionStatusProps = {type: string, status: ?InternetStatus}

Expand Down Expand Up @@ -69,128 +60,6 @@ export function ConnectionInfo (props: ConnectionInfoProps) {
)
}

type SelectNetworkOption = {
...$Exact<WifiNetwork>,
value: string,
label: React.Node,
}

type NetworkDropdownProps = {
list: ?WifiNetworkList,
value: ?string,
disabled: boolean,
onChange: SelectNetworkOption => mixed,
}

export function NetworkDropdown (props: NetworkDropdownProps) {
const {value, disabled, onChange} = props
const list = props.list || []
const options = list.map(NetworkOption)
const selectedOption = find(options, {value})

const selectStyles = {
option: base => ({
...base,
padding: '0.25rem 0',
}),
input: () => ({
marginTop: '-0.25rem',
marginLeft: 0,
}),
container: base => ({
...base,
backgroundColor: 'transparent',
height: '2rem',
overflow: 'visible',
}),
control: () => ({
backgroundColor: '#e5e2e2',
border: 'none',
padding: '0.25rem 0rem',
outline: 'none',
borderRadius: '3px',
height: '1.75rem',
boxShadow: 'none',
}),
indicatorSeparator: () => ({
display: 'none',
}),
}
// Custom dropdown indicator icon component needed to match comp lib
const DropdownIndicator = props => {
return (
components.DropdownIndicator && (
<components.DropdownIndicator {...props}>
<div className={styles.dropdown_icon}>
<Icon name="menu-down" width="100%" />
</div>
</components.DropdownIndicator>
)
)
}
// custom Menu (options dropdown) component
const Menu = props => {
return (
<components.Menu {...props}>
<div className={styles.options_menu}>{props.children}</div>
</components.Menu>
)
}

return (
<Select
className={styles.wifi_dropdown}
isDisabled={disabled}
value={selectedOption}
onChange={onChange}
options={options}
styles={selectStyles}
components={{DropdownIndicator, Menu}}
/>
)
}

function NetworkOption (nw: WifiNetwork): SelectNetworkOption {
const value = nw.ssid
const connectedIcon = nw.active ? (
<Icon name="check" className={styles.wifi_option_icon} />
) : (
<span className={styles.wifi_option_icon} />
)

const securedIcon =
nw.securityType !== 'none' ? (
<Icon name="lock" className={styles.wifi_option_icon_right} />
) : (
<span className={styles.wifi_option_icon_right} />
)

let signalIconName: IconName
if (nw.signal <= 25) {
signalIconName = 'ot-wifi-0'
} else if (nw.signal <= 50) {
signalIconName = 'ot-wifi-1'
} else if (nw.signal <= 75) {
signalIconName = 'ot-wifi-2'
} else {
signalIconName = 'ot-wifi-3'
}
const signalIcon = (
<Icon name={signalIconName} className={styles.wifi_option_icon_right} />
)

const label = (
<div className={styles.wifi_option}>
{connectedIcon}
<span className={styles.wifi_name}>{value}</span>
{signalIcon}
{securedIcon}
</div>
)

return {...nw, value, label}
}

type NetworkAddressProps = {
connection: ?NetworkInterface,
wired: ?boolean,
Expand Down
Loading