Skip to content

Commit

Permalink
fix: only valid addresses to onChange
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat committed Jun 25, 2024
1 parent 8cfa9bf commit a020497
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/library/Inputs/AccountId32/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const AccountId32 = ({
const inputRef = useRef<HTMLInputElement>(null);

// Get input value from input meta.
const inputMetaValue = getInputMetaValue(tabId, inputId);
const inputMetaValue = getInputMetaValue(tabId, inputId) || '';

// The current selected address. If input meta value is not present, use the the first imported
// account, if any.
Expand All @@ -63,11 +63,17 @@ export const AccountId32 = ({

// Handle input value change.
const handleInputChange = (val: string) => {
// Update input search value.
setInputMetaValue(tabId, inputId, val);
setSearchValue(val);

// Get the first address from `accounts` that starts with `val`, or fall back to default.
const address =
accounts.find(({ name }) => name.startsWith(val))?.address ||
defaultAddress ||
'';

if (onChange !== undefined) {
onChange(val);
onChange(address);
}
};

Expand All @@ -80,9 +86,6 @@ export const AccountId32 = ({
}
};

// The current search value of the input.
const [searchValue, setSearchValue] = useState<string>('');

// Pallet selection open.
const [dropdownOpen, setDropdownOpenState] = useState<boolean>(false);
const dropdownOpenRef = useRef(dropdownOpen);
Expand All @@ -94,11 +97,13 @@ export const AccountId32 = ({

// Filter accounts based on search term, if present.
const filteredAccounts =
searchValue !== ''
inputMetaValue !== ''
? accounts.filter(
({ address, name }) =>
name.toLowerCase().includes(formatInputString(searchValue, true)) ||
address.toLowerCase().includes(searchValue.toLowerCase())
name
.toLowerCase()
.includes(formatInputString(inputMetaValue, true)) ||
address.toLowerCase().includes(inputMetaValue.toLowerCase())
)
: accounts;

Expand Down

0 comments on commit a020497

Please sign in to comment.