Skip to content

Commit

Permalink
Demo & test changing attachments (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
danreeves committed Apr 21, 2018
1 parent ec927d5 commit 75e3e4e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
47 changes: 34 additions & 13 deletions example/components/demo.js
Expand Up @@ -88,31 +88,52 @@ const ToggleButton = styled.button`
}
`;

const AbsoluteDiv = styled.div`
position: absolute;
`;

export default class Demo extends React.Component {
tether = null;

container = null;

state = { on: true };
attachments = ['middle left', 'top center', 'middle right', 'bottom center'];

state = { on: true, attachment: this.attachments[0] };

componentDidMount() {
// Rerender with the container ref
this.setState({});
}

toggleTooltip = () => {
this.setState(({ on }) => {
return { on: !on };
});
};

cycleAttachment = () => {
const { attachment } = this.state;
let nextAttachment = this.attachments.indexOf(attachment) + 1;
if (nextAttachment === this.attachments.length) {
nextAttachment = 0;
}
this.setState(() => ({
attachment: this.attachments[nextAttachment],
}));
};

render() {
return (
<DemoZone>
<ToggleButton
id="CLICK_ME"
onClick={() =>
this.setState(({ on }) => {
return { on: !on };
})
}
>
Toggle tooltip
</ToggleButton>
<AbsoluteDiv>
<ToggleButton id="TOGGLE_TOOLTIP" onClick={this.toggleTooltip}>
Toggle tooltip
</ToggleButton>
<ToggleButton id="CYCLE_ATTACHMENT" onClick={this.cycleAttachment}>
Change side
</ToggleButton>
</AbsoluteDiv>
<div
ref={container => {
this.container = container;
Expand All @@ -124,7 +145,7 @@ export default class Demo extends React.Component {
ref={tether => {
this.tether = tether;
}}
attachment="middle left"
attachment={this.state.attachment}
constraints={[
{
to: this.container,
Expand All @@ -141,7 +162,7 @@ export default class Demo extends React.Component {
onDrag={() =>
this.tether.getTetherInstance() && this.tether.position()
}
defaultPosition={{ x: 25, y: 25 }}
defaultPosition={{ x: 25, y: 125 }}
/>
{this.state.on && (
<Tooltip id="WATCH_ME">
Expand Down
18 changes: 18 additions & 0 deletions example/components/tooltip.js
Expand Up @@ -23,6 +23,15 @@ function triangleForSide(side) {
border-bottom: 10px solid #333;
`;
}
if (side === 'bottom') {
return css`
bottom: -10px;
left: calc(50% - 10px);
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid #333;
`;
}
if (side === 'right') {
return css`
top: calc(50% - 10px);
Expand All @@ -33,13 +42,15 @@ function triangleForSide(side) {
`;
}
}

const triangleCommon = css`
position: absolute;
content: ' ';
font-size: 0;
line-height: 0;
width: 0;
`;

export default styled.div`
display: inline-block;
border-radius: 6px;
Expand Down Expand Up @@ -82,5 +93,12 @@ export default styled.div`
.tether-target-attached-bottom & {
margin-top: 10px;
}
.tether-target-attached-top &:after {
${() => triangleForSide('bottom')};
${triangleCommon};
}
.tether-target-attached-top & {
margin-bottom: 10px;
}
`};
`;
13 changes: 10 additions & 3 deletions tests/e2e/demo.js
Expand Up @@ -8,7 +8,8 @@ test(`It handles repositioning, constraints,
and unmounting the tethered component`, async t => {
const target = new Selector('#DRAG_ME');
const tooltip = new Selector('#WATCH_ME');
const button = new Selector('#CLICK_ME');
const toggleTooltip = new Selector('#TOGGLE_TOOLTIP');
const cycleAttachment = new Selector('#CYCLE_ATTACHMENT');

await t.hover(target);
// Target is to the left of the tooltip
Expand All @@ -27,11 +28,17 @@ test(`It handles repositioning, constraints,
.expect(targetAfterLeft > tooltipAfterLeft)
.ok(`${targetAfterLeft} > ${tooltipAfterLeft}`);

// The attachement can be changed
await t.click(cycleAttachment);
await t.click(cycleAttachment);
await t.click(cycleAttachment);
await t.click(cycleAttachment);

// Toggle the tooltip off
await t.click(button);
await t.click(toggleTooltip);
await t.drag(target, -300, 0);
// Toggle the tooltip on
await t.click(button);
await t.click(toggleTooltip);
await t.drag(target, -300, 0);
});

Expand Down

0 comments on commit 75e3e4e

Please sign in to comment.