-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commerce): atomic-product-field-condition (#3959)
Related to #3956 Adds the `atomic-product-field-condition` component to enable conditional rendering based on commerce-specific fields in Coveo's UI Kit. - Implements the component with properties `ifDefined` and `ifNotDefined` to check for the presence or absence of specified fields. - Introduces `mustMatch` and `mustNotMatch` properties for conditional rendering based on matching field values, utilizing `ProductTemplatesHelpers`. - Ensures the component evaluates conditions on component load and removes itself from the DOM if conditions are not met, maintaining clean template structure. - Utilizes `@ProductContext` to access the current product's properties for condition evaluation. --- KIT-3058 For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/coveo/ui-kit/issues/3956?shareId=c1d4fa4c-3d58-4aab-a30d-db1259b300e5). --------- Co-authored-by: GitHub Actions Bot <> Co-authored-by: ylakhdar <[email protected]>
- Loading branch information
1 parent
a32fb64
commit 5e14eec
Showing
5 changed files
with
129 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
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
59 changes: 59 additions & 0 deletions
59
...src/components/commerce/atomic-product-field-condition/atomic-product-field-condition.tsx
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,59 @@ | ||
import {Product, ProductTemplateCondition} from '@coveo/headless/commerce'; | ||
import {Component, Prop, h, Element} from '@stencil/core'; | ||
import {MapProp} from '../../../utils/props-utils'; | ||
import {ProductContext} from '../product-template-components/product-template-decorators'; | ||
import { | ||
makeDefinedConditions, | ||
makeMatchConditions, | ||
} from '../product-template/product-template-common'; | ||
|
||
/** | ||
* The `atomic-product-field-condition` component takes a list of conditions that, if fulfilled, apply the template in which it's defined. | ||
* The condition properties can be based on any top-level product property of the `product` object, not restricted to fields (e.g., `ec_name`). | ||
* @internal | ||
*/ | ||
@Component({ | ||
tag: 'atomic-product-field-condition', | ||
shadow: false, | ||
}) | ||
export class AtomicProductFieldCondition { | ||
@Element() host!: HTMLElement; | ||
|
||
/** | ||
* Verifies whether the specified fields are defined. | ||
*/ | ||
@Prop({reflect: true}) ifDefined?: string; | ||
/** | ||
* Verifies whether the specified fields are not defined. | ||
*/ | ||
@Prop({reflect: true}) ifNotDefined?: string; | ||
|
||
@MapProp({splitValues: true}) mustMatch: Record<string, string[]> = {}; | ||
|
||
@MapProp({splitValues: true}) mustNotMatch: Record<string, string[]> = {}; | ||
|
||
private conditions: ProductTemplateCondition[] = []; | ||
private shouldBeRemoved = false; | ||
|
||
@ProductContext() private product!: Product; | ||
|
||
public componentWillLoad() { | ||
this.conditions = makeDefinedConditions(this.ifDefined, this.ifNotDefined); | ||
this.conditions.push( | ||
...makeMatchConditions(this.mustMatch, this.mustNotMatch) | ||
); | ||
} | ||
|
||
public render() { | ||
if (!this.conditions.every((condition) => condition(this.product))) { | ||
this.shouldBeRemoved = true; | ||
return ''; | ||
} | ||
|
||
return <slot />; | ||
} | ||
|
||
public componentDidLoad() { | ||
this.shouldBeRemoved && this.host.remove(); | ||
} | ||
} |
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