mirror of https://github.com/Nezreka/SoulSync.git
- move the conditional rendering helper into components/primitives - use it in the issues board and issue domain host - keep the issue page and host easier to scan without repeated null branchespull/388/head
parent
a4a4c0f12d
commit
adb6426a2f
@ -0,0 +1 @@
|
||||
export { Show } from './show';
|
||||
@ -0,0 +1,33 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { Show } from './show';
|
||||
|
||||
describe('Show', () => {
|
||||
it('renders children when the condition is true', () => {
|
||||
render(
|
||||
<Show when={true}>
|
||||
<span>Visible</span>
|
||||
</Show>,
|
||||
);
|
||||
|
||||
expect(screen.getByText('Visible')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders fallback when the condition is false', () => {
|
||||
render(
|
||||
<Show fallback={<span>Hidden</span>} when={false}>
|
||||
<span>Visible</span>
|
||||
</Show>,
|
||||
);
|
||||
|
||||
expect(screen.getByText('Hidden')).toBeInTheDocument();
|
||||
expect(screen.queryByText('Visible')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('supports render-prop children', () => {
|
||||
render(<Show when="Ada">{(name) => <span>{name}</span>}</Show>);
|
||||
|
||||
expect(screen.getByText('Ada')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,23 @@
|
||||
import type { ReactNode } from 'react';
|
||||
|
||||
type ShowChildren<T> = ReactNode | ((value: NonNullable<T>) => ReactNode);
|
||||
|
||||
export function Show<T>({
|
||||
fallback = null,
|
||||
children,
|
||||
when,
|
||||
}: {
|
||||
children: ShowChildren<T>;
|
||||
fallback?: ReactNode;
|
||||
when: T;
|
||||
}) {
|
||||
if (!when) {
|
||||
return <>{fallback}</>;
|
||||
}
|
||||
|
||||
if (typeof children === 'function') {
|
||||
return <>{(children as (value: NonNullable<T>) => ReactNode)(when as NonNullable<T>)}</>;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
Loading…
Reference in new issue