Skip to content

Commit

Permalink
Merge pull request #6 from OldStarchy:OldStarchy/issue5
Browse files Browse the repository at this point in the history
Sort cards in card select form
  • Loading branch information
OldStarchy authored Aug 17, 2024
2 parents 39b61bd + e646c48 commit 4a52f99
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/components/form/SelectCardForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { Card } from '../../context/universe/Card';
import { Button } from '../common/Button';
import { Select } from '../common/Select';
Expand All @@ -13,6 +13,10 @@ export function SelectCardForm({
onSelectCard: (card: Card['name']) => void;
}) {
const [cardName, setCardName] = useState(options[0]);
const orderedOptions = useMemo(
() => [...options].sort((a, b) => a.localeCompare(b)),
[options],
);

useEffect(() => {
if (options.length === 0) {
Expand All @@ -38,7 +42,7 @@ export function SelectCardForm({
setCardName(e.target.value);
}}
>
{options.map((cardName) => (
{orderedOptions.map((cardName) => (
<option value={cardName} key={cardName}>
{cardName}
</option>
Expand Down

0 comments on commit 4a52f99

Please sign in to comment.