woocommerce/plugins/woocommerce-blocks/assets/js/blocks
Manish Menaria ba4f3d4d29
Add price range filter to Product Collection block (#42858)
* Add price range filter to Product Collection block

This update introduces a price range filter feature to Product Collection. Changes include:

1. Constants Update:
   - Added `priceRange` as undefined in `DEFAULT_QUERY` and `DEFAULT_FILTERS` in `constants.ts`.

2. Style Adjustments:
   - Added CSS for `.wc-block-product-price-range-control` in `editor.scss` to align the input text to the end.

3. Component Integration:
   - In `inspector-controls/index.tsx`, the `PriceRangeControl` component is now imported and integrated.

4. New Components:
   - `PriceTextField.tsx` and `PriceRangeControl/index.tsx` have been created to handle price range inputs in the Product Collection block.

5. Backend Integration:
   - `PriceRange` interface added in `types.ts` for type support.
   - In `ProductCollection.php`, a filter (`add_price_range_filter`) is added to modify the main query based on the price range, including adjustments for tax considerations.

Overall, this enhancement allows users to filter products within a specific price range. The backend adjustments ensure that the filtering respects tax settings and displays accurate prices.

* Fix formatting

* Fix: Price range filter not working on Editor

* Improve: Share logic between Frontend & Editor

* Add changelog

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Remove duplicate changelog file

* Enhanced Input Control for Price Fields

Key changes include:

1. **Switch to Input Control**: Replaced the NumberControl component with the more versatile InputControl. This offers better handling of currency formatting and user input.

2. **Currency Formatting Logic**: Added robust logic for formatting numbers according to the currency settings. This includes handling thousand separators, decimal places, and currency symbols.

3. **String-to-Number Conversion**: Implemented a function to convert user-entered strings back to numbers, accounting for currency symbols and separators. This ensures accurate parsing of user input for processing.

4. **Input Handling Improvements**: Modified the onChange handlers for minimum and maximum price inputs. Now, they correctly handle edge cases like undefined or zero values, maintaining consistency in the user interface and data processing.

* Refactor price range query handling

1. Introduction of a new method `get_price_range_query_args()` to encapsulate the logic for handling price range queries, especially for the two edge cases:
   - Prices excluding tax displayed including tax.
   - Prices including tax displayed excluding tax.

2. Removal of direct conditionals in the `get_query_results()` method, replacing them with a call to the new `get_price_range_query_args()` method. This makes the code more modular and easier to understand.

This refactor enhances readability and maintainability of the code, ensuring that special cases in price range filtering are handled more effectively.

* Remove unnecessary suffix prop

* Refactor PriceTextField formatting logic for currency

1. Conditional application of thousand separators: The code now checks for the existence of `currency.thousandSeparator` before applying it. This prevents potential errors when the separator is undefined.

2. Simplified decimal separator handling: Introduced a fallback for the decimal separator, defaulting to a period ('.') if not specified by the currency settings.

3. Enhanced readability and documentation: Added comments to clarify the purpose of code blocks, especially where currency symbols are added or removed, and where value normalization occurs.

4. Function renaming for clarity: Renamed `formatValueWithCurrencySymbol` to `formatCurrency`, which better reflects its purpose.

* Fix onBlur issue with PriceTextField component

Refactored the PriceTextField component to utilize useState for better state management. This change introduces a local state variable, 'newValue', to store the current value. The state updates occur in the handleOnChange function, ensuring that the component's state is managed efficiently. Additionally, a new function, handleOnBlur, is implemented to handle the onBlur event, updating the component's state when focus is lost. The handleEnterKeyPress function captures the 'Enter' key press, providing a more user-friendly experience by allowing users to confirm their input with the Enter key.

* Fix linting error

---------

Co-authored-by: github-actions <github-actions@github.com>
2023-12-21 12:45:20 +05:30
..
active-filters Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
attribute-filter Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
breadcrumbs Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
cart Link cross-sells product image to product page (#42904) 2023-12-21 11:13:47 +07:00
cart-checkout-shared Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
catalog-sorting Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
checkout Add Checkbox support for Additional Fields in Checkout Block (#42780) 2023-12-18 13:21:36 +01:00
classic-shortcode Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
classic-template Product gallery/legacy/transform (#42719) 2023-12-14 05:47:48 -08:00
collection-filters Add: Active Filters block powered by Interactivity API (#42008) 2023-12-19 17:29:33 +07:00
customer-account Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
featured-items Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
filter-wrapper Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
handpicked-products Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
migration-products-to-product-collection Simplify Product Collection query attributes by removing attributes which are never used (https://github.com/woocommerce/woocommerce-blocks/pull/11259) 2023-10-18 12:47:55 +02:00
mini-cart Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
order-confirmation Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
page-content-wrapper update text domain in plugins/woocommerce-blocks (#42717) 2023-12-12 14:12:36 -08:00
price-filter Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-best-sellers Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-categories Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-category Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-collection Add price range filter to Product Collection block (#42858) 2023-12-21 12:45:20 +05:30
product-gallery Product Gallery: Simplify the Product Gallery Pop-up (#42797) 2023-12-20 19:03:01 +01:00
product-new Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-on-sale Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-query Remove padding for the products query when background is added (#42817) 2023-12-18 11:14:43 +01:00
product-results-count Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-search Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-tag Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
product-template update text domain in plugins/woocommerce-blocks (#42717) 2023-12-12 14:12:36 -08:00
product-top-rated Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
products Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
products-by-attribute Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
rating-filter Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
reviews Reviews by Product block: fix encoded in input aria-label (#42944) 2023-12-19 13:05:18 +01:00
shared/styles Add logic of Upgrade Notice after upgrading Products to Product Collection (https://github.com/woocommerce/woocommerce-blocks/pull/10267) 2023-08-18 08:25:15 +02:00
single-product Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
stock-filter Fixed Blocks Linting Errors (#42727) 2023-12-12 15:05:20 -08:00
store-notices update text domain in plugins/woocommerce-blocks (#42717) 2023-12-12 14:12:36 -08:00
README.md Remove propTypes definitions from Newest Products block (https://github.com/woocommerce/woocommerce-blocks/pull/9613) 2023-05-29 13:12:36 +07:00

README.md

Blocks

Our blocks are generally made up of up to 4 files:

|- block.js
|- editor.scss
|- index.js
|- style.scss

The only required file is index.js, this sets up the block using registerBlockType. Each block has edit and save functions.

The scss files are split so that things in style are added to the editor and frontend, while styles in editor are only added to the editor. Most of our blocks should use core components that won't need CSS though.

Editing

A simple edit function can live in index.js, but most blocks are a little more complicated, so the edit function instead returns a Block component, which lives in block.js. By using a component, we can use React lifecycle methods to fetch data or save state.

The Newest Products block is a good example to read over, this is a simple block that fetches the products and renders them using the ProductPreview component.

We include settings in the sidebar, called the Inspector in gutenberg. See an example of this.

Other blocks have the concept of an "edit state", like when you need to pick a product in the Featured Product block, or pick a category in the Products by Category block.