-
Notifications
You must be signed in to change notification settings - Fork 61
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: search product control implementations #2547
Open
ramondorosario-ct
wants to merge
13
commits into
vtex:main
Choose a base branch
from
cubos-vtex:feat/search-product-control-implementations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
37d34d4
feat: search product item control
ramondorosario-ct 7255092
feat: rename file
ramondorosario-ct a3177b0
style: tokens
ramondorosario-ct 28e054a
style: ident
ramondorosario-ct 09f0b28
feat: add skuMatrixControl prop
ramondorosario-ct 5a7bf2b
feat: change timeout time
ramondorosario-ct e02f24c
feat: search product item control
ramondorosario-ct 2c85f5d
Merge branch 'feat/search-product-item-control' of github.com:cubos-v…
ramondorosario-ct 5e39413
feat: add quantity and onChangeQuantity props
ramondorosario-ct 18c5d5d
chore: file formater
ramondorosario-ct 8207add
feat: search product control implementations
ramondorosario-ct f8573ff
feat: adjust control behavior on mobile version
ramondorosario-ct d5f06a0
Merge branch 'feat/search-product-item-control' into feat/search-prod…
ramondorosario-ct File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
124 changes: 124 additions & 0 deletions
124
packages/components/src/molecules/SearchProducts/SearchProductItemControl.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,124 @@ | ||
import React, { forwardRef, HTMLAttributes } from 'react' | ||
import { Badge, Icon, IconButton, Input, Loader, QuantitySelector } from '../..' | ||
type StatusButtonAddToCartType = 'default' | 'inProgress' | 'completed' | ||
|
||
export interface SearchProductItemControlProps | ||
extends Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'onClick'> { | ||
children: React.ReactNode | ||
availability: boolean | ||
hasVariants: boolean | ||
skuMatrixControl: React.ReactNode | ||
quantity: number | ||
onClick?(e: React.MouseEvent<HTMLButtonElement>): void | ||
onChangeQuantity(value: number): void | ||
} | ||
|
||
const SearchProductItemControl = forwardRef< | ||
HTMLDivElement, | ||
SearchProductItemControlProps | ||
>(function SearchProductItemControl( | ||
{ | ||
availability, | ||
children, | ||
hasVariants, | ||
skuMatrixControl, | ||
quantity, | ||
onClick, | ||
onChangeQuantity, | ||
...otherProps | ||
}, | ||
ref | ||
) { | ||
const [statusAddToCart, setStatusAddToCart] = | ||
React.useState<StatusButtonAddToCartType>('default') | ||
function stopPropagationClick(e: React.MouseEvent) { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
} | ||
function handleAddToCart(event: React.MouseEvent<HTMLButtonElement>) { | ||
if (onClick) { | ||
setStatusAddToCart('inProgress') | ||
|
||
setTimeout(() => { | ||
setStatusAddToCart('completed') | ||
onClick(event) | ||
}, 1000) | ||
|
||
setTimeout(() => { | ||
setStatusAddToCart('default') | ||
onChangeQuantity(1) | ||
}, 2000) | ||
} | ||
} | ||
|
||
const getIcon = React.useCallback(() => { | ||
switch (statusAddToCart) { | ||
case 'inProgress': | ||
return <Loader /> | ||
case 'completed': | ||
return <Icon name="Checked" width={24} height={24} /> | ||
default: | ||
return <Icon name="ShoppingCart" width={24} height={24} /> | ||
} | ||
}, [statusAddToCart]) | ||
|
||
const showSKUMatrixControl = availability && hasVariants; | ||
const isMobile = window.innerWidth <= 768 | ||
|
||
return ( | ||
<div | ||
ref={ref} | ||
data-fs-search-product-item-control | ||
onClick={stopPropagationClick} | ||
{...otherProps} | ||
> | ||
<div data-fs-search-product-item-control-content> | ||
{!availability && ( | ||
<Badge | ||
data-fs-search-product-item-control-badge | ||
variant="warning" | ||
> | ||
Out of Stock | ||
</Badge> | ||
)} | ||
{children} | ||
</div> | ||
{availability && !hasVariants && ( | ||
<div | ||
data-fs-search-product-item-control-actions | ||
role="group" | ||
onClick={stopPropagationClick} | ||
> | ||
{!isMobile && ( | ||
<QuantitySelector | ||
disabled={statusAddToCart !== 'default'} | ||
initial={quantity} | ||
onChange={onChangeQuantity} | ||
/> | ||
)} | ||
|
||
{isMobile && ( | ||
<Input | ||
data-fs-product-item-control-input | ||
type="number" | ||
min={1} | ||
value={quantity} | ||
onChange={(e) => onChangeQuantity(e.target.valueAsNumber)} | ||
/> | ||
)} | ||
|
||
<IconButton | ||
variant="primary" | ||
aria-label="Add product to cart" | ||
onClick={handleAddToCart} | ||
disabled={statusAddToCart === 'inProgress'} | ||
icon={getIcon()} | ||
/> | ||
</div> | ||
)} | ||
|
||
{showSKUMatrixControl && skuMatrixControl} | ||
</div> | ||
) | ||
}) | ||
export default SearchProductItemControl |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use a
fs/button
instead?