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(select): add badge in select options #549

Merged
merged 20 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions packages/core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ export namespace Components {
label?: string
selected?: boolean
disabled?: boolean
tag?: { color: string; label: string }
}>;
"placeholder": string;
"readonly"?: boolean;
"setTag": () => Promise<void>;
"value"?: IonTypes.IonSelect['value'];
}
interface AtomTag {
Expand Down Expand Up @@ -730,6 +732,7 @@ declare namespace LocalJSX {
label?: string
selected?: boolean
disabled?: boolean
tag?: { color: string; label: string }
}>;
"placeholder"?: string;
"readonly"?: boolean;
Expand Down
26 changes: 22 additions & 4 deletions packages/core/src/components/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ const optionsMock: {
selected?: boolean
disabled?: boolean
label?: string
tag?: { type: string; label: string }
}[] = [
{ value: 'apple', selected: true },
{ value: 'banana', disabled: true },
{ value: 'orange' },
{ value: 'orange', tag: { type: 'success', label: 'New' } },
]

describe('AtomSelect', () => {
Expand Down Expand Up @@ -252,17 +253,21 @@ describe('AtomSelect', () => {
await page.waitForChanges()

const selectEl = page.root?.shadowRoot?.querySelector('ion-select')
const spy = jest.fn()
const spyIonBlur = jest.fn()
const spySetTag = jest.spyOn(page.rootInstance, 'setTag')

page.root?.addEventListener('ionBlur', spy)
page.root?.addEventListener('ionBlur', spyIonBlur)

if (selectEl) {
selectEl.dispatchEvent(new Event('ionBlur'))
}

await page.waitForChanges()

page.root?.dispatchEvent(new CustomEvent('ionBlur'))

expect(spy).toHaveBeenCalled()
expect(spyIonBlur).toHaveBeenCalled()
expect(spySetTag).toHaveBeenCalled()
})

it('emits atomCancel event on select cancel', async () => {
Expand Down Expand Up @@ -331,4 +336,17 @@ describe('AtomSelect', () => {

expect(handleDismiss).not.toHaveBeenCalled()
})

it('should filter options with tag', async () => {
const page = await newSpecPage({
components: [AtomSelect],
html: '<atom-select />',
})

await page.waitForChanges()
const mockFiltered = optionsMock.filter((option) => option?.tag?.label)
const instanceObjetct = page.rootInstance.filterOptionsWithTag(optionsMock)

expect(Object.keys(instanceObjetct).length).toEqual(mockFiltered.length)
})
})
69 changes: 69 additions & 0 deletions packages/core/src/components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Host,
Prop,
h,
Method,
} from '@stencil/core'

import { IconProps } from '../../icons'
Expand Down Expand Up @@ -40,6 +41,7 @@ export class AtomSelect {
label?: string
selected?: boolean
disabled?: boolean
tag?: { color: string; label: string }
}> = []

@Event() atomBlur!: EventEmitter<void>
Expand All @@ -48,10 +50,75 @@ export class AtomSelect {
@Event() atomDismiss!: EventEmitter<void>
@Event() atomFocus!: EventEmitter<void>

@Method()
setTag() {
const ionItemElements = document.querySelectorAll('ion-item')
const optionsWithTag = this.optionsWithTag

if (!ionItemElements || !optionsWithTag) return

ionItemElements?.forEach((itemElement) => {
const elementText = itemElement.textContent?.trim()
const optionWithTag = optionsWithTag[elementText]
const sideElement =
this.getSideElement(itemElement, 'ion-radio') ||
this.getSideElement(itemElement, 'ion-checkbox')

if (!optionWithTag || !itemElement) return

const tagElement = document.createElement('atom-tag')
const { color, label } = optionWithTag.tag

tagElement.setAttribute('color', color)
tagElement.textContent = label
tagElement.classList.add('atom-tag')
tagElement.style.marginLeft = '8px'

const insideElement = sideElement.shadowRoot
.firstElementChild as HTMLElement

insideElement.style.justifyContent = 'start'

insideElement.firstElementChild.insertAdjacentElement(
'afterend',
tagElement
)
})
}

getSideElement(element, name) {
return element.getElementsByTagName(name)[0] as HTMLElement
}
filterOptionsWithTag = (
options: Array<{
label?: string
value?: string
tag?: { label: string; color: string }
}>
) => {
return options?.reduce((optionsWithTag, option) => {
if (option?.tag?.label) {
const label = option.label || option.value

if (label) {
optionsWithTag[label] = option
}
}

return optionsWithTag
}, {})
}

optionsWithTag = {}

componentDidLoad() {
this.selectEl.addEventListener('ionDismiss', this.handleDismiss)
}

componentWillLoad() {
this.optionsWithTag = this.filterOptionsWithTag(this.options)
}

disconnectedCallback() {
this.selectEl.removeEventListener('ionDismiss', this.handleDismiss)
}
Expand All @@ -67,6 +134,8 @@ export class AtomSelect {
}

private handleBlur = () => {
this.setTag()

this.selectEl.removeEventListener('ionBlur', this.handleBlur)
this.atomBlur.emit()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ const createSelect = (args) => {
atomSelectElements.forEach((atomSelect) => {
atomSelect.options = [
{ id: '1', value: 'Red', disabled: false },
{ id: '2', value: 'Green', disabled: false },
{
id: '2',
value: 'Green',
disabled: false,
tag: { color: 'success', label: 'New' },
},
{ id: '3', value: 'Blue', disabled: false },
{
id: '4',
value: 'nice_blue',
disabled: false,
label: 'Nice Blue',
tag: { color: 'success', label: 'New' },
},
{ id: '5', value: 'Disabled example', disabled: true },
]
Expand Down
Loading