Skip to content

Commit

Permalink
moved badge to Generic props
Browse files Browse the repository at this point in the history
  • Loading branch information
dfee committed Jun 13, 2019
1 parent 0b8d7fc commit eb39273
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 158 deletions.
88 changes: 88 additions & 0 deletions src/base/helpers/__tests__/badge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { makePropTypes, makeValidatingTransform } from "src/base/helpers/badge";
import { DEFAULTS } from "src/base/helpers/variables";
import { tuple } from "../../../utils";

import {
validateBoolPropType,
validateOneOfPropType,
validateStringOrNumberPropType,
} from "src/__tests__/testing";
import {
testItShouldNotSetClassNameOnEmpty,
testItShouldPreserveCustomClassName,
testItShouldPreserveUnknown,
testItShouldUseDefaultLocationProp,
} from "./testing";

const CNAME = "foo";
const LOC = "prop";

describe("Badge helpers", () => {
const propTypes = makePropTypes();
const vtfunc = makeValidatingTransform();

describe("propTypes", () => {
validateStringOrNumberPropType(propTypes, "badge");
validateOneOfPropType(propTypes, "badgeColor", DEFAULTS.colors);
validateBoolPropType(propTypes, "badgeOutlined");
validateBoolPropType(propTypes, "badgeRounded");
validateOneOfPropType(propTypes, "badgeSize", DEFAULTS.badgeSizes);
testItShouldUseDefaultLocationProp(vtfunc, {
badgeSize: "__UNKNOWN",
});

describe("custom", () => {
const customBadgeSizes = tuple("a", "b");
const customPropTypes = makePropTypes({
badgeSizes: customBadgeSizes,
});

validateOneOfPropType(customPropTypes, "badgeSize", customBadgeSizes);
});
});

describe("transform", () => {
testItShouldPreserveUnknown(vtfunc);
testItShouldNotSetClassNameOnEmpty(vtfunc);
testItShouldPreserveCustomClassName(vtfunc);

it("should should get badge className and data-badge", () => {
expect(vtfunc({ badge: "foobar" }, CNAME, LOC)).toEqual({
className: "badge",
"data-badge": "foobar",
});
});

DEFAULTS.colors.map(color => {
it(`should be ${color}`, () => {
expect(vtfunc({ badgeColor: color }, CNAME, LOC)).toEqual({
className: `has-badge-${color}`,
});
});
});

[false, true].map(badgeOutlined => {
it(`should ${badgeOutlined ? "" : "not "}be badgeOutlined`, () => {
expect(vtfunc({ badgeOutlined }, CNAME, LOC)).toEqual({
className: badgeOutlined ? "has-badge-outlined" : "",
});
});
});

[false, true].map(badgeRounded => {
it(`should ${badgeRounded ? "" : "not "}be badgeRounded`, () => {
expect(vtfunc({ badgeRounded }, CNAME, LOC)).toEqual({
className: badgeRounded ? "has-badge-rounded" : "",
});
});
});

DEFAULTS.badgeSizes.map(badgeSize => {
it(`should be size ${badgeSize}`, () => {
expect(vtfunc({ badgeSize }, CNAME, LOC)).toEqual({
className: `has-badge-${badgeSize}`,
});
});
});
});
});
14 changes: 14 additions & 0 deletions src/base/helpers/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe("Modifiers", () => {
testItShouldNotSetClassNameOnEmpty(rvtfunc);
testItShouldPreserveCustomClassName(rvtfunc);

it("should apply badge transforms", () => {
expect(rvtfunc({ badge: "asdf" }, CNAME, LOC)).toEqual({
className: "badge",
"data-badge": "asdf",
});
});

it("should apply float transforms", () => {
expect(rvtfunc({ clearfix: true }, CNAME, LOC)).toEqual({
className: "is-clearfix",
Expand All @@ -35,6 +42,13 @@ describe("Modifiers", () => {
});
});

it("should apply tooltip transforms", () => {
expect(rvtfunc({ tooltip: "asdf" }, CNAME, LOC)).toEqual({
className: "tooltip",
"data-tooltip": "asdf",
});
});

it("should apply typography transforms", () => {
expect(rvtfunc({ textSize: 1 }, CNAME, LOC)).toEqual({
className: "is-size-1",
Expand Down
2 changes: 2 additions & 0 deletions src/base/helpers/__tests__/tooltip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
validateBoolPropType,
validateOneOfPropType,
validatePropType,
validateStringOrNumberPropType,
} from "src/__tests__/testing";
import {
testItShouldNotSetClassNameOnEmpty,
Expand All @@ -25,6 +26,7 @@ describe("Tooltip helpers", () => {
const vtfunc = makeValidatingTransform();

describe("propTypes", () => {
validateStringOrNumberPropType(propTypes, "tooltip");
validateBoolPropType(propTypes, "tooltipActive");
validateOneOfPropType(propTypes, "tooltipColor", DEFAULTS.colors);
validateBoolPropType(propTypes, "tooltipMultiline");
Expand Down
59 changes: 59 additions & 0 deletions src/base/helpers/badge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import classNames from "classnames";
import PropTypes from "prop-types";

import {
makePropTypesFactory,
makeValidatingTransformFactory,
TransformFunction,
} from "./factory";
import { DEFAULTS, Variables } from "./variables";

export type BadgeHelpersProps = Partial<{
badge: number | string;
badgeColor: Variables["colors"];
badgeOutlined: boolean;
badgeRounded: boolean;
badgeSize: (typeof DEFAULTS["badgeSizes"])[number];
}>;

// Factories
export const makePropTypes = makePropTypesFactory(vars => ({
badge: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
badgeColor: PropTypes.oneOf(vars.colors),
badgeOutlined: PropTypes.bool,
badgeRounded: PropTypes.bool,
badgeSize: PropTypes.oneOf(vars.badgeSizes),
}));

export const transform: TransformFunction<BadgeHelpersProps> = props => {
const {
badge,
badgeColor,
badgeOutlined,
badgeRounded,
badgeSize,
...rest
} = props;

rest.className = classNames(
{
badge,
[`has-badge-${badgeColor}`]: badgeColor,
"has-badge-outlined": badgeOutlined,
"has-badge-rounded": badgeRounded,
[`has-badge-${badgeSize}`]: badgeSize,
},
rest.className,
);

if (badge !== undefined) {
rest["data-badge"] = badge;
}

return rest;
};

export const makeValidatingTransform = makeValidatingTransformFactory(
makePropTypes,
transform,
);
8 changes: 7 additions & 1 deletion src/base/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Prefer } from "../../types";
import { makeRootValidatingTransformFactory } from "./factory";

import {
BadgeHelpersProps,
makeValidatingTransform as badgeMVT,
} from "./badge";
import {
FloatHelpersProps,
makeValidatingTransform as floatMVT,
Expand Down Expand Up @@ -40,7 +44,8 @@ export interface HelpersPropsOverrides {}

export type HelpersProps = Prefer<
HelpersPropsOverrides,
FloatHelpersProps &
BadgeHelpersProps &
FloatHelpersProps &
OverflowHelpersProps &
OverlayHelpersProps &
TooltipHelpersProps &
Expand All @@ -53,6 +58,7 @@ export type HelpersProps = Prefer<
export const makeRootValidatingTransform = makeRootValidatingTransformFactory<
HelpersProps
>(
badgeMVT,
floatMVT,
overflowMVT,
overlayMVT,
Expand Down
9 changes: 9 additions & 0 deletions src/base/helpers/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export type VariablesDefinitions = {
colors: (string | number)[];
shades: (string | number)[];

// Badge
badgeSizes: (string | number)[];

// Float
floatPulledAlignments: (string | number)[];

Expand Down Expand Up @@ -50,6 +53,9 @@ export const DEFAULTS = {
"white-bis",
),

// Badge
badgeSizes: tuple("small", "medium", "large"),

// Float
floatPulledAlignments: tuple("left", "right"),

Expand Down Expand Up @@ -89,6 +95,9 @@ export interface VariablesDefaults {
colors: (typeof DEFAULTS.colors)[number];
shades: (typeof DEFAULTS.shades)[number];

// Badge
badgeSizes: (typeof DEFAULTS.badgeSizes)[number];

// Float
floatPulledAlignments: (typeof DEFAULTS.floatPulledAlignments)[number];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
} from "src/__docs__/components";
import { DEFAULTS } from "src/base/helpers/variables";
import { Block, Button, Title } from "src/elements";
import { Badge, BADGE_DEFAULTS } from "../badge";

```
# Badge
Display a **badge** element in front of another component.
Expand Down Expand Up @@ -140,7 +140,7 @@ Use the `badgeRounded` prop of `<Badge>` to round a badge.
docUrl="https://wikiki.github.io/elements/badge/"
docProvider="Bulma-Extensions"
props={{
badgeContent: {
badge: {
description: "the contents of the badge",
typeName: "number | string",
},
Expand All @@ -160,3 +160,4 @@ Use the `badgeRounded` prop of `<Badge>` to round a badge.
},
}}
/>
```
89 changes: 0 additions & 89 deletions src/extensions/badge/__tests__/badge.test.tsx

This file was deleted.

Loading

0 comments on commit eb39273

Please sign in to comment.