-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
feat: introduce StatelessSelect #7976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
efe445f
feat: introduce StatelessSelect
canerakdas 8372085
refactor: review updates
canerakdas 89e339e
chore: review updates
canerakdas d5f65d1
Merge branch 'main' into feat/stateless-select
canerakdas f5926f5
fix: use conditional rendering for select value display
canerakdas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
packages/ui-components/src/Common/Select/NoScriptSelect/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useId } from 'react'; | ||
|
||
import Select from '#ui/Common/Select'; | ||
import type { StatelessSelectProps } from '#ui/Common/Select/StatelessSelect'; | ||
import StatelessSelect from '#ui/Common/Select/StatelessSelect'; | ||
|
||
const WithNoScriptSelect = <T extends string>({ | ||
as, | ||
...props | ||
}: StatelessSelectProps<T>) => { | ||
const id = useId(); | ||
const selectId = `select-${id.replace(/[^a-zA-Z0-9]/g, '')}`; | ||
|
||
return ( | ||
<> | ||
<Select {...props} fallbackClass={selectId} /> | ||
<noscript> | ||
<style>{`.${selectId} { display: none!important; }`}</style> | ||
<StatelessSelect {...props} as={as} /> | ||
</noscript> | ||
</> | ||
); | ||
}; | ||
|
||
export default WithNoScriptSelect; |
128 changes: 128 additions & 0 deletions
128
packages/ui-components/src/Common/Select/StatelessSelect/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { ChevronDownIcon } from '@heroicons/react/24/solid'; | ||
import classNames from 'classnames'; | ||
import { useId, useMemo } from 'react'; | ||
|
||
import type { SelectGroup, SelectProps } from '#ui/Common/Select'; | ||
import type { LinkLike } from '#ui/types'; | ||
import { isStringArray, isValuesArray } from '#ui/util/array'; | ||
|
||
import styles from '../index.module.css'; | ||
|
||
type StatelessSelectConfig = { | ||
as?: LinkLike | 'div'; | ||
}; | ||
|
||
export type StatelessSelectProps<T extends string> = SelectProps<T> & | ||
StatelessSelectConfig; | ||
|
||
const StatelessSelect = <T extends string>({ | ||
values = [], | ||
defaultValue, | ||
placeholder, | ||
label, | ||
inline, | ||
className, | ||
ariaLabel, | ||
disabled = false, | ||
as: Component = 'div', | ||
}: StatelessSelectProps<T>) => { | ||
canerakdas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const id = useId(); | ||
|
||
const mappedValues = useMemo(() => { | ||
let mappedValues = values; | ||
|
||
if (isStringArray(mappedValues)) { | ||
mappedValues = mappedValues.map(value => ({ | ||
label: value, | ||
value: value, | ||
})); | ||
} | ||
|
||
if (isValuesArray(mappedValues)) { | ||
return [{ items: mappedValues }]; | ||
} | ||
|
||
return mappedValues as Array<SelectGroup<T>>; | ||
}, [values]) as Array<SelectGroup<T>>; | ||
|
||
// Find the current/default item to display in summary | ||
const currentItem = useMemo( | ||
() => | ||
mappedValues | ||
.flatMap(({ items }) => items) | ||
.find(item => item.value === defaultValue), | ||
[mappedValues, defaultValue] | ||
); | ||
|
||
return ( | ||
<div | ||
className={classNames( | ||
styles.select, | ||
styles.noscript, | ||
{ [styles.inline]: inline }, | ||
className | ||
)} | ||
> | ||
{label && ( | ||
<label className={styles.label} htmlFor={id}> | ||
{label} | ||
</label> | ||
)} | ||
|
||
<details className={styles.trigger} id={id}> | ||
<summary | ||
className={styles.summary} | ||
aria-label={ariaLabel} | ||
aria-disabled={disabled} | ||
> | ||
{currentItem && ( | ||
<span className={styles.selectedValue}> | ||
{currentItem.iconImage} | ||
<span>{currentItem.label}</span> | ||
</span> | ||
)} | ||
{!currentItem && ( | ||
<span className={styles.placeholder}>{placeholder}</span> | ||
)} | ||
<ChevronDownIcon className={styles.icon} /> | ||
</summary> | ||
|
||
<div | ||
className={classNames(styles.dropdown, { [styles.inline]: inline })} | ||
> | ||
{mappedValues.map(({ label: groupLabel, items }, groupKey) => ( | ||
<div | ||
key={groupLabel?.toString() ?? groupKey} | ||
className={styles.group} | ||
> | ||
{groupLabel && ( | ||
<div className={classNames(styles.item, styles.label)}> | ||
{groupLabel} | ||
</div> | ||
)} | ||
|
||
{items.map( | ||
({ value, label, iconImage, disabled: itemDisabled }) => ( | ||
<Component | ||
key={value} | ||
href={value} | ||
className={classNames(styles.item, styles.text, { | ||
[styles.disabled]: itemDisabled || disabled, | ||
[styles.selected]: value === defaultValue, | ||
})} | ||
aria-disabled={itemDisabled || disabled} | ||
> | ||
{iconImage} | ||
<span>{label}</span> | ||
</Component> | ||
) | ||
)} | ||
</div> | ||
))} | ||
</div> | ||
</details> | ||
</div> | ||
); | ||
}; | ||
|
||
export default StatelessSelect; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const isStringArray = ( | ||
values: Array<unknown> | ||
): values is Array<string> => | ||
Boolean(values[0] && typeof values[0] === 'string'); | ||
|
||
export const isValuesArray = <T>(values: Array<unknown>): values is Array<T> => | ||
Boolean(values[0] && typeof values[0] === 'object' && 'value' in values[0]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.