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

Fix/person/select/selectedperson null #1641

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions .changeset/good-kiwis-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@equinor/fusion-wc-person": patch
---

### Changes in `PersonSelectController`

- Updated the `attrSelectPerson` method to clear `selectedIds` when `personData` or `selectedPerson` is null.
- Updated the `clearInput` method to reset `azureId` and `upn` properties.
- Refactored the `clear` method to use `#firePersonSelectEvent` instead of directly dispatching the event.
- Removed unnecessary comments and cleaned up the code.

### Changes in `PersonSelectElement`

- Added a new CSS class `.selected-persons` to hide the text input when a person is selected.
- Updated the `selectedPersonsTemplate` method to be conditionally rendered based on the `selectedIds` size and `isOpen` state.
- Adjusted the `classMap` to include the `selected-persons` class based on the `selectedIds` size and `isOpen`
22 changes: 6 additions & 16 deletions packages/person/src/components/select/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PersonSelectController implements ReactiveController {
}

/**
* Resolve person from selectedPerson property.
* Resolve personInfo task from selectedPerson property.
* Runs on host updated when property is changed
*/
public attrSelectPerson(select: string | null | undefined) {
Expand Down Expand Up @@ -186,8 +186,9 @@ export class PersonSelectController implements ReactiveController {
this.#firePersonSelectEvent(personData);

/* clear component if null */
if (personData === null) {
if (personData === null || this.#host.selectedPerson === null) {
this.clearInput();
this.selectedIds.clear();
}
}

Expand All @@ -206,6 +207,8 @@ export class PersonSelectController implements ReactiveController {
this.#host.value = '';
this.#host.search = '';
this.#host.textInputElement.value = '';
this.#host.azureId = '';
this.#host.upn = '';
}

public clear() {
Expand All @@ -215,19 +218,8 @@ export class PersonSelectController implements ReactiveController {
this.selectedIds.clear();
}

if (this.#host.selectedPerson) {
this.#host.selectedPerson = null;
}

/* Dispatch custom select event with our details */
this.#host.dispatchEvent(
new PersonSelectEvent({
detail: {
selected: null,
},
bubbles: true,
}),
);
this.#firePersonSelectEvent(null);
}

/**
Expand Down Expand Up @@ -312,7 +304,6 @@ export class PersonSelectController implements ReactiveController {
if (event.key === 'ArrowDown') {
/* focus on the fwc-list' */
if (this._isOpen && this.#host.listItems.length) {
// this.#host.listElement?.focus();
this.#host.listElement?.focusItemAtIndex(0);
}
return;
Expand All @@ -324,7 +315,6 @@ export class PersonSelectController implements ReactiveController {
}
this.timer = setTimeout(() => {
const value = target.value.trim().toLowerCase();
// this.#search = value;
this.#host.search = value;
this.#host.requestUpdate();
}, 500);
Expand Down
3 changes: 3 additions & 0 deletions packages/person/src/components/select/element.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const styles: CSSResult[] = [
fwc-person-list-item {
--fwc-person-list-item-background: #ffffff;
}
.selected-persons fwc-textinput {
opacity: 0;
}
#selected-persons {
list-style: none;
padding: 0;
Expand Down
5 changes: 3 additions & 2 deletions packages/person/src/components/select/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export class PersonSelectElement
// task is complete and we have the attribute
if (this.selectedPerson && this.tasks.info.status === TaskStatus.COMPLETE) {
// save result in controller if not already there
if (!this.controllers.element.selectedIds.has(this.tasks.info.value?.azureId as string)) {
if (!this.controllers.element.selectedIds.size) {
this.controllers.element.selectPersonInfo(this.tasks.info.value as PersonInfo);
}
}
Expand Down Expand Up @@ -305,6 +305,7 @@ export class PersonSelectElement
dense: dense == true,
'variant-filled': variant === 'filled',
'variant-outlined': variant === 'outlined',
'selected-persons': this.controllers.element.selectedIds.size > 0 && !this.controllers.element.isOpen,
};

/** Select person by selectedPerson property on info task */
Expand All @@ -313,6 +314,7 @@ export class PersonSelectElement
return html`<div id=${this.id} class=${classMap(cssClasses)}>
<div class="input">
<slot name="leading"></slot>
${this.selectedPersonsTemplate()}
<fwc-textinput
label=${ifDefined(this.label)}
type="text"
Expand All @@ -328,7 +330,6 @@ export class PersonSelectElement
}}
@keyup=${this.controllers.element.handleKeyup}
></fwc-textinput>
${this.selectedPersonsTemplate()}
<slot name="trailing">
<span slot="trailing">
${this.controllers.element.selectedIds.size || this.controllers.element.isOpen
Expand Down
6 changes: 1 addition & 5 deletions storybook/stories/person/person-select.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { html } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';

import type { Meta, StoryObj } from '@storybook/web-components';
import { setCustomElementsManifest } from '@storybook/web-components';
import PersonSelect, { PersonSelectElementProps, PersonSelectEvent } from '@equinor/fusion-wc-person/select';
import cem from '@equinor/fusion-wc-person/custom-elements.json';

import { faker } from '@faker-js/faker';
import { personProviderDecorator } from './person-provider';
Expand All @@ -13,11 +11,9 @@ PersonSelect;

faker.seed(1);

setCustomElementsManifest(cem);

type Story = StoryObj<PersonSelectElementProps>;

const meta: Meta<typeof cem> = {
const meta: Meta<typeof PersonSelect> = {
title: 'select',
component: 'fwc-person-select',
decorators: [personProviderDecorator],
Expand Down