Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

[FSSS-164] Extract UISelect from Sort to its own component #299

Merged
merged 8 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions src/components/search/Sort/Sort.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { useSearch } from '@faststore/sdk'
import React from 'react'
import { Select as UISelect } from '@faststore/ui'
import { CaretDown as CaretDownIcon } from 'phosphor-react'

import './sort.scss'
import Select from 'src/components/ui/Select'

const OptionsMap = {
price_desc: 'Price, descending',
Expand All @@ -25,23 +22,15 @@ function Sort() {
} = useSearch()

return (
<div className="sort / title-small">
<label htmlFor="select-sort">Sort by</label>
<UISelect
data-testid="search-sort"
onChange={(e) => setSort(keys[e.target.selectedIndex])}
value={sort}
aria-label="Product Sort"
id="select-sort"
>
{keys.map((key) => (
<option key={key} value={key}>
{OptionsMap[key]}
</option>
))}
</UISelect>
<CaretDownIcon size={18} weight="bold" />
</div>
<Select
id="sort-select"
className="sort / title-small"
hellofanny marked this conversation as resolved.
Show resolved Hide resolved
labelText="Sort by"
options={OptionsMap}
onChange={(e) => setSort(keys[e.target.selectedIndex])}
value={sort}
testId="search-sort"
/>
)
}

Expand Down
56 changes: 56 additions & 0 deletions src/components/ui/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react'
import { Select as UISelect } from '@faststore/ui'
import type { SelectProps } from '@faststore/ui'
import { CaretDown as CaretDownIcon } from 'phosphor-react'

import './select.scss'

interface UISelectProps extends SelectProps {
/*
* Redefines the id property to be required when using the Select component. The
* id will be used to link the UISelect component and its label.
*/
id: string
/*
* Defines the options available in the select. The SelectOptions object
* keys are the property names, while the values correspond to the text that
* will be displayed in the UI
*/
options: Record<string, string>
/*
* Specifies the text that will be displayed in the label right next to the Select.
* If omitted, the label will not be rendered.
*/
labelText?: string
heitorado marked this conversation as resolved.
Show resolved Hide resolved
}

export default function Select({
id,
className,
options,
onChange,
labelText,
value,
'aria-label': ariaLabel,
testId,
}: UISelectProps) {
return (
<div data-select className={className}>
{labelText && <label htmlFor={id}>{labelText}</label>}
<UISelect
data-testid={testId}
onChange={onChange}
value={value}
aria-label={ariaLabel}
id={id}
>
{Object.keys(options).map((key) => (
<option key={key} value={key}>
{options[key]}
</option>
))}
</UISelect>
<CaretDownIcon size={18} weight="bold" />
</div>
)
}
1 change: 1 addition & 0 deletions src/components/ui/Select/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './Select'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import "../../../styles/scaffold.scss";

.sort {
[data-select] {
position: relative;
display: flex;
align-items: center;
Expand Down