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

refactor(ui-kit/button): use button content property #966

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
75d7391
refactor(ui-kit/button): use button content property
njfamirm Mar 19, 2023
1e58e06
feat: use button content
njfamirm Mar 19, 2023
aac6d20
refactor(ui/button): render template and reflect attribs
AliMD Mar 22, 2023
b7d57a1
refactor(ui-kit/button): check click signal id on click handler
njfamirm Mar 22, 2023
944496f
refactor(ui-kit/icon-button): render template and reflect attributes
njfamirm Mar 22, 2023
da2d09a
refactor(com-pwa/order-detail): move button to manager
njfamirm Mar 22, 2023
1469618
refactor(com-pwa/new-ordr): move button to manager
njfamirm Mar 22, 2023
96041c7
fix(com-pwa/new-order-controller): scroll to top on review state
njfamirm Mar 22, 2023
8894dc6
refactor(com-pwa/new-order): set label Key on button content
njfamirm Mar 22, 2023
7092cd3
perf(ui-kit/top-app-bar): check attribute value before set it
njfamirm Mar 22, 2023
ca9b1cc
perf(ui-kit): set attribute on firstUpdated
njfamirm Mar 22, 2023
fda027a
perf(ui-kit): render on null content property
njfamirm Mar 22, 2023
929aee0
refactor(ui-kit): set default content value
njfamirm Mar 22, 2023
4e813a6
feat(ui-kit/text-field): use content property
njfamirm Mar 22, 2023
1b74a6c
feat: use content for text field
njfamirm Mar 22, 2023
5c94a36
feat(ui-kit/radio-group): use content property
njfamirm Mar 22, 2023
7b5e2ab
feat: use content for radio group
njfamirm Mar 22, 2023
cce9332
fix(com-pwa): qty text field signal listener
njfamirm Mar 23, 2023
bbd94f7
fix(com-pwa): radio group value
njfamirm Mar 23, 2023
668cf75
fix(com-pwa/buttons): detail label key
njfamirm Mar 23, 2023
8052a2f
fix(com-pwa): reload button
njfamirm Mar 23, 2023
9d0f9c7
fix(ui/*-button): remove default value for content in signal handler
njfamirm Mar 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 65 additions & 30 deletions ui/ui-kit/src/button/button.ts
@@ -1,17 +1,47 @@
import {css, customElement, html, property} from '@alwatr/element';
import {PropertyValues, css, customElement, html, nothing, property} from '@alwatr/element';
import {message} from '@alwatr/i18n';
import '@alwatr/icon';
import {eventTrigger} from '@alwatr/signal';

import {AlwatrSurface} from '../card/surface.js';

import type {ClickSignalType, Stringifyable} from '@alwatr/type';
import type {ClickSignalType, Stringifyable, StringifyableRecord} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
'alwatr-button': AlwatrButton;
}
}

export interface ButtonContent extends StringifyableRecord {
/**
* Label i18n key.
*/
labelKey?: string;

/**
* Icon name.
*/
icon?: string;

/**
* Flip icon on rtl
*/
flipRtl?: true;

/**
* Unique name for identify click event over signal.
*/
clickSignalId?: string;

/**
* Dispatched signal with ClickSignalType and this detail.
*/
clickDetail?: Stringifyable;

disabled?: boolean;
}

