Re-add React.memo for linked inputs (#2407)

This commit is contained in:
Jaco Greeff 2020-03-18 10:17:20 +01:00 committed by GitHub
parent 484a60378a
commit 0cff9a324b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 59 additions and 32 deletions

View File

@ -4,7 +4,7 @@
import { BareProps } from './types';
import React, { useEffect, useRef, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import SUIButton from 'semantic-ui-react/dist/commonjs/elements/Button/Button';
import SUIDropdown, { DropdownProps } from 'semantic-ui-react/dist/commonjs/modules/Dropdown/Dropdown';
@ -48,20 +48,23 @@ function BaseDropdown<Option> ({ allowAdd = false, className, defaultValue, drop
const lastUpdate = useRef<string>('');
const [stored, setStored] = useState<any>();
const _setStored = (value: any): void => {
const json = JSON.stringify({ v: value });
const _setStored = useCallback(
(value: any): void => {
const json = JSON.stringify({ v: value });
if (lastUpdate.current !== json) {
lastUpdate.current = json;
if (lastUpdate.current !== json) {
lastUpdate.current = json;
setStored(value);
onChange && onChange(
transform
? transform(value)
: value
);
}
};
setStored(value);
onChange && onChange(
transform
? transform(value)
: value
);
}
},
[onChange, transform]
);
useEffect((): void => {
_setStored(isUndefined(value) ? defaultValue : value);
@ -119,7 +122,7 @@ function BaseDropdown<Option> ({ allowAdd = false, className, defaultValue, drop
);
}
const Dropdown = styled(BaseDropdown)`
const Dropdown = React.memo(styled(BaseDropdown)`
.ui--Dropdown-item {
position: relative;
white-space: nowrap;
@ -155,7 +158,7 @@ const Dropdown = styled(BaseDropdown)`
}
}
}
` as unknown as IDropdown<any>;
`) as unknown as IDropdown<any>;
(Dropdown as any).Header = SUIDropdown.Header;

View File

@ -29,7 +29,7 @@ function transform ({ value }: Props): (method: string) => ConstValueBase {
};
}
export default function SelectKey (props: Props): React.ReactElement<Props> | null {
function SelectKey (props: Props): React.ReactElement<Props> | null {
const { className, isError, onChange, options, style, value } = props;
if (!options.length) {
@ -49,3 +49,5 @@ export default function SelectKey (props: Props): React.ReactElement<Props> | nu
/>
);
}
export default React.memo(SelectKey);

View File

@ -19,7 +19,7 @@ interface Props extends BareProps {
value: ConstValueBase;
}
export default function SelectSection ({ className, defaultValue, isError, onChange, options, style, value: { section } }: Props): React.ReactElement<Props> {
function SelectSection ({ className, defaultValue, isError, onChange, options, style, value: { section } }: Props): React.ReactElement<Props> {
return (
<Dropdown
className={classes('ui--DropdownLinked-Sections', className)}
@ -33,3 +33,5 @@ export default function SelectSection ({ className, defaultValue, isError, onCha
/>
);
}
export default React.memo(SelectSection);

View File

@ -40,7 +40,7 @@ function getValue (api: ApiPromise, { method, section }: ConstValueBase): ConstV
};
}
export default function InputConsts ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
function InputConsts ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(keyOptions(api, defaultValue.section));
const [optionsSection] = useState<DropdownOptions>(sectionOptions(api));
@ -92,3 +92,5 @@ export default function InputConsts ({ className, defaultValue, help, label, onC
</LinkedWrapper>
);
}
export default React.memo(InputConsts);

View File

@ -35,7 +35,7 @@ function LinkedWrapper ({ children, className, help, label, style, withLabel }:
);
}
export default styled(LinkedWrapper)`
export default React.memo(styled(LinkedWrapper)`
.ui--DropdownLinked-Items {
.text {
box-sizing: border-box;
@ -72,4 +72,4 @@ export default styled(LinkedWrapper)`
text-align: right;
text-overflow: ellipsis;
}
`;
`);

View File

@ -6,7 +6,7 @@ import { SubmittableExtrinsicFunction } from '@polkadot/api/types';
import { BareProps } from '../types';
import { DropdownOptions } from '../util/types';
import React from 'react';
import React, { useCallback } from 'react';
import ApiPromise from '@polkadot/api/promise';
import Dropdown from '../Dropdown';
@ -20,13 +20,16 @@ interface Props extends BareProps {
value: SubmittableExtrinsicFunction<'promise'>;
}
export default function SelectMethod ({ api, className, isError, onChange, options, style, value }: Props): React.ReactElement<Props> | null {
function SelectMethod ({ api, className, isError, onChange, options, style, value }: Props): React.ReactElement<Props> | null {
if (!options.length) {
return null;
}
const transform = (method: string): SubmittableExtrinsicFunction<'promise'> =>
api.tx[value.section][method];
const transform = useCallback(
(method: string): SubmittableExtrinsicFunction<'promise'> =>
api.tx[value.section][method],
[value]
);
return (
<Dropdown
@ -41,3 +44,5 @@ export default function SelectMethod ({ api, className, isError, onChange, optio
/>
);
}
export default React.memo(SelectMethod);

View File

@ -19,7 +19,7 @@ interface Props extends BareProps {
value: SubmittableExtrinsicFunction<'promise'>;
}
export default function SelectSection ({ className, defaultValue, isError, onChange, options, style, value }: Props): React.ReactElement<Props> {
function SelectSection ({ className, defaultValue, isError, onChange, options, style, value }: Props): React.ReactElement<Props> {
return (
<Dropdown
className={classes('ui--DropdownLinked-Sections', className)}
@ -33,3 +33,5 @@ export default function SelectSection ({ className, defaultValue, isError, onCha
/>
);
}
export default React.memo(SelectSection);

View File

@ -26,7 +26,7 @@ function transform ({ value }: Props): (method: string) => RpcMethod {
};
}
export default function SelectMethod (props: Props): React.ReactElement<Props> | null {
function SelectMethod (props: Props): React.ReactElement<Props> | null {
const { className, isError, onChange, options, style, value } = props;
if (!options.length) {
@ -46,3 +46,5 @@ export default function SelectMethod (props: Props): React.ReactElement<Props> |
/>
);
}
export default React.memo(SelectMethod);

View File

@ -18,8 +18,7 @@ interface Props extends BareProps {
options: DropdownOptions;
value: RpcMethod;
}
export default function SelectSection ({ className, defaultValue, isError, onChange, options, style, value }: Props): React.ReactElement<Props> {
function SelectSection ({ className, defaultValue, isError, onChange, options, style, value }: Props): React.ReactElement<Props> {
return (
<Dropdown
className={classes('ui--DropdownLinked-Sections', className)}
@ -33,3 +32,5 @@ export default function SelectSection ({ className, defaultValue, isError, onCha
/>
);
}
export default React.memo(SelectSection);

View File

@ -28,7 +28,7 @@ interface Props {
withLabel?: boolean;
}
export default function InputRpc ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
function InputRpc ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(methodOptions(api, defaultValue.section));
const [optionsSection] = useState<DropdownOptions>(sectionOptions(api));
@ -77,3 +77,5 @@ export default function InputRpc ({ className, defaultValue, help, label, onChan
</LinkedWrapper>
);
}
export default React.memo(InputRpc);

View File

@ -28,7 +28,7 @@ function transform (api: ApiPromise, { value }: Props): (method: string) => Stor
};
}
export default function SelectKey (props: Props): React.ReactElement<Props> | null {
function SelectKey (props: Props): React.ReactElement<Props> | null {
const { api } = useApi();
const { className, isError, onChange, options, style, value } = props;
@ -49,3 +49,5 @@ export default function SelectKey (props: Props): React.ReactElement<Props> | nu
/>
);
}
export default React.memo(SelectKey);

View File

@ -19,7 +19,7 @@ interface Props extends BareProps {
value: StorageEntryPromise;
}
export default function SelectSection ({ className, defaultValue, isError, onChange, options, style, value: { creator: { section } } }: Props): React.ReactElement<Props> {
function SelectSection ({ className, defaultValue, isError, onChange, options, style, value: { creator: { section } } }: Props): React.ReactElement<Props> {
return (
<Dropdown
className={classes('ui--DropdownLinked-Sections', className)}
@ -33,3 +33,5 @@ export default function SelectSection ({ className, defaultValue, isError, onCha
/>
);
}
export default React.memo(SelectSection);

View File

@ -27,7 +27,7 @@ interface Props {
withLabel?: boolean;
}
export default function InputStorage ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
function InputStorage ({ className, defaultValue, help, label, onChange, style, withLabel }: Props): React.ReactElement<Props> {
const { api } = useApi();
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(keyOptions(api, defaultValue.creator.section));
const [optionsSection] = useState<DropdownOptions>(sectionOptions(api));
@ -76,3 +76,5 @@ export default function InputStorage ({ className, defaultValue, help, label, on
</LinkedWrapper>
);
}
export default React.memo(InputStorage);