Skip to content

Commit b944d0c

Browse files
committed
feat(price): 增加兜底逻辑
1 parent e9b18e4 commit b944d0c

1 file changed

Lines changed: 49 additions & 32 deletions

File tree

src/packages/price/price.tsx

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import classNames from 'classnames'
33
import { ComponentDefaults } from '@/utils/typings'
44
import { useRtl } from '@/packages/configprovider/index'
55
import { WebPriceProps, PriceColorEnum } from '@/types'
6+
import { shouldRenderPriceAsRaw } from '@/utils/should-render-price-raw'
67

78
const defaultProps = {
89
...ComponentDefaults,
@@ -36,10 +37,17 @@ export const Price: FunctionComponent<Partial<WebPriceProps>> = (props) => {
3637
const classPrefix = 'nut-price'
3738

3839
const rtl = useRtl()
40+
const isRenderPriceRaw = useMemo(
41+
() =>
42+
typeof originalPrice === 'string' &&
43+
shouldRenderPriceAsRaw(originalPrice),
44+
[originalPrice]
45+
)
3946

4047
const price = useMemo(() => {
48+
if (isRenderPriceRaw) return ''
4149
return originalPrice.toString().replace(/[^\d.]/g, '')
42-
}, [originalPrice])
50+
}, [originalPrice, isRenderPriceRaw])
4351

4452
const isCustomPriceColor = useMemo(() => {
4553
const specificPriceColor = Object.values(PriceColorEnum)
@@ -112,45 +120,54 @@ export const Price: FunctionComponent<Partial<WebPriceProps>> = (props) => {
112120
)
113121
}
114122

115-
return (
116-
<div
117-
className={`${classPrefix} ${classPrefix}-${isCustomPriceColor ? 'custom' : color} ${className}`}
118-
style={style}
119-
{...rest}
120-
>
121-
{symbol && position === 'before' ? renderSymbol() : null}
122-
<div
123-
className={`${classPrefix}-integer ${classPrefix}-integer-${size} ${
124-
line ? `${classPrefix}-line` : ''
125-
}`}
126-
style={priceColorStyle}
127-
>
128-
{formatThousands(price)}
129-
</div>
130-
{digits !== 0 ? (
131-
<>
132-
{checkPoint(price) || digits ? (
123+
const renderRawContent = () => <>{originalPrice}</>
124+
const renderInner = () => {
125+
return (
126+
<>
127+
{symbol && position === 'before' ? renderSymbol() : null}
128+
<div
129+
className={`${classPrefix}-integer ${classPrefix}-integer-${size} ${
130+
line ? `${classPrefix}-line` : ''
131+
}`}
132+
style={priceColorStyle}
133+
>
134+
{formatThousands(price)}
135+
</div>
136+
{digits !== 0 ? (
137+
<>
138+
{checkPoint(price) || digits ? (
139+
<div
140+
className={`${classPrefix}-decimal ${classPrefix}-decimal-${size} ${
141+
line ? `${classPrefix}-line` : ''
142+
}`}
143+
style={priceColorStyle}
144+
>
145+
.
146+
</div>
147+
) : null}
133148
<div
134149
className={`${classPrefix}-decimal ${classPrefix}-decimal-${size} ${
135150
line ? `${classPrefix}-line` : ''
136151
}`}
137152
style={priceColorStyle}
138153
>
139-
.
154+
{formatDecimal(price)}
140155
</div>
141-
) : null}
142-
<div
143-
className={`${classPrefix}-decimal ${classPrefix}-decimal-${size} ${
144-
line ? `${classPrefix}-line` : ''
145-
}`}
146-
style={priceColorStyle}
147-
>
148-
{formatDecimal(price)}
149-
</div>
150-
</>
151-
) : null}
156+
</>
157+
) : null}
152158

153-
{symbol && position === 'after' ? renderSymbol() : null}
159+
{symbol && position === 'after' ? renderSymbol() : null}
160+
</>
161+
)
162+
}
163+
164+
return (
165+
<div
166+
className={`${classPrefix} ${classPrefix}-${isCustomPriceColor ? 'custom' : color} ${className}`}
167+
style={style}
168+
{...rest}
169+
>
170+
{isRenderPriceRaw ? renderRawContent() : renderInner()}
154171
</div>
155172
)
156173
}

0 commit comments

Comments
 (0)