Use toggle for custom endpoint (#1274)

This commit is contained in:
Jaco Greeff 2019-06-04 18:01:56 +02:00 committed by GitHub
parent 47bace3ac5
commit 63c3f8fa8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 52 deletions

View File

@ -7,7 +7,7 @@ import { SettingsStruct } from '@polkadot/ui-settings/types';
import React from 'react';
import styled from 'styled-components';
import { Button, Dropdown, Input } from '@polkadot/ui-app';
import { Button, Dropdown, Input, Toggle } from '@polkadot/ui-app';
import { ActionStatus } from '@polkadot/ui-app/Status/types';
import uiSettings from '@polkadot/ui-settings';
@ -23,23 +23,6 @@ type State = {
settings: SettingsStruct
};
const Wrapper = styled.div`
.ui.menu {
justify-content: flex-end;
margin-bottom: 0;
.active.item {
font-weight: bold;
}
}
.sub-label {
cursor: pointer;
padding: 0rem .5833rem;
text-align: right;
}
`;
class General extends React.PureComponent<Props, State> {
constructor (props: Props) {
super(props);
@ -57,11 +40,11 @@ class General extends React.PureComponent<Props, State> {
}
render () {
const { t } = this.props;
const { className, t } = this.props;
const { isUrlValid, settings: { i18nLang, uiMode, uiTheme } } = this.state;
return (
<Wrapper>
<div className={className}>
{this.renderEndpoint()}
<div className='ui--row'>
<div className='medium'>
@ -101,7 +84,7 @@ class General extends React.PureComponent<Props, State> {
label={t('Save & Reload')}
/>
</Button.Group>
</Wrapper>
</div>
);
}
@ -111,20 +94,12 @@ class General extends React.PureComponent<Props, State> {
return (
<>
<Button.Group isBasic>
<Button
isBasic
isNegative={!isCustomNode}
label={t('preset')}
onClick={this.toggleCustomNode}
/>
<Button
isBasic
isNegative={isCustomNode}
label={t('custom')}
onClick={this.toggleCustomNode}
/>
</Button.Group>
<Toggle
className='settings--cutomToggle'
defaultValue={isCustomNode}
label={t('custom endpoint')}
onChange={this.onChangeCustom}
/>
<div className='ui--row'>
{
isCustomNode
@ -178,22 +153,17 @@ class General extends React.PureComponent<Props, State> {
}));
}
private toggleCustomNode = (): void => {
this.setState(({ isCustomNode, settings }: State) => {
// reset URL to a preset when toggled to preset
const apiUrl = isCustomNode
? uiSettings.availableNodes[0].value
: settings.apiUrl;
return {
isCustomNode: !isCustomNode,
isUrlValid: true,
settings: {
...settings,
apiUrl
}
};
});
private onChangeCustom = (isCustomNode: boolean): void => {
this.setState(({ settings }: State) => ({
isCustomNode,
isUrlValid: true,
settings: {
...settings,
apiUrl: isCustomNode
? settings.apiUrl
: uiSettings.availableNodes[0].value
}
}));
}
private isValidUrl (apiUrl: string): boolean {
@ -215,4 +185,23 @@ class General extends React.PureComponent<Props, State> {
}
}
export default translate(General);
export default translate(styled(General)`
.settings--cutomToggle {
text-align: right;
}
.ui.menu {
justify-content: flex-end;
margin-bottom: 0;
.active.item {
font-weight: bold;
}
}
.sub-label {
cursor: pointer;
padding: 0rem .5833rem;
text-align: right;
}
`);

View File

@ -0,0 +1,54 @@
// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BareProps } from './types';
import React from 'react';
import SUICheckbox from 'semantic-ui-react/dist/commonjs/modules/Checkbox';
import styled from 'styled-components';
type Props = BareProps & {
defaultValue?: boolean,
label: React.ReactNode,
onChange?: (isChecked: boolean) => void
};
class Toggle extends React.PureComponent<Props> {
render () {
const { className, defaultValue, label } = this.props;
return (
<div className={className}>
<SUICheckbox
defaultChecked={defaultValue}
onChange={this.onChange}
toggle
/>
<label>{label}</label>
</div>
);
}
private onChange = (event: React.FormEvent<HTMLInputElement>, { checked }: any): void => {
const { onChange } = this.props;
onChange && onChange(checked);
}
}
export default styled(Toggle)`
> label {
display: inline-block;
margin-left: 0.5rem;
}
> label,
> .ui.checkbox {
vertical-align: middle;
}
.ui.checkbox + label {
color: rgba(78, 78, 78, 0.75);
}
`;

View File

@ -54,6 +54,7 @@ export { default as Static } from './Static';
export { default as Status } from './Status';
export { default as SummaryBox } from './SummaryBox';
export { default as Tabs } from './Tabs';
export { default as Toggle } from './Toggle';
export { default as Tooltip } from './Tooltip';
export { default as TxButton } from './TxButton';
export { default as TxComponent } from './TxComponent';