forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move product editor utils to product editor package (woocommerce#36730)
* Move product editor utils to product editor package * Add changelog entries * Move remaining utils * Move util import/exports to separate index file
- Loading branch information
Showing
38 changed files
with
253 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: add | ||
|
||
Add product editor utils |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const NUMBERS_AND_ALLOWED_CHARS = '[^-0-9%s1%s2]'; | ||
export const NUMBERS_AND_DECIMAL_SEPARATOR = '[^-\\d\\%s]+'; | ||
export const ONLY_ONE_DECIMAL_SEPARATOR = '[%s](?=%s*[%s])'; | ||
// This should never be a real slug value of any existing shipping class | ||
export const ADD_NEW_SHIPPING_CLASS_OPTION_VALUE = | ||
'__ADD_NEW_SHIPPING_CLASS_OPTION__'; | ||
export const UNCATEGORIZED_CATEGORY_SLUG = 'uncategorized'; | ||
export const PRODUCT_VARIATION_TITLE_LIMIT = 32; | ||
export const STANDARD_RATE_TAX_CLASS_SLUG = 'standard'; |
39 changes: 39 additions & 0 deletions
39
packages/js/product-editor/src/utils/format-currency-display-value.ts
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,39 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { NUMBERS_AND_ALLOWED_CHARS } from './constants'; | ||
|
||
type CurrencyConfig = { | ||
code: string; | ||
symbol: string; | ||
symbolPosition: string; | ||
decimalSeparator: string; | ||
priceFormat: string; | ||
thousandSeparator: string; | ||
precision: number; | ||
}; | ||
|
||
/** | ||
* Cleans and formats the currency value shown to the user. | ||
* | ||
* @param {string} value Form value. | ||
* @param {Object} currencyConfig Currency context. | ||
* @return {string} Display value. | ||
*/ | ||
export const formatCurrencyDisplayValue = ( | ||
value: string, | ||
currencyConfig: CurrencyConfig, | ||
format: ( number: number | string ) => string | ||
) => { | ||
const { decimalSeparator, thousandSeparator } = currencyConfig; | ||
|
||
const regex = new RegExp( | ||
NUMBERS_AND_ALLOWED_CHARS.replace( '%s1', decimalSeparator ).replace( | ||
'%s2', | ||
thousandSeparator | ||
), | ||
'g' | ||
); | ||
|
||
return value === undefined ? value : format( value ).replace( regex, '' ); | ||
}; |
24 changes: 24 additions & 0 deletions
24
packages/js/product-editor/src/utils/get-checkbox-tracks.ts
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,24 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { ChangeEvent } from 'react'; | ||
import { Product } from '@woocommerce/data'; | ||
import { recordEvent } from '@woocommerce/tracks'; | ||
|
||
/** | ||
* Get additional props to be passed to all checkbox inputs. | ||
* | ||
* @param name Name of the checkbox. | ||
* @return Props. | ||
*/ | ||
export function getCheckboxTracks< T = Product >( name: string ) { | ||
return { | ||
onChange: ( | ||
isChecked: ChangeEvent< HTMLInputElement > | T[ keyof T ] | ||
) => { | ||
recordEvent( `product_checkbox_${ name }`, { | ||
checked: isChecked, | ||
} ); | ||
}, | ||
}; | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/js/product-editor/src/utils/get-currency-symbol-props.ts
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,26 @@ | ||
type CurrencyConfig = { | ||
code: string; | ||
symbol: string; | ||
symbolPosition: string; | ||
decimalSeparator: string; | ||
priceFormat: string; | ||
thousandSeparator: string; | ||
precision: number; | ||
}; | ||
|
||
/** | ||
* Get input props for currency related values and symbol positions. | ||
* | ||
* @param {Object} currencyConfig - Currency context | ||
* @return {Object} Props. | ||
*/ | ||
export const getCurrencySymbolProps = ( currencyConfig: CurrencyConfig ) => { | ||
const { symbol, symbolPosition } = currencyConfig; | ||
const currencyPosition = symbolPosition.includes( 'left' ) | ||
? 'prefix' | ||
: 'suffix'; | ||
|
||
return { | ||
[ currencyPosition ]: symbol, | ||
}; | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { formatCurrencyDisplayValue } from './format-currency-display-value'; | ||
import { getCheckboxTracks } from './get-checkbox-tracks'; | ||
import { getCurrencySymbolProps } from './get-currency-symbol-props'; | ||
import { getDerivedProductType } from './get-derived-product-type'; | ||
import { getProductStatus, PRODUCT_STATUS_LABELS } from './get-product-status'; | ||
import { | ||
getProductStockStatus, | ||
getProductStockStatusClass, | ||
} from './get-product-stock-status'; | ||
import { getProductTitle, AUTO_DRAFT_NAME } from './get-product-title'; | ||
import { | ||
getProductVariationTitle, | ||
getTruncatedProductVariationTitle, | ||
} from './get-product-variation-title'; | ||
import { preventLeavingProductForm } from './prevent-leaving-product-form'; | ||
|
||
export { | ||
AUTO_DRAFT_NAME, | ||
formatCurrencyDisplayValue, | ||
getCheckboxTracks, | ||
getCurrencySymbolProps, | ||
getDerivedProductType, | ||
getProductStatus, | ||
getProductStockStatus, | ||
getProductStockStatusClass, | ||
getProductTitle, | ||
getProductVariationTitle, | ||
getTruncatedProductVariationTitle, | ||
preventLeavingProductForm, | ||
PRODUCT_STATUS_LABELS, | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,9 @@ | ||
declare global { | ||
interface Window { | ||
wcAdminFeatures: Record< string, boolean >; | ||
} | ||
} | ||
|
||
/*~ If your module exports nothing, you'll need this line. Otherwise, delete it */ | ||
export {}; | ||
|
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
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
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
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.