Skip to content

Commit

Permalink
Add side note to select field options
Browse files Browse the repository at this point in the history
  • Loading branch information
emrberk committed Mar 4, 2024
1 parent cc21e0e commit 4f54ffd
Show file tree
Hide file tree
Showing 38 changed files with 52 additions and 18 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/ui/__stories__/SelectField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ WithHelperText.args = {
helperText: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
}

export const WithSideNote = Template.bind({})
WithSideNote.args = {
options: options.map((o) => ({ ...o, sideNote: 'Side Note' })),
}

export const Clearable = Template.bind({})
Clearable.args = {
isClearable: true,
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/Select/Common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Icon } from '../Icon'
import { IconButton } from '../IconButton'

import styles from './Common.module.scss'
import { SelectFieldOption } from './helpers'
import { TruncatedText } from '../TruncatedText'

const DropdownIndicator: typeof rsComponents.DropdownIndicator = ({ selectProps }) => (
Expand Down Expand Up @@ -55,7 +56,7 @@ const MenuList: typeof rsComponents.MenuList = (props) => {

const Option: typeof rsComponents.Option = (props) => (
<rsComponents.Option {...props}>
<TruncatedText text={props.label} />
<TruncatedText text={props.label} sideNote={(props.data as SelectFieldOption<string>)?.sideNote} />
</rsComponents.Option>
)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -64,7 +65,7 @@ const IndicatorSeparator: typeof rsComponents.IndicatorSeparator = () => null

const SingleValue: typeof rsComponents.SingleValue = ({ children, ...props }) => (
<rsComponents.SingleValue {...props}>
<TruncatedText text={children as string} />
<TruncatedText text={children as string} sideNote={(props.data as SelectFieldOption<string>)?.sideNote} />
</rsComponents.SingleValue>
)

Expand Down
7 changes: 7 additions & 0 deletions packages/ui/src/Select/SelectField.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ $menuMargin: c.$grid * 3;
$selectContainer: ':global .hz-select-field';
$selectControl: ':global .hz-select-field__control';
$selectValueContainer: ':global .hz-select-field__value-container';
$selectSingleValue: ':global .hz-select-field__single-value';
$selectMenuPortal: ':global .hz-select-field__menu-portal';
$selectMenu: ':global .hz-select-field__menu';
$selectMenuList: ':global .hz-select-field__menu-list';
Expand All @@ -24,6 +25,12 @@ $selectGroupHeading: ':global .hz-select-field__group-heading';
}
}

&.withSideNote {
#{$selectSingleValue} {
width: 100%;
}
}

.helperText {
margin-left: c.$grid * 1.5;
display: block;
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/Select/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export const SelectField = <V extends string | number = string>(props: SelectFie
[styles.hasIcon]: !!iconLeft,
[styles.small]: size === 'small',
[styles.withError]: 'error' in props,
[styles.withSideNote]: options.some((option) => 'sideNote' in option),
},
className,
)}
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/Select/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { canUseDOM } from '../utils/ssr'
export type SelectFieldOption<V> = {
label: string
value: V
sideNote?: string
}

export type SelectFieldOptionsMap<V> = {
Expand Down
18 changes: 16 additions & 2 deletions packages/ui/src/TruncatedText.module.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
@use '../styles/helpers' as h;
@use '../styles/constants' as c;

.truncatedText {
@include h.overflowEllipsis;
.container {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;

.truncatedText {
@include h.overflowEllipsis;
}

.sideNote {
flex-shrink: 0;
color: c.$colorTextSecondary;
margin-left: c.$grid * 2;
}
}
33 changes: 19 additions & 14 deletions packages/ui/src/TruncatedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import styles from './TruncatedText.module.scss'

interface TruncatedTextProps {
text: ReactText | ((...arg: unknown[]) => string) | ReactElement
sideNote?: string
// Pass a new value to trigger a force re-render
forceUpdateToken?: ReactText | boolean
className?: string
Expand All @@ -25,6 +26,7 @@ export const TruncatedText: FC<TruncatedTextProps> = ({
tooltipVisible,
tooltipPlacement = 'top',
hoverAbleTooltip,
sideNote,
}) => {
const textRef = useRef<HTMLDivElement>(null)
const [tooltip, setTooltip] = useState<ReactChild | undefined>()
Expand All @@ -42,19 +44,22 @@ export const TruncatedText: FC<TruncatedTextProps> = ({
}, [text, forceUpdateToken])

return (
<Tooltip
id={idTooltip}
content={tooltip}
visible={tooltipVisible}
placement={tooltipPlacement}
updateToken={forceUpdateToken}
hoverAbleTooltip={hoverAbleTooltip}
>
{(ref) => (
<div ref={mergeRefs([textRef, ref])} className={cn(styles.truncatedText, className)}>
<span aria-labelledby={tooltip ? idTooltip : undefined}>{text}</span>
</div>
)}
</Tooltip>
<div className={styles.container}>
<Tooltip
id={idTooltip}
content={tooltip}
visible={tooltipVisible}
placement={tooltipPlacement}
updateToken={forceUpdateToken}
hoverAbleTooltip={hoverAbleTooltip}
>
{(ref) => (
<div ref={mergeRefs([textRef, ref])} className={cn(styles.truncatedText, className)}>
<span aria-labelledby={tooltip ? idTooltip : undefined}>{text}</span>
</div>
)}
</Tooltip>
{sideNote && <span className={styles.sideNote}>{sideNote}</span>}
</div>
)
}

0 comments on commit 4f54ffd

Please sign in to comment.