Skip to content

Commit

Permalink
Let action change between button and link
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman committed Mar 31, 2021
1 parent 5e15283 commit c5b55c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ import { RenderResult } from '@dojo/framework/core/interfaces';
import { create, tsx } from '@dojo/framework/core/vdom';
import { LinkProperties } from '@dojo/framework/routing/interfaces';
import Link from '@dojo/framework/routing/Link';
import ActionButton, { ActionButtonProperties } from '../action-button';
import theme from '../middleware/theme';
import * as css from '../theme/default/header.m.css';

export interface ActionProperties extends LinkProperties {}
export type ActionProperties = LinkProperties | ActionButtonProperties;

const actionFactory = create({ theme }).properties<ActionProperties>();

function IsLink(properties: ActionProperties): properties is LinkProperties {
return Boolean(properties && properties.hasOwnProperty('to'));
}

export const Action = actionFactory(({ properties, children, middleware: { theme } }) => {
const themedCss = theme.classes(css);
const props = properties();

const action = (
<Link {...properties()} classes={[theme.variant(), themedCss.action]}>
{children()}
</Link>
);

return action;
if (IsLink(props)) {
return (
<Link {...props} classes={[theme.variant(), themedCss.action]}>
{children()}
</Link>
);
} else {
return <ActionButton {...props}>{children()}</ActionButton>;
}
});

export interface HeaderProperties {
Expand Down
6 changes: 6 additions & 0 deletions src/header/tests/unit/Header.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Link from '@dojo/framework/routing/Link';
import assertionTemplate from '@dojo/framework/testing/harness/assertionTemplate';
import harness from '@dojo/framework/testing/harness/harness';
import Header, { Action } from '../..';
import ActionButton from '../../../action-button';
import * as classes from '../../../theme/default/header.m.css';

const baseTemplate = assertionTemplate(() => (
Expand Down Expand Up @@ -118,6 +119,11 @@ describe('HeaderToolbar', () => {
);
});

it('renders action as button', () => {
const h = harness(() => <Action name="testButton">test</Action>);
h.expect(assertionTemplate(() => <ActionButton name="testButton">test</ActionButton>));
});

it('Renders a sticky header', () => {
const h = harness(() => (
<Header sticky>
Expand Down

0 comments on commit c5b55c8

Please sign in to comment.