|
3 | 3 | type ComponentClass, |
4 | 4 | type HTMLProps, |
5 | 5 | type ReactNode, |
| 6 | + type JSX, |
6 | 7 | } from 'react'; |
7 | 8 |
|
8 | 9 | /** |
@@ -358,5 +359,97 @@ body.dark-mode { |
358 | 359 | */ |
359 | 360 | export class BodyClass extends ReactComponent<ElementClassProps> {} |
360 | 361 |
|
| 362 | +/** |
| 363 | +Inserts a separator between each element of the children. |
| 364 | +
|
| 365 | +@param children - The elements to intersperse with separators. |
| 366 | +@param separator - The separator to insert between elements. Can be a ReactNode or a function that returns a ReactNode. |
| 367 | +
|
| 368 | +@example |
| 369 | +``` |
| 370 | +import {intersperse} from 'react-extras'; |
| 371 | +
|
| 372 | +const items = ['Apple', 'Orange', 'Banana']; |
| 373 | +const list = intersperse( |
| 374 | + items.map(item => <li key={item}>{item}</li>), |
| 375 | + ', ' |
| 376 | +); |
| 377 | +// => [<li>Apple</li>, ', ', <li>Orange</li>, ', ', <li>Banana</li>] |
| 378 | +``` |
| 379 | +
|
| 380 | +@example |
| 381 | +``` |
| 382 | +import {intersperse} from 'react-extras'; |
| 383 | +
|
| 384 | +const items = ['Apple', 'Orange', 'Banana']; |
| 385 | +const list = intersperse( |
| 386 | + items.map(item => <li key={item}>{item}</li>), |
| 387 | + (index, count) => index === count - 2 ? ' and ' : ', ' |
| 388 | +); |
| 389 | +// => [<li>Apple</li>, ', ', <li>Orange</li>, ' and ', <li>Banana</li>] |
| 390 | +``` |
| 391 | +*/ |
| 392 | +export function intersperse( |
| 393 | + children: ReactNode, |
| 394 | + separator?: ReactNode | ((index: number, count: number) => ReactNode) |
| 395 | +): ReactNode[]; |
| 396 | + |
| 397 | +type JoinProps = { |
| 398 | + /** |
| 399 | + The separator to insert between elements. |
| 400 | +
|
| 401 | + Can be a ReactNode or a function that returns a ReactNode. |
| 402 | +
|
| 403 | + @default ', ' |
| 404 | + */ |
| 405 | + readonly separator?: ReactNode | ((index: number, count: number) => ReactNode); |
| 406 | + |
| 407 | + /** |
| 408 | + The elements to join with separators. |
| 409 | + */ |
| 410 | + readonly children: ReactNode; |
| 411 | +}; |
| 412 | + |
| 413 | +/** |
| 414 | +React component that renders the children with a separator between each element. |
| 415 | +
|
| 416 | +@example |
| 417 | +``` |
| 418 | +import {Join} from 'react-extras'; |
| 419 | +
|
| 420 | +<Join> |
| 421 | + <li>Apple</li> |
| 422 | + <li>Orange</li> |
| 423 | + <li>Banana</li> |
| 424 | +</Join> |
| 425 | +// => <li>Apple</li>, <li>Orange</li>, <li>Banana</li> |
| 426 | +``` |
| 427 | +
|
| 428 | +@example |
| 429 | +``` |
| 430 | +import {Join} from 'react-extras'; |
| 431 | +
|
| 432 | +<Join separator=" | "> |
| 433 | + <a href="#">Home</a> |
| 434 | + <a href="#">About</a> |
| 435 | + <a href="#">Contact</a> |
| 436 | +</Join> |
| 437 | +// => <a href="#">Home</a> | <a href="#">About</a> | <a href="#">Contact</a> |
| 438 | +``` |
| 439 | +
|
| 440 | +@example |
| 441 | +``` |
| 442 | +import {Join} from 'react-extras'; |
| 443 | +
|
| 444 | +<Join separator={(index, count) => index === count - 2 ? ' and ' : ', '}> |
| 445 | + <span>Apple</span> |
| 446 | + <span>Orange</span> |
| 447 | + <span>Banana</span> |
| 448 | +</Join> |
| 449 | +// => <span>Apple</span>, <span>Orange</span> and <span>Banana</span> |
| 450 | +``` |
| 451 | +*/ |
| 452 | +export function Join(props: JoinProps): JSX.Element; |
| 453 | + |
361 | 454 | export {default as classNames} from '@sindresorhus/class-names'; |
362 | 455 | export {default as autoBind} from 'auto-bind/react'; |
0 commit comments