-
Notifications
You must be signed in to change notification settings - Fork 8
feat(text-group): add TextGroupList feature #81 #625
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
4 commits
Select commit
Hold shift + click to select a range
ff01901
feat(text-group): add TextGroupList feature #81
airikej a8d117d
fix(text-group): improve tests #81
airikej 0056b9b
Merge branch 'rc' into fix/81-textgroup-multiple-rows-in-one-dl-element
airikej 321a3e1
fix(text-group): code rabbit fixes #81
airikej 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export * from './text-group'; | ||
| export * from './text-group-list/text-group-list'; |
134 changes: 134 additions & 0 deletions
134
src/tedi/components/content/text-group/text-group-list/text-group-list.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,134 @@ | ||
| import cn from 'classnames'; | ||
| import React from 'react'; | ||
|
|
||
| import { BreakpointSupport, useBreakpointProps } from '../../../../helpers'; | ||
| import { Label } from '../../label/label'; | ||
| import styles from '../text-group.module.scss'; | ||
|
|
||
| type TextAlign = 'left' | 'right'; | ||
|
|
||
| type TextGroupListBreakpointProps = | ||
| | { | ||
| /** | ||
| * Type of text group layout. | ||
| */ | ||
| type?: 'horizontal'; | ||
| /** | ||
| * Alignment for the label text. | ||
| * @default 'left' | ||
| */ | ||
| labelAlign?: TextAlign; | ||
| /** | ||
| * Width for the label column (e.g., `'200px'`, `'30%'`, or a `number` | ||
| * interpreted as a percent). | ||
| * @default 'auto' | ||
| */ | ||
| labelWidth?: string | number; | ||
| } | ||
| | { | ||
| /** | ||
| * Type of text group layout. | ||
| */ | ||
| type: 'vertical'; | ||
| /** | ||
| * Alignment for the label text. Vertical layout only supports left | ||
| * alignment — pass `'right'` only with `type: 'horizontal'`. | ||
| * @default 'left' | ||
| */ | ||
| labelAlign?: 'left'; | ||
| /** | ||
| * Width for the label column (e.g., `'200px'`, `'30%'`, or a `number` | ||
| * interpreted as a percent). | ||
| * @default 'auto' | ||
| */ | ||
| labelWidth?: string | number; | ||
| }; | ||
|
|
||
| export interface TextGroupListItem { | ||
| /** | ||
| * Label rendered as the `<dt>` for this row. Strings are auto-wrapped in | ||
| * `<Label>`; any other ReactNode is rendered as-is. | ||
| */ | ||
| label: React.ReactNode; | ||
| /** | ||
| * Value rendered as the `<dd>` for this row. | ||
| */ | ||
| value: React.ReactNode | React.ReactNode[]; | ||
| /** | ||
| * Per-row override of the list-level `labelAlign`. Falls back to the list's | ||
| * value when omitted. | ||
| */ | ||
| labelAlign?: TextAlign; | ||
| /** | ||
| * Per-row override of the list-level `labelWidth`. Falls back to the list's | ||
| * value when omitted. | ||
| */ | ||
| labelWidth?: string | number; | ||
| } | ||
|
|
||
| export type TextGroupListProps = BreakpointSupport<TextGroupListBreakpointProps> & { | ||
| /** | ||
| * Label / value pairs rendered together inside a **single** `<dl>` element, | ||
| * preserving the definition-list semantics that stacking N individual | ||
| * `<TextGroup>`s would break. | ||
| */ | ||
| items: TextGroupListItem[]; | ||
| /** | ||
| * Additional class name(s) to apply to the root `<dl>` element. | ||
| */ | ||
| className?: string; | ||
| }; | ||
|
|
||
| const renderLabelContent = (label: React.ReactNode): React.ReactNode => | ||
| typeof label === 'string' ? <Label>{label}</Label> : label; | ||
|
|
||
| const resolveLabelWidth = (labelWidth: string | number): string => | ||
| typeof labelWidth === 'number' ? `${labelWidth}%` : labelWidth; | ||
|
|
||
| /** | ||
| * Multi-row variant of `TextGroup`. Visually identical to stacking N | ||
| * `<TextGroup>` rows, but wraps every label / value pair in **one** semantic | ||
| * `<dl>` — so screen readers announce them as one definition list, not N | ||
| * fragments. Reuse the same `type` / `labelWidth` / `labelAlign` knobs as the | ||
| * single-pair component; per-row overrides are available via `items[i]`. | ||
| */ | ||
| export const TextGroupList = (props: TextGroupListProps): JSX.Element => { | ||
| const { getCurrentBreakpointProps } = useBreakpointProps(props.defaultServerBreakpoint); | ||
| const { | ||
| items, | ||
| labelWidth = 'auto', | ||
| className, | ||
| type = 'vertical', | ||
| labelAlign = 'left', | ||
| } = getCurrentBreakpointProps<TextGroupListProps>(props); | ||
|
|
||
| const listBEM = cn( | ||
| styles['tedi-text-group'], | ||
| styles['tedi-text-group--list'], | ||
| styles[`tedi-text-group--${type}`], | ||
| className | ||
| ); | ||
| const listLabelWidth = resolveLabelWidth(labelWidth); | ||
|
|
||
| return ( | ||
| <dl className={listBEM} style={{ '--label-width': listLabelWidth } as React.CSSProperties}> | ||
| {items.map((item, index) => { | ||
| const rowLabelAlign = item.labelAlign ?? labelAlign; | ||
| const rowStyle: React.CSSProperties | undefined = | ||
| item.labelWidth !== undefined | ||
| ? ({ '--label-width': resolveLabelWidth(item.labelWidth) } as React.CSSProperties) | ||
| : undefined; | ||
| return ( | ||
| <div key={index} className={styles['tedi-text-group__row']} style={rowStyle}> | ||
| <dt className={cn(styles['tedi-text-group__label'], styles[`tedi-text-group--align-${rowLabelAlign}`])}> | ||
| {renderLabelContent(item.label)} | ||
| </dt> | ||
| <dd className={cn(styles['tedi-text-group__value'])}>{item.value}</dd> | ||
| </div> | ||
| ); | ||
| })} | ||
| </dl> | ||
| ); | ||
| }; | ||
|
|
||
| TextGroupList.displayName = 'TextGroup.List'; |
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
Oops, something went wrong.
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.