-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Show More icon in data table module
PiperOrigin-RevId: 526089815
- Loading branch information
Googler
committed
Apr 21, 2023
1 parent
1a9657e
commit ee54333
Showing
9 changed files
with
395 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
/* Show More */ | ||
:host { | ||
--show-more-max-width: 20px; | ||
} | ||
|
||
.lit-showmore { | ||
background-color: var(--lit-cyea-400); | ||
display: inline; | ||
width: var(--show-more-max-width); | ||
border-radius: 10px; | ||
vertical-align: top; | ||
} | ||
|
||
/* TODO(lit-dev): Make this behavior keyed on host attribute selector once | ||
show more functionality is expanded. */ | ||
.lit-showmore:hover { | ||
background-color: var(--lit-mage-400); | ||
} | ||
|
||
.icon-button { | ||
color: white; | ||
font-size: 24px; | ||
vertical-align: top; | ||
} | ||
|
||
.icon-button:hover { | ||
color: white; | ||
} |
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,82 @@ | ||
/** | ||
* @fileoverview A reusable element for collapsing and expanding text for LIT | ||
* | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// tslint:disable:no-new-decorators | ||
import {html} from 'lit'; | ||
import {customElement, property} from 'lit/decorators'; | ||
|
||
import {ReactiveElement} from '../lib/elements'; | ||
import {styles as sharedStyles} from '../lib/shared_styles.css'; | ||
|
||
import {styles} from './showmore.css'; | ||
|
||
/** | ||
* A show more element that denotes hidden text that can be expanded on click. | ||
* | ||
* | ||
* Usage: | ||
* <lit-showmore | ||
* @showmore=${showMore}> | ||
* </lit-showmore> | ||
*/ | ||
@customElement('lit-showmore') | ||
export class LitShowMore extends ReactiveElement { | ||
static override get styles() { | ||
return [sharedStyles, styles]; | ||
} | ||
|
||
@property({type: Boolean}) visible = true; | ||
@property({type: Number}) hiddenTextLength = 0; | ||
|
||
/** | ||
* Renders the Show More element with hidden or visible test. | ||
*/ | ||
override render() { | ||
const onChange = (e:Event) => { | ||
const event = new CustomEvent('showmore'); | ||
this.dispatchEvent(event); | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
this.visible = false; | ||
}; | ||
|
||
/* Non-standard HTML formatting must be preserved to prevent newlines from | ||
turning into spaces that expand the background of the Show More element. | ||
*/ | ||
// clang-format off | ||
const renderIcon = html`<span slot="tooltip-anchor" | ||
class="material-icon-outlined icon-button" | ||
@click=${onChange}>more_horiz</span>`; | ||
|
||
const renderShowMore = | ||
html`<lit-tooltip | ||
content="Show ${this.hiddenTextLength} more characters"> | ||
${renderIcon} | ||
</lit-tooltip>`; | ||
|
||
return html`<span class='lit-showmore'>${renderShowMore}</span>`; | ||
// clang-format on | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
'lit-showmore': LitShowMore; | ||
} | ||
} |
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,64 @@ | ||
import 'jasmine'; | ||
|
||
import {html, render} from 'lit'; | ||
import {LitElement} from 'lit'; | ||
|
||
import {LitShowMore} from './showmore'; | ||
|
||
describe('showmore test', () => { | ||
let showmore: LitShowMore; | ||
const CHANGE_HANDLERS = { | ||
showmore: (e: Event) => {}, | ||
}; | ||
|
||
beforeEach(async () => { | ||
showmore = new LitShowMore(); | ||
document.body.appendChild(showmore); | ||
await showmore.updateComplete; | ||
}); | ||
|
||
it('should instantiate correctly', () => { | ||
expect(showmore).toBeDefined(); | ||
expect(showmore).toBeInstanceOf(HTMLElement); | ||
expect(showmore).toBeInstanceOf(LitElement); | ||
}); | ||
|
||
it('is initially visible', async () => { | ||
const content = html` | ||
<lit-showmore | ||
id="visible" | ||
@show-more=${CHANGE_HANDLERS.showmore}> | ||
</lit-showmore>`; | ||
render(content, document.body); | ||
const queryString = 'lit-showmore#visible'; | ||
const showMoreIcon = | ||
document.body.querySelector<LitShowMore>(queryString)!; | ||
expect(showMoreIcon).toBeDefined(); | ||
await showMoreIcon.updateComplete; | ||
const contentDiv = | ||
showMoreIcon.renderRoot.children[0] as HTMLDivElement; | ||
expect(contentDiv).toBeDefined(); | ||
expect(contentDiv.innerHTML).toContain('more_horiz'); | ||
}); | ||
|
||
it('emits an event when clicked', async() => { | ||
const spy = spyOn(CHANGE_HANDLERS, 'showmore'); | ||
const content = html` | ||
<lit-showmore | ||
id="event-when-clicked" | ||
@showmore=${CHANGE_HANDLERS.showmore}> | ||
</lit-showmore>`; | ||
render(content, document.body); | ||
const queryString = 'lit-showmore#event-when-clicked'; | ||
const showMoreIcon = | ||
document.body.querySelector<LitShowMore>(queryString)!; | ||
await showMoreIcon.updateComplete; | ||
const icon = showMoreIcon.renderRoot.querySelector( | ||
'span > lit-tooltip > span[slot="tooltip-anchor"]' | ||
) as HTMLSpanElement; | ||
icon.click(); | ||
await showMoreIcon.updateComplete; | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
}); | ||
}); |
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
Oops, something went wrong.