Skip to content

Commit

Permalink
refactor(web): use label in Dropdown selected option display
Browse files Browse the repository at this point in the history
  • Loading branch information
xFJA committed Aug 8, 2024
1 parent 8dd020e commit e608a7d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions apps/web/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export interface Option {

export interface DropdownProps {
options: Option[];
selected: string | number;
selected: Option;
width?: string;
onChange(newValue: string | number): void;
onChange(newOption: Option): void;
}

const DEFAULT_WIDTH = "w-32";
Expand All @@ -37,7 +37,7 @@ export const Dropdown: React.FC<DropdownProps> = function ({
} cursor-pointer rounded-lg border border-transparent bg-controlBackground-light pl-2 pr-8 text-left text-sm shadow-md hover:border hover:border-controlBackground-light focus:outline-none focus-visible:border-indigo-500 focus-visible:ring-2 focus-visible:ring-white active:border-controlBorderHighlight-dark ui-open:border-controlActive-light dark:bg-controlBackground-dark dark:hover:border-controlBorderHighlight-dark dark:ui-open:border-controlActive-dark`}
>
<span className="block truncate align-middle font-normal">
{selected}
{selected.label}
</span>
<span className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-2">
<ChevronUpDownIcon
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/components/Dropdown/components/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ListboxOption } from "@headlessui/react";

import type { Option as OptionProps } from "../Dropdown";

export const Option: React.FC<OptionProps> = function ({ label, value }) {
export const Option: React.FC<OptionProps> = function (props) {
return (
<ListboxOption
className={({ focus }) =>
Expand All @@ -12,14 +12,14 @@ export const Option: React.FC<OptionProps> = function ({ label, value }) {
: "text-contentSecondary-light dark:text-contentSecondary-dark"
}`
}
value={value}
value={props}
>
{({ selected }) => (
<div className="flex items-center">
<span
className={`block truncate text-sm ${selected ? "font-bold" : ""}`}
>
{label}
{props.label}
</span>
</div>
)}
Expand Down
9 changes: 6 additions & 3 deletions apps/web/src/components/Layouts/PaginatedListLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const PaginatedListLayout: FC<PaginatedListLayoutProps> = function ({
const hasItems = !items || items.length;

const handlePageSizeSelection = useCallback<DropdownProps["onChange"]>(
(newPageSize: number) =>
({ value: newPageSize }) =>
void router.push({
pathname: router.pathname,
query: {
Expand All @@ -56,7 +56,10 @@ export const PaginatedListLayout: FC<PaginatedListLayoutProps> = function ({
* Update the selected page to a lower value if we require less pages to show the
* new amount of elements per page.
*/
p: Math.min(Math.ceil(totalItems ?? 0 / newPageSize), page),
p: Math.min(
Math.ceil(totalItems ?? 0 / (newPageSize as number)),
page
),
ps: newPageSize,
},
}),
Expand Down Expand Up @@ -116,7 +119,7 @@ export const PaginatedListLayout: FC<PaginatedListLayoutProps> = function ({
Displayed items:
<Dropdown
options={PAGE_SIZES_OPTIONS}
selected={pageSize}
selected={{ value: pageSize, label: pageSize.toString() }}
width="w-full"
onChange={handlePageSizeSelection}
/>
Expand Down
9 changes: 6 additions & 3 deletions apps/web/src/components/PaginatedTable/PaginatedTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const PaginatedTable: FC<PaginatedTableProps> = function ({
: undefined;

const handlePageSizeSelection = useCallback<DropdownProps["onChange"]>(
(newPageSize: number) =>
({ value: newPageSize }) =>
void router.push({
pathname: router.pathname,
query: {
Expand All @@ -79,7 +79,10 @@ export const PaginatedTable: FC<PaginatedTableProps> = function ({
* Update the selected page to a lower value if we require less pages to show the
* new amount of elements per page.
*/
p: Math.min(Math.ceil(totalItems ?? 0 / newPageSize), page),
p: Math.min(
Math.ceil(totalItems ?? 0 / (newPageSize as number)),
page
),
ps: newPageSize,
},
}),
Expand Down Expand Up @@ -136,7 +139,7 @@ export const PaginatedTable: FC<PaginatedTableProps> = function ({
Displayed items:
<Dropdown
options={PAGE_SIZES_OPTIONS}
selected={pageSize}
selected={{ value: pageSize, label: pageSize.toString() }}
width="w-full"
onChange={handlePageSizeSelection}
/>
Expand Down
7 changes: 5 additions & 2 deletions apps/web/src/pages/blob/[hash].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ const Blob: NextPage = function () {
</div>
<Dropdown
options={blobViewModesOptions}
selected={selectedBlobViewMode}
onChange={(newViewMode) =>
selected={{
value: selectedBlobViewMode,
label: selectedBlobViewMode,
}}
onChange={({ value: newViewMode }) =>
setSelectedBlobViewMode(newViewMode as BlobViewMode)
}
/>
Expand Down

0 comments on commit e608a7d

Please sign in to comment.