chore: advanced version of h-output demo helper

This commit is contained in:
Thijs Louisse 2022-03-15 23:38:52 +01:00
parent 8d44b1d531
commit 8314b4b3ab

View file

@ -3,27 +3,56 @@ import { LionField } from '@lion/form-core';
import { LionFieldset } from '@lion/fieldset'; import { LionFieldset } from '@lion/fieldset';
export class HelperOutput extends LitElement { export class HelperOutput extends LitElement {
static get properties() { static properties = {
return {
field: Object, field: Object,
show: Array, show: Array,
title: String,
readyPromise: Object,
}; };
}
static get styles() { static styles = [
return [
css` css`
:host { :host {
display: block; display: block;
margin-top: 16px; margin-top: 8px;
}
code {
font-size: 8px;
background-color: #eee;
}
caption {
position: absolute;
top: 0;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(100%);
clip: rect(1px, 1px, 1px, 1px);
white-space: nowrap;
border: 0;
margin: 0;
padding: 0;
} }
table, table,
th, th,
td { td {
border: 1px solid #ccc; border-bottom: 1px solid rgb(204, 204, 204);
border-image: initial;
padding: 4px; padding: 4px;
font-size: 12px; font-size: 12px;
border-left: none;
border-right: none;
text-align: left;
}
td {
max-width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} }
table { table {
@ -35,7 +64,6 @@ export class HelperOutput extends LitElement {
} }
`, `,
]; ];
}
firstUpdated(changedProperties) { firstUpdated(changedProperties) {
super.firstUpdated(changedProperties); super.firstUpdated(changedProperties);
@ -47,15 +75,25 @@ export class HelperOutput extends LitElement {
} }
} }
this.__rerender = this.__rerender.bind(this); this.__rerender = this.__rerender.bind(this);
this.field.addEventListener('model-value-changed', this.__rerender);
this.field.addEventListener('mousemove', this.__rerender); const storyRoot = this.field.closest('[mdjs-story-name]');
this.field.addEventListener('blur', this.__rerender);
this.field.addEventListener('focusin', this.__rerender); storyRoot.addEventListener('model-value-changed', this.__rerender);
this.field.addEventListener('focusout', this.__rerender); storyRoot.addEventListener('mousemove', this.__rerender);
// this.field.addEventListener('blur', this.__rerender);
storyRoot.addEventListener('focusin', this.__rerender);
storyRoot.addEventListener('focusout', this.__rerender);
storyRoot.addEventListener('change', this.__rerender);
if (this.field._inputNode.form) { if (this.field._inputNode.form) {
this.field._inputNode.form.addEventListener('submit', this.__rerender); this.field._inputNode.form.addEventListener('submit', this.__rerender);
} }
if (this.readyPromise) {
this.readyPromise.then(() => {
this.__rerender();
});
}
} }
__rerender() { __rerender() {
@ -67,29 +105,47 @@ export class HelperOutput extends LitElement {
} }
// eslint-disable-next-line class-methods-use-this // eslint-disable-next-line class-methods-use-this
__renderProp(p) { __renderProp(prop) {
const field = this.field || {};
let resultText = '';
if (typeof prop === 'string') {
const p = field[prop];
if (typeof p === 'boolean') { if (typeof p === 'boolean') {
return p === true ? '✓' : ''; return p === true ? '✓' : '';
} }
if (typeof p === 'undefined') { if (typeof p === 'undefined') {
return '?'; return html`<code>undefined</code>`;
} }
return p; if (typeof p === 'object' && p !== null) {
return JSON.stringify(p);
}
resultText = p;
} else {
resultText = prop.processor(field);
}
return html`<span title="${resultText}">${resultText}</span>`;
}
constructor() {
super();
this.title = 'States';
} }
render() { render() {
const field = this.field || {}; const computePropName = prop => (typeof prop === 'string' ? prop : prop.name);
return html` return html`
<table> <table>
<caption> <caption>
Interaction States ${this.title}
</caption> </caption>
<tr> <tr>
${this.show.map(prop => html`<th>${prop}</th>`)} ${this.show.map(prop => html`<th>${computePropName(prop)}</th>`)}
</tr> </tr>
<tr></tr> <tr></tr>
<tr> <tr>
${this.show.map(prop => html`<td>${this.__renderProp(field[prop])}</td>`)} ${this.show.map(prop => html`<td>${this.__renderProp(prop)}</td>`)}
</tr> </tr>
</table> </table>
`; `;