Search/filter functionality of selection dropdowns (#9766)

* feat: added search/filter functionality to selection dropdowns in the storage, constants, RPC calls, runtime calls, and extrinsic pages

* chore: update copyright header

* fix: types

---------

Co-authored-by: Dominik Fedor <DominikFedor@users.noreply.github.com>
This commit is contained in:
Dominik Fedor 2024-03-28 14:24:30 +01:00 committed by GitHub
parent c09201fe3b
commit 130eac4d90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 46 additions and 13 deletions

View File

@ -1,7 +1,7 @@
// Copyright 2017-2024 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DropdownItemProps, DropdownProps } from 'semantic-ui-react';
import type { DropdownItemProps, DropdownProps, StrictDropdownProps } from 'semantic-ui-react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { Button as SUIButton, Dropdown as SUIDropdown } from 'semantic-ui-react';
@ -28,7 +28,7 @@ interface Props<Option extends DropdownItemProps> {
onBlur?: () => void;
onChange?: (value: any) => void;
onClose?: () => void;
onSearch?: (filteredOptions: any[], query: string) => Option[];
onSearch?: StrictDropdownProps['search'];
options: (React.ReactNode | Option)[];
placeholder?: string;
renderLabel?: (item: any) => any;

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { DropdownItemProps } from 'semantic-ui-react';
import type { KeyringOption$Type, KeyringOptions, KeyringSectionOption, KeyringSectionOptions } from '@polkadot/ui-keyring/options/types';
import type { KeyringOption$Type, KeyringOptions, KeyringSectionOption } from '@polkadot/ui-keyring/options/types';
import type { Option } from './types.js';
import React from 'react';
@ -299,14 +299,14 @@ class InputAddress extends React.PureComponent<Props, State> {
}
};
private onSearch = (filteredOptions: KeyringSectionOptions, _query: string): DropdownItemProps[] => {
private onSearch = (filteredOptions: DropdownItemProps[], _query: string): DropdownItemProps[] => {
const { isInput = true } = this.props;
const query = _query.trim();
const queryLower = query.toLowerCase();
const matches = filteredOptions.filter((item): boolean =>
!!item.value && (
(item.name.toLowerCase && item.name.toLowerCase().includes(queryLower)) ||
item.value.toLowerCase().includes(queryLower)
!!item.value && typeof item.name === 'string' && (
(item.name.toLowerCase?.().includes(queryLower)) ||
item.value.toString().toLowerCase().includes(queryLower)
)
);
@ -314,11 +314,15 @@ class InputAddress extends React.PureComponent<Props, State> {
const accountId = transformToAccountId(query);
if (accountId) {
matches.push(
keyring.saveRecent(
accountId.toString()
).option
);
const item = keyring.saveRecent(
accountId.toString()
).option;
matches.push({
key: item.key,
name: item.name,
value: item.value || undefined
});
}
}
@ -333,7 +337,7 @@ class InputAddress extends React.PureComponent<Props, State> {
const hasNext = nextItem?.value;
return !(isNull(item.value) || isUndefined(item.value)) || (!isLast && !!hasNext);
}) as DropdownItemProps[];
});
};
}

View File

@ -7,6 +7,7 @@ import type { DropdownOption } from '../util/types.js';
import React, { useCallback } from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -32,6 +33,7 @@ function SelectMethod ({ className = '', defs, isError, onChange, options, value
className={`${className} ui--DropdownLinked-Items`}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
transform={_transform}
value={value.method}

View File

@ -7,6 +7,7 @@ import type { DropdownOptions } from '../util/types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -24,6 +25,7 @@ function SelectSection ({ className = '', defaultValue, isError, onChange, optio
defaultValue={defaultValue}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
value={value.section}
withLabel={false}

View File

@ -7,6 +7,7 @@ import type { ConstValueBase } from './types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -39,6 +40,7 @@ function SelectKey (props: Props): React.ReactElement<Props> | null {
className={`${className} ui--DropdownLinked-Items`}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
transform={transform(props)}
value={value.method}

View File

@ -7,6 +7,7 @@ import type { ConstValueBase, StorageEntryPromise } from './types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -24,6 +25,7 @@ function SelectSection ({ className = '', defaultValue, isError, onChange, optio
defaultValue={defaultValue}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
value={section}
withLabel={false}

View File

@ -8,6 +8,7 @@ import type { DropdownOptions } from '../util/types.js';
import React, { useCallback } from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
api: ApiPromise;
@ -38,6 +39,7 @@ function SelectMethod ({ api, className = '', defaultValue, isDisabled, isError,
isDisabled={isDisabled}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
transform={transform}
value={value.method}

View File

@ -7,6 +7,7 @@ import type { DropdownOptions } from '../util/types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -26,6 +27,7 @@ function SelectSection ({ className = '', defaultValue, isDisabled, isError, onC
isDisabled={isDisabled}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
value={value.section}
withLabel={false}

View File

@ -7,6 +7,7 @@ import type { DropdownOption } from '../util/types.js';
import React, { useCallback } from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
import useRpcs from './useRpcs.js';
interface Props {
@ -34,6 +35,7 @@ function SelectMethod ({ className = '', isError, onChange, options, value }: Pr
className={`${className} ui--DropdownLinked-Items`}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
transform={_transform}
value={value.method}

View File

@ -7,6 +7,7 @@ import type { DropdownOptions } from '../util/types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -24,6 +25,7 @@ function SelectSection ({ className = '', defaultValue, isError, onChange, optio
defaultValue={defaultValue}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
value={value.section}
withLabel={false}

View File

@ -10,6 +10,7 @@ import React from 'react';
import { useApi } from '@polkadot/react-hooks';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -41,6 +42,7 @@ function SelectKey (props: Props): React.ReactElement<Props> | null {
className={`${className} ui--DropdownLinked-Items`}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
transform={transform(api, props)}
value={value.creator.method}

View File

@ -7,6 +7,7 @@ import type { DropdownOptions } from '../util/types.js';
import React from 'react';
import Dropdown from '../Dropdown.js';
import { filterDropdownItems } from '../util/index.js';
interface Props {
className?: string;
@ -24,6 +25,7 @@ function SelectSection ({ className = '', defaultValue, isError, onChange, optio
defaultValue={defaultValue}
isError={isError}
onChange={onChange}
onSearch={filterDropdownItems}
options={options}
value={section}
withLabel={false}

View File

@ -0,0 +1,8 @@
// Copyright 2017-2024 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DropdownItemProps } from 'semantic-ui-react';
export const filterDropdownItems = (items: DropdownItemProps[], query: string) => {
return items.filter((item) => item.value?.toString().toLowerCase().includes(query.toLowerCase()));
};

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
export { checkVisibility } from './checkVisibility.js';
export { filterDropdownItems } from './dropdownItemsFilter.js';
export { getAccountCryptoType } from './getAccountCryptoType.js';
export { getAddressMeta } from './getAddressMeta.js';
export { getAddressName } from './getAddressName.js';