types#DropdownOption TypeScript Examples

The following examples show how to use types#DropdownOption. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Select.tsx    From contracts-ui with GNU General Public License v3.0 7 votes vote down vote up
export function AddressSelect({ placeholder = 'Select address', ...props }: Props) {
  const { keyring } = useApi();
  const { myContracts } = useDatabase();

  const options = useMemo((): GroupBase<DropdownOption<string>>[] => {
    return [
      {
        label: 'My Accounts',
        options: createAccountOptions(keyring?.getPairs()),
      },
      ...(myContracts?.owned && myContracts.owned.length > 0
        ? [
            {
              label: 'Uploaded Contracts',
              options: (myContracts?.owned || []).map(({ name, address }) => ({
                label: name,
                value: address,
              })),
            },
          ]
        : []),
    ];
  }, [keyring, myContracts?.owned]);

  return <Select options={options} placeholder={placeholder} {...props} />;
}
Example #2
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
function isGroupedOptions<T>(
  options: ReactSelectProps<DropdownOption<T>, false>['options']
): options is GroupBase<DropdownOption<T>>[] {
  try {
    return !!options && (options as GroupBase<DropdownOption<T>>[])[0].options !== undefined;
  } catch (e) {
    return false;
  }
}
Example #3
Source File: Select.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
function Option({ label, value }: DropdownOption<string>) {
  return <Account className="p-1.5" name={label} value={value} />;
}
Example #4
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
function Control<T>(props: ControlProps<DropdownOption<T>, false>) {
  return <components.Control {...props} className={classes(props.className, 'min-h-0')} />;
}
Example #5
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
function Input<T>(props: InputProps<DropdownOption<T>, false>) {
  return (
    <components.Input
      {...props}
      inputClassName="dark:text-white outline-none border-none shadow-none focus:ring-transparent"
    />
  );
}
Example #6
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function Dropdown<T>({
  className = '',
  components = {},
  formatOptionLabel,
  isDisabled = false,
  isSearchable = false,
  onChange: _onChange,
  options = [],
  placeholder,
  value: _value,
}: DropdownProps<T>) {
  const onChange = useCallback(
    (option: DropdownOption<T> | null): void => {
      option && _onChange(option.value);
    },
    [_onChange]
  );

  const value = useMemo(() => {
    if (isGroupedOptions(options)) {
      return options
        .reduce((result: DropdownOption<T>[], { options }) => [...result, ...options], [])
        .find(({ value }) => value === _value);
    }

    return (options as DropdownOption<T>[]).find(({ value }) => value === _value);
  }, [options, _value]);

  return (
    <Select
      className={classes('dropdown', className)}
      classNamePrefix="dropdown"
      components={{ Control, DropdownIndicator, Input, Option, ...components }}
      formatOptionLabel={formatOptionLabel}
      isDisabled={isDisabled}
      isSearchable={isSearchable}
      onChange={onChange}
      options={options}
      placeholder={placeholder}
      styles={{
        dropdownIndicator: provided => ({ ...provided, padding: '0.25rem' }),
        input: provided => ({ ...provided, color: 'unset' }),
        option: () => ({}),
      }}
      value={value}
    />
  );
}
Example #7
Source File: dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function createConstructorOptions(data?: AbiConstructor[]): DropdownOption<number>[] {
  return (data || []).map((constructor, index) => ({
    label: <MessageSignature message={constructor} />,
    value: index,
  }));
}
Example #8
Source File: dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function createMessageOptions(data?: AbiMessage[]): DropdownOption<AbiMessage>[] {
  return (data || []).map(message => ({
    label: <MessageSignature message={message} />,
    value: message,
  }));
}
Example #9
Source File: dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function createAccountOptions(data: Partial<KeyringPair>[]): DropdownOption<string>[] {
  return data.map(pair => ({
    label: pair.meta?.name as string,
    value: pair.address || '',
  }));
}
Example #10
Source File: dropdown.tsx    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function createContractOptions(data: ContractDocument[]): DropdownOption<string>[] {
  return data.map(({ name, address }) => ({
    label: name,
    value: address,
  }));
}
Example #11
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 4 votes vote down vote up
function Option<T>({ children, ...props }: OptionProps<DropdownOption<T>, false>) {
  return (
    <components.Option {...props}>
      <span>{children}</span>
      {props.isSelected && <CheckIcon className="selected" />}
    </components.Option>
  );
}
Example #12
Source File: Dropdown.tsx    From contracts-ui with GNU General Public License v3.0 4 votes vote down vote up
function DropdownIndicator<T>(props: DropdownIndicatorProps<DropdownOption<T>, false>) {
  return (
    <components.DropdownIndicator {...props}>
      <ChevronDownIcon />
    </components.DropdownIndicator>
  );
}