/**
* Alwatr outlined text field.
*
Expand Down Expand Up @@ -50,18 +80,16 @@ export class AlwatrButton extends AlwatrSurface {
`,
];

@property()
icon?: string;

@property({attribute: 'click-signal-id'})
clickSignalId?: string;
@property({type: Object})
content?: ButtonContent;

@property({attribute: false})
detail?: Stringifyable;
protected override firstUpdated(_changedProperties: PropertyValues<this>): void {
super.firstUpdated(_changedProperties);
this.setAttribute('stated', '');
}

override connectedCallback(): void {
super.connectedCallback();
this.setAttribute('stated', '');
this.addEventListener('click', this._click);
}

Expand All @@ -70,30 +98,37 @@ export class AlwatrButton extends AlwatrSurface {
this.removeEventListener('click', this._click);
}

protected override update(changedProperties: PropertyValues<this>): void {
super.update(changedProperties);

const disabled = Boolean(this.content?.disabled);
if (this.hasAttribute('disabled') !== disabled) {
this.toggleAttribute('disabled', disabled);
}
}

override render(): unknown {
this._logger.logMethod('render');
if (this.icon) {
return html`
<alwatr-icon .name=${this.icon} ?flip-rtl=${this.hasAttribute('flip-rtl')}></alwatr-icon>
<slot>button</slot>
`;
}
else {
return html`<slot>button</slot>`;
}
const content = this.content || {};

return [
content.icon
? html`<alwatr-icon .name=${content.icon} ?flip-rtl=${content.flipRtl}></alwatr-icon>`
: nothing,
html`<slot>${message(content.labelKey)}</slot>`,
];
}

protected _click(event: MouseEvent): void {
this._logger.logMethodArgs('click', {clickSignalId: this.clickSignalId});
if (this.clickSignalId) {
eventTrigger.dispatch<ClickSignalType>(this.clickSignalId, {
x: event.clientX,
y: event.clientY,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
detail: this.detail,
});
}
if (this.content?.clickSignalId == null) return;
this._logger.logMethodArgs('click', {clickSignalId: this.content.clickSignalId});
eventTrigger.dispatch<ClickSignalType>(this.content.clickSignalId, {
x: event.clientX,
y: event.clientY,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
metaKey: event.metaKey,
detail: this.content.clickDetail,
});
}
}
26 changes: 15 additions & 11 deletions ui/ui-kit/src/button/icon-button.ts
Expand Up @@ -76,12 +76,16 @@ export class AlwatrStandardIconButton extends AlwatrSurface {
`,
];

@property({type: Object, attribute: false})
@property({type: Object})
content?: IconButtonContent;

protected override firstUpdated(_changedProperties: PropertyValues<this>): void {
super.firstUpdated(_changedProperties);
this.setAttribute('stated', '');
}

override connectedCallback(): void {
super.connectedCallback();
this.setAttribute('stated', '');
this.addEventListener('click', this._click);
}

Expand All @@ -90,20 +94,20 @@ export class AlwatrStandardIconButton extends AlwatrSurface {
this.removeEventListener('click', this._click);
}

protected override shouldUpdate(changedProperties: PropertyValues<this>): boolean {
return super.shouldUpdate(changedProperties) && this.content != null;
}

override render(): unknown {
this._logger.logMethod('render');
if (this.content == null) return;
protected override update(changedProperties: PropertyValues<this>): void {
super.update(changedProperties);

const disabled = Boolean(this.content.disabled);
const disabled = Boolean(this.content?.disabled);
if (this.hasAttribute('disabled') !== disabled) {
this.toggleAttribute('disabled', disabled);
}
}

override render(): unknown {
this._logger.logMethod('render');
const content = this.content || {icon: ''};

return html`<alwatr-icon .name=${this.content.icon} ?flip-rtl=${this.content.flipRtl}></alwatr-icon>`;
return html`<alwatr-icon .name=${content.icon} ?flip-rtl=${content.flipRtl}></alwatr-icon>`;
}

protected _click(event: MouseEvent): void {
Expand Down
6 changes: 3 additions & 3 deletions ui/ui-kit/src/card/icon-box.ts
@@ -1,4 +1,4 @@
import {css, customElement, html, property, nothing, ifDefined, when, PropertyValues} from '@alwatr/element';
import {css, customElement, html, property, ifDefined, when, PropertyValues} from '@alwatr/element';
import '@alwatr/icon';

import {AlwatrSurface} from './surface.js';
Expand Down Expand Up @@ -126,8 +126,8 @@ export class AlwatrIconBox extends AlwatrSurface {

override render(): unknown {
this._logger.logMethod('render');
const content = this.content;
if (content == null) return nothing;
const content: IconBoxContent = this.content || {headline: ''};

const target = content.target !== 'download' ? content.target : undefined;

const template = html`<h3 class="headline">
Expand Down
5 changes: 2 additions & 3 deletions ui/ui-kit/src/card/image-box.ts
@@ -1,4 +1,4 @@
import {css, customElement, html, property, nothing, ifDefined, type PropertyValues} from '@alwatr/element';
import {css, customElement, html, property, ifDefined, type PropertyValues} from '@alwatr/element';
import '@alwatr/icon';

import {AlwatrSurface} from './surface.js';
Expand Down Expand Up @@ -148,8 +148,7 @@ export class AlwatrImageBox extends AlwatrSurface {

override render(): unknown {
this._logger.logMethod('render');
const content = this.content;
if (content == null) return nothing;
const content = this.content || {image: '', headline: ''};
const target = content.target !== 'download' ? content.target : undefined;

const template = html`
Expand Down
21 changes: 8 additions & 13 deletions ui/ui-kit/src/card/product-card.ts
Expand Up @@ -4,10 +4,9 @@ import {
SignalMixin,
LocalizeMixin,
ToggleMixin,
nothing,
html,
property,
type PropertyValues,
PropertyValueMap,
} from '@alwatr/element';
import {number} from '@alwatr/i18n';
import '@alwatr/icon';
Expand Down Expand Up @@ -139,29 +138,25 @@ export class AlwatrProductCard extends ToggleMixin(LocalizeMixin(SignalMixin(Alw
@property({type: Object, attribute: false})
content?: ProductCartContent;

override connectedCallback(): void {
super.connectedCallback();
protected override firstUpdated(_changedProperties: PropertyValueMap<this>): void {
super.firstUpdated(_changedProperties);
this.setAttribute('stated', '');
this.setAttribute('elevated', '');
}

protected override shouldUpdate(changedProperties: PropertyValues<this>): boolean {
return super.shouldUpdate(changedProperties) && this.content != null;
}

override render(): unknown {
this._logger.logMethod('render');
if (this.content == null) return nothing;
const content: ProductCartContent = this.content || {imagePath: '', title: ''};

// const icon = this.selected ? 'radio-button-on-outline' : 'radio-button-off-outline';

return html`
<img src=${this.content.imagePath} alt=${this.content.title} />
<img src=${content.imagePath} alt=${content.title} />
<div class="content">
<h2 class="title">${this.content.title}</h2>
<h2 class="title">${content.title}</h2>
<div class="price">
<del>${number(this.content.price)}</del>
<ins>${number(this.content.finalPrice)}</ins>
<del>${number(content.price)}</del>
<ins>${number(content.finalPrice)}</ins>
<alwatr-icon .name=${'toman'}></alwatr-icon>
</div>
</div>
Expand Down
57 changes: 37 additions & 20 deletions ui/ui-kit/src/radio-group/radio-group.ts
@@ -1,4 +1,6 @@
import {customElement, css, html, map, AlwatrBaseElement, property, nothing, live} from '@alwatr/element';
import {customElement, css, html, map, AlwatrBaseElement, property, live} from '@alwatr/element';
import {eventTrigger} from '@alwatr/signal';
import {Stringifyable} from '@alwatr/type';

declare global {
interface HTMLElementTagNameMap {
Expand All @@ -8,9 +10,19 @@ declare global {

export type RadioOption = {label: string; value: string};

export type RadioGroupOptions = {
export type RadioGroupSignalDetail<T extends Stringifyable = null> = Stringifyable & {
name: string;
value: string;
detail: T;
}

export type RadioGroupContent = {
name: string;
title: string;
radioGroup: Array<RadioOption>;
value?: string;
inputChangeSignalName?: string;
inputChangeSignalDetail?: Stringifyable;
};

/**
Expand Down Expand Up @@ -71,36 +83,32 @@ export class AlwatrRadioGroup extends AlwatrBaseElement {
`;

@property({type: Object})
name = 'unknown';

@property({type: Object})
options?: RadioGroupOptions;

@property({type: String})
value?: string;
content?: RadioGroupContent;

override render(): unknown {
this._logger.logMethod('render');
return html`
<fieldset>
<legend>${this.options?.title}</legend>
<legend>${this.content?.title}</legend>
${this._optionsTemplate()}
</fieldset>
`;
}

protected _optionsTemplate(): unknown {
const options = this.options;
if (options == null) return nothing;
return map(options.radioGroup, (radioItem, index) => {
const content = this.content || {radioGroup: [], name: '', value: ''};

return map(content.radioGroup, (radioItem, index) => {
const id: string = 'radioInput_' + index;
content.value ??= '';

return html`<div>
<input
type="radio"
id=${id}
.name=${this.name}
type="radio"
.name=${content.name}
.value=${radioItem.value}
.checked=${live(radioItem.value === this.value)}
.checked=${live(radioItem.value === content.value)}
@change=${this._inputChanged}
/>
<label for=${id}>${radioItem.label}</label>
Expand All @@ -109,9 +117,18 @@ export class AlwatrRadioGroup extends AlwatrBaseElement {
}

private _inputChanged(event: Event): void {
const target = event.target as HTMLInputElement;
if (target == null) return;
this.value = target.value;
this.dispatchEvent(new CustomEvent('input-change'));
this._logger.logMethod('_inputChanged');
const target = event.target as HTMLInputElement | HTMLTextAreaElement;
const content = this.content;
if (target == null || content == null) return;

content.value = target.value;
if (content.inputChangeSignalName) {
eventTrigger.dispatch<RadioGroupSignalDetail<Stringifyable>>(content.inputChangeSignalName, {
name: content.name,
value: content.value,
detail: content.inputChangeSignalDetail,
});
}
}
}
12 changes: 8 additions & 4 deletions ui/ui-kit/src/snackbar/element.ts
@@ -1,4 +1,4 @@
import {html, css, customElement, property, when} from '@alwatr/element';
import {html, css, customElement, property, when, PropertyValueMap} from '@alwatr/element';
import {untilEvent} from '@alwatr/util';

import '../button/button.js';
Expand Down Expand Up @@ -112,16 +112,20 @@ export class AlwatrSnackbar extends AlwatrSurface {
@property({type: String})
actionLabel?: string;

override connectedCallback(): void {
super.connectedCallback();
protected override firstUpdated(_changedProperties: PropertyValueMap<this>): void {
super.firstUpdated(_changedProperties);
this.setAttribute('elevated', '3');
}

override render(): unknown {
this._logger.logMethod('render');
return html`<span class="message">${this.message}</span>${when(
this.actionLabel,
() => html`<alwatr-button @click=${this._actionButtonClick}>${this.actionLabel}</alwatr-button>`,
() =>
html`<alwatr-button
.content=${{labelKey: this.actionLabel}}
@click=${this._actionButtonClick}
></alwatr-button>`,
)}`;
}

Expand Down