Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui5-card): fires headerPress event upon header click #250

Merged
merged 7 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/main/src/Card.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<div {{> controlData}} class="sapFCard">
<header class="sapFCardHeader">
<header class="{{classes.header}}"
@click="{{ctr._headerPress}}"
@keydown="{{ctr._headerPress}}"
@keyup="{{ctr._headerPress}}"
tabindex="0">
{{#if image}}
<img src="{{ctr.avatar}}" aria-label="Avatar" class="sapFCardAvatar sapFCardHeaderImg">
{{/if}}
Expand Down
70 changes: 70 additions & 0 deletions packages/main/src/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import URI from "@ui5/webcomponents-base/src/types/URI";
import Bootstrap from "@ui5/webcomponents-base/src/Bootstrap";
import ShadowDOM from "@ui5/webcomponents-base/src/compatibility/ShadowDOM";
import { isIconURI } from "@ui5/webcomponents-base/src/IconPool";
import { isSpace, isEnter } from "@ui5/webcomponents-base/src/events/PseudoEvents";
import Function from "@ui5/webcomponents-base/src/types/Function";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a native type function so I think we can skip this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is, but the metadata validation would not pass for now. As this refactoring is started by Vladi, I will change it to native Function once it's done.

import CardRenderer from "./build/compiled/CardRenderer.lit";
import Icon from "./Icon";

Expand Down Expand Up @@ -82,6 +84,26 @@ const metadata = {
type: URI,
defaultValue: null,
},

_headerActive: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

activeHeader?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is for the pressed state, but there could be a public property determining wether the header is active or not (activeHeader as you said) and then this would be renamed to _pressedHeader or similar.

type: Boolean,
},

_headerPress: {
type: Function,
},
},
events: /** @lends sap.ui.webcomponents.main.Card.prototype */ {

/**
* Fired when the <code>ui5-card</code> header is pressed
* by click/tap or by using the Enter or Space key.
*
* @event
* @public
* @since 0.10.0
*/
headerPress: {},
},
};

Expand All @@ -107,6 +129,12 @@ const metadata = {
* @public
*/
class Card extends WebComponent {
constructor() {
super();

this._headerPress = this.headerPress.bind(this);
}

static get metadata() {
return metadata;
}
Expand All @@ -125,6 +153,12 @@ class Card extends WebComponent {
image,
ctr: state,
renderIcon: state.icon && !state.image,
classes: {
header: {
"sapFCardHeader": true,
"sapFCardHeaderActive": state._headerActive,
},
},
};
}

Expand All @@ -133,6 +167,42 @@ class Card extends WebComponent {

super.define(...params);
}

headerPress(event) {
const click = event.type === "click";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure how stable is this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this approach is arguable, we can discuss it briefly today.

const keydown = event.type === "keydown";

if (click) {
this.fireEvent("headerPress");
return;
}

if (isEnter(event)) {
this._handleEnter(keydown);
return;
}

if (isSpace(event)) {
event.preventDefault();
this._handleSpace(keydown);
}
}

_handleEnter(keydown) {
if (keydown) {
this.fireEvent("headerPress");
}

this._headerActive = keydown;
}

_handleSpace(keydown) {
if (!keydown) {
this.fireEvent("headerPress");
}

this._headerActive = keydown;
}
}

Bootstrap.boot().then(_ => {
Expand Down
19 changes: 19 additions & 0 deletions packages/main/src/themes/base/Card.less
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@_ui5_card_header_active_bg: #f0f0f0;
@_ui5_card_header_hover_bg: lighten(@_ui5_card_header_active_bg, 4);
@_ui5_card_header_border_color: darken(@sapUiTileBackground, 20);
@_ui5_card_focus_border_width: 1px;

:host(ui5-card) {
display: inline-block;
Expand All @@ -34,17 +35,35 @@ ui5-card {
}

.sapFCardHeader {
position: relative;
display: flex;
align-items: flex-start;
background: @sapUiTileBackground;
border-bottom: 1px solid @_ui5_card_header_border_color;
padding: @_ui5_card_content_padding;
}

.sapFCardHeader:focus {
outline: none;
}

.sapFCardHeader:focus:before {
content: "";
position: absolute;
border: @_ui5_card_focus_border_width dotted @sapUiContentFocusColor;
pointer-events: none;
top: 1px;
left: 1px;
right: 1px;
bottom: 1px;
}

.sapFCardHeader:hover {
cursor: pointer;
background: @_ui5_card_header_hover_bg;
}

.sapFCardHeader.sapFCardHeaderActive,
.sapFCardHeader:active {
background: @_ui5_card_header_active_bg;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/main/src/themes/sap_belize_hcb/Card.less
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

@_ui5_card_header_hover_bg: @sapUiHighlight;
@_ui5_card_header_active_bg: @sapUiHighlight;
@_ui5_card_header_border_color: @sapUiTileBorderColor;
@_ui5_card_header_border_color: @sapUiTileBorderColor;
@_ui5_card_focus_border_width: 0.125rem;
9 changes: 9 additions & 0 deletions packages/main/test/sap/ui/webcomponents/main/pages/Card.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<body>

<ui5-card
id="card"
status="4 of 10"
heading="Quick Links"
class="myCard">
Expand All @@ -38,7 +39,15 @@
<ui5-li icon="sap-icon://bar-chart" >AdWords Campaigns</ui5-li>
<ui5-li icon="sap-icon://tools-opportunity" >Winter Campaign Results</ui5-li>
</ui5-list>

<ui5-input id="field" value="0"></ui5-input>
</ui5-card>

<script>
card.addEventListener("headerPress", function (event) {
console.log(event);
field.value = parseInt(field.value) + 1;
});
</script>
</body>
</html>
16 changes: 15 additions & 1 deletion packages/main/test/specs/Card.spec.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
const assert = require('assert');
const assert = require('assert');
describe("Card general interaction", () => {
browser.url("http://localhost:8080/test-resources/sap/ui/webcomponents/main/pages/Card.html");

it("fires headerPress upon click, Enter and Space", () => {
const cardHeader = browser.findElementDeep("#card >>> .sapFCardHeader");
const field = browser.$("#field");

cardHeader.click();
cardHeader.keys("Space");
cardHeader.keys("Enter");

assert.strictEqual(field.getProperty("value"), "3", "headerPress should be called 3 times");
});
});