-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
255 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { | ||
makePropTypes, | ||
makeValidatingTransform, | ||
} from "src/base/helpers/tooltip"; | ||
import { DEFAULTS } from "src/base/helpers/variables"; | ||
import { tuple } from "../../../utils"; | ||
|
||
import { | ||
validateBoolPropType, | ||
validateOneOfPropType, | ||
validatePropType, | ||
} from "src/__tests__/testing"; | ||
import { | ||
testItShouldNotSetClassNameOnEmpty, | ||
testItShouldPreserveCustomClassName, | ||
testItShouldPreserveUnknown, | ||
testItShouldUseDefaultLocationProp, | ||
} from "./testing"; | ||
|
||
const CNAME = "foo"; | ||
const LOC = "prop"; | ||
|
||
describe("Tooltip helpers", () => { | ||
const propTypes = makePropTypes(); | ||
const vtfunc = makeValidatingTransform(); | ||
|
||
describe("propTypes", () => { | ||
validateBoolPropType(propTypes, "tooltipActive"); | ||
validateOneOfPropType(propTypes, "tooltipColor", DEFAULTS.colors); | ||
validateBoolPropType(propTypes, "tooltipMultiline"); | ||
validateOneOfPropType( | ||
propTypes, | ||
"tooltipPosition", | ||
DEFAULTS.tooltipPositions, | ||
); | ||
validatePropType(propTypes, "tooltipResponsive", [ | ||
...DEFAULTS.breakpoints | ||
.map(breakpoint => | ||
DEFAULTS.tooltipPositions.map(tooltipPosition => ({ | ||
descriptor: `${breakpoint}-${tooltipPosition}`, | ||
valid: true, | ||
value: { [breakpoint]: tooltipPosition }, | ||
})), | ||
) | ||
.reduce((acc, cv) => [...acc, ...cv], []), | ||
{ | ||
error: new RegExp(`Warning.+Failed prop.+ \`tooltipResponsive\``), | ||
valid: false, | ||
value: false, | ||
}, | ||
{ | ||
error: new RegExp(`Warning.+Failed prop.+ \`tooltipResponsive.foo\``), | ||
valid: false, | ||
value: { foo: "asdf" }, | ||
}, | ||
]); | ||
testItShouldUseDefaultLocationProp(vtfunc, { | ||
tooltipPosition: "__UNKNOWN", | ||
}); | ||
|
||
describe("custom", () => { | ||
const customTooltipPositions = tuple("a", "b"); | ||
const customPropTypes = makePropTypes({ | ||
tooltipPositions: customTooltipPositions, | ||
}); | ||
|
||
validateOneOfPropType( | ||
customPropTypes, | ||
"tooltipPosition", | ||
customTooltipPositions, | ||
); | ||
}); | ||
}); | ||
|
||
describe("transform", () => { | ||
testItShouldPreserveUnknown(vtfunc); | ||
testItShouldNotSetClassNameOnEmpty(vtfunc); | ||
testItShouldPreserveCustomClassName(vtfunc); | ||
|
||
it("should should get tooltip className and data-tooltip", () => { | ||
expect(vtfunc({ tooltip: "foobar" }, CNAME, LOC)).toEqual({ | ||
className: "tooltip", | ||
"data-tooltip": "foobar", | ||
}); | ||
}); | ||
|
||
[false, true].map(tooltipActive => { | ||
it(`should ${tooltipActive ? "" : "not "}be tooltipActive`, () => { | ||
expect(vtfunc({ tooltipActive }, CNAME, LOC)).toEqual({ | ||
className: tooltipActive ? "is-tooltip-active" : "", | ||
}); | ||
}); | ||
}); | ||
|
||
DEFAULTS.colors.map(color => { | ||
it(`should be ${color}`, () => { | ||
expect(vtfunc({ tooltipColor: color }, CNAME, LOC)).toEqual({ | ||
className: `is-tooltip-${color}`, | ||
}); | ||
}); | ||
}); | ||
|
||
[false, true].map(tooltipMultiline => { | ||
it(`should ${tooltipMultiline ? "" : "not "}be tooltipMultiline`, () => { | ||
expect(vtfunc({ tooltipMultiline }, CNAME, LOC)).toEqual({ | ||
className: tooltipMultiline ? "is-tooltip-multiline" : "", | ||
}); | ||
}); | ||
}); | ||
|
||
DEFAULTS.tooltipPositions.map(tooltipPosition => { | ||
it(`should be position ${tooltipPosition}`, () => { | ||
expect(vtfunc({ tooltipPosition }, CNAME, LOC)).toEqual({ | ||
className: `is-tooltip-${tooltipPosition}`, | ||
}); | ||
}); | ||
}); | ||
|
||
DEFAULTS.breakpoints.map(breakpoint => { | ||
DEFAULTS.tooltipPositions.map(tooltipPosition => { | ||
it(`should be position ${breakpoint}-${tooltipPosition}`, () => { | ||
expect( | ||
vtfunc( | ||
{ tooltipResponsive: { [breakpoint]: tooltipPosition } }, | ||
CNAME, | ||
LOC, | ||
), | ||
).toEqual({ | ||
className: `is-tooltip-${tooltipPosition}-${breakpoint}`, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import classNames from "classnames"; | ||
import PropTypes from "prop-types"; | ||
|
||
import { | ||
makePropTypesFactory, | ||
makeValidatingTransformFactory, | ||
TransformFunction, | ||
} from "./factory"; | ||
import { DEFAULTS, Variables } from "./variables"; | ||
|
||
export type TooltipHelpersProps = Partial<{ | ||
tooltip: number | string; | ||
tooltipActive: boolean; | ||
tooltipColor: Variables["colors"]; | ||
tooltipMultiline: boolean; | ||
tooltipPosition: (typeof DEFAULTS["tooltipPositions"])[number]; | ||
tooltipResponsive: { | ||
[K in Variables["breakpoints"]]?: (typeof DEFAULTS["tooltipPositions"])[number]; | ||
}; | ||
}>; | ||
|
||
// Factories | ||
export const makePropTypes = makePropTypesFactory(vars => ({ | ||
tooltip: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
tooltipActive: PropTypes.bool, | ||
tooltipColor: PropTypes.oneOf(vars.colors), | ||
tooltipMultiline: PropTypes.bool, | ||
tooltipPosition: PropTypes.oneOf(vars.tooltipPositions), | ||
tooltipResponsive: PropTypes.objectOf(PropTypes.oneOf(vars.tooltipPositions)), | ||
})); | ||
|
||
export const transform: TransformFunction<TooltipHelpersProps> = props => { | ||
const { | ||
tooltip, | ||
tooltipActive, | ||
tooltipColor, | ||
tooltipMultiline, | ||
tooltipPosition, | ||
tooltipResponsive = {}, | ||
...rest | ||
} = props; | ||
|
||
rest.className = classNames( | ||
{ | ||
"is-tooltip-active": tooltipActive, | ||
[`is-tooltip-${tooltipColor}`]: tooltipColor, | ||
"is-tooltip-multiline": tooltipMultiline, | ||
[`is-tooltip-${tooltipPosition}`]: tooltipPosition, | ||
tooltip, | ||
}, | ||
...Object.keys(tooltipResponsive).map( | ||
breakpoint => `is-tooltip-${tooltipResponsive[breakpoint]}-${breakpoint}`, | ||
), | ||
rest.className, | ||
); | ||
|
||
if (tooltip !== undefined) { | ||
rest["data-tooltip"] = tooltip; | ||
} | ||
|
||
return rest; | ||
}; | ||
|
||
export const makeValidatingTransform = makeValidatingTransformFactory( | ||
makePropTypes, | ||
transform, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
name: Tooltip | ||
menu: Extensions | ||
route: /extensions/tooltip | ||
--- | ||
|
||
import { Playground } from "docz"; | ||
|
||
import { | ||
ForwardRefAsExoticComponentDoc, | ||
mapEnumerable, | ||
OptionBlock, | ||
} from "src/__docs__/components"; | ||
import { DEFAULTS } from "src/base/helpers/variables"; | ||
import { Block, Button, Title } from "src/elements"; | ||
|
||
# Tooltip | ||
|
||
Display a **tooltip** attached to any kind of element with different positioning. | ||
|
||
It can display number or strings. | ||
|
||
<Playground> | ||
<Button badgeContent="pending" badgeColor="warning" tooltip="Tooltip Text"> | ||
Notifications | ||
</Button> | ||
</Playground> |
Oops, something went wrong.