Skip to content

Commit 4fec838

Browse files
committed
Rewrite SortButton
Includes: - Replace previously hand-made button with button component from Geist UI. - Change text depending on sorting condition (sort / sorting / sorted). - Change styles depending on sorting state. - Only execute clickAction prop if sorting state is not sorted. - If sorting state is sorted, display a checkmark icon alongside the text.
1 parent 2c9d194 commit 4fec838

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

components/SortButton.tsx

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
1-
import { useState } from "react"
1+
import { useEffect, useState } from "react"
2+
import { Button, Spacer } from "@geist-ui/react"
3+
import { Check } from "@geist-ui/react-icons"
24

3-
export const SortButton: React.FC<{ clickAction: () => void }> = (props) => {
4-
const [text, setText] = useState("Sort!")
5-
const { clickAction } = props
5+
type SortingState = "Sort" | "Sorting" | "Sorted"
6+
7+
interface SortButtonProps {
8+
clickAction: () => void
9+
sortState: SortingState
10+
}
11+
12+
export const SortButton: React.FC<SortButtonProps> = (props) => {
13+
const { sortState, clickAction } = props
14+
const [text, setText] = useState<SortingState>("Sort")
15+
16+
useEffect(() => {
17+
if (sortState !== text) {
18+
setText(sortState)
19+
}
20+
}, [sortState])
21+
22+
let isSorting = text === "Sorting"
23+
let isSorted = text === "Sorted"
24+
const onClick = () => {
25+
if (isSorted) return
26+
clickAction()
27+
}
628
return (
7-
<button
8-
disabled={text === "Sorting!"}
9-
className="border rounded-md text-white bg-black font-bold shadow-md px-5 py-2 mx-auto w-32 block mt-8 transition-all disabled:opacity-60 disabled:bg-gray-400"
10-
onClick={() => {
11-
clickAction()
12-
setText("Sorting!")
13-
}}>
29+
<Button
30+
loading={isSorting}
31+
shadow
32+
type={isSorted ? "success" : "secondary"}
33+
onClick={onClick}
34+
style={{ display: "block", margin: "0 auto" }}>
35+
{isSorted && (
36+
<>
37+
<Check size={20} />
38+
<Spacer inline x={0.2} />
39+
</>
40+
)}
1441
{text}
15-
</button>
42+
</Button>
1643
)
1744
}

0 commit comments

Comments
 (0)