Skip to content

Commit

Permalink
Add a For component
Browse files Browse the repository at this point in the history
  • Loading branch information
efoken committed Dec 6, 2024
1 parent 8ac2600 commit ec48bb4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
23 changes: 23 additions & 0 deletions packages/components/src/For/For.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { AnyObject } from '@react-universal/utils';

export interface ForProps<T> {
/**
* The render function to render each item in the array.
*/
children: (item: Exclude<T, undefined>, index: number) => React.ReactNode;
/**
* The array to iterate overs.
*/
each: T[] | readonly T[] | undefined;
/**
* The fallback content to render when the array is empty.
*/
fallback?: React.ReactNode;
}

export const For = <T extends string | number | AnyObject | undefined>({
each,
fallback,
children,
}: ForProps<T>): React.ReactNode =>
each?.length === 0 ? fallback || null : each?.map(children as any);
2 changes: 2 additions & 0 deletions packages/components/src/For/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { For } from './For';
export type { ForProps } from './For';
6 changes: 3 additions & 3 deletions packages/components/src/Show/Show.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { isValidElement } from 'react';

export interface ShowProps<T> {
/**
* The children to render if `when` is `true`
* The children to render if `when` is `true`.
*/
children: React.ReactNode | ((props: T) => React.ReactNode);
/**
* The fallback content to render if `when` is `false`
* The fallback content to render if `when` is `false`.
*/
fallback?: React.ReactNode;
/**
* If `true`, it'll render the `children` prop
* If `true`, it'll render the `children` prop.
*/
when: T | null | undefined;
}
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './Box';
export * from './Button';
export * from './Container';
export * from './For';
export * from './Heading';
export * from './Image';
export * from './Link';
Expand Down

0 comments on commit ec48bb4

Please sign in to comment.