Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/core] Indicator: Add count support (#2237)
* [@mantine/core] Indicator: count support * [@mantine/core] Indicatoe: isShow back to disabled * [@mantine/core] Indicator: typescript error fix * [@mantine/core] MachineNumber: test pass * [docs] Indicator docs optimization * [@mantine/core] Indicator: Resolve conflicts Co-authored-by: Aster <x.b.he@accenture.com>
- Loading branch information
1 parent
680c0ca
commit a8c45f8
Showing
22 changed files
with
944 additions
and
85 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
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 @@ | ||
import { createStyles } from '@mantine/styles'; | ||
|
||
export default createStyles(() => ({ | ||
base: { | ||
display: 'flex', | ||
alignItems: 'center', | ||
overflow: 'hidden', | ||
}, | ||
})); |
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,63 @@ | ||
import React, { useState, forwardRef, useMemo, useEffect } from 'react'; | ||
import { isString, isDef, usePrevious } from '@mantine/utils'; | ||
import { MachineNumber } from './MachineNumber'; | ||
import useStyles from './Machine.styles'; | ||
|
||
interface MachineNumberProps { | ||
value: number | string; | ||
max: number; | ||
} | ||
|
||
export const Machine = forwardRef<HTMLDivElement, MachineNumberProps>(({ value = 0, max }, ref) => { | ||
const [oldValue, setOldValue] = useState<number>(); | ||
const [newValue, setNewValue] = useState<number>(); | ||
const prevValueRef = usePrevious(value); | ||
|
||
useEffect(() => { | ||
if (isString(value)) { | ||
setOldValue(undefined); | ||
setNewValue(undefined); | ||
} else if (isString(prevValueRef)) { | ||
setOldValue(undefined); | ||
setNewValue(value); | ||
} else { | ||
setOldValue(prevValueRef); | ||
setNewValue(value); | ||
} | ||
}, [value, prevValueRef]); | ||
|
||
const numbers = useMemo(() => { | ||
if (isString(value)) return []; | ||
if (value < 1) return [0]; | ||
const result: number[] = []; | ||
let currentValue = value; | ||
if (isDef(max)) { | ||
currentValue = Math.min(max, currentValue); | ||
} | ||
while (currentValue >= 1) { | ||
result.push(currentValue % 10); | ||
currentValue /= 10; | ||
currentValue = Math.floor(currentValue); | ||
} | ||
result.reverse(); | ||
return result; | ||
}, [value, max]); | ||
|
||
const { classes } = useStyles(null, { name: 'machine' }); | ||
|
||
return isString(value) ? ( | ||
<span ref={ref}>{value}</span> | ||
) : ( | ||
<span ref={ref} className={classes.base}> | ||
{numbers.map((number, i) => ( | ||
<MachineNumber | ||
key={numbers.length - i - 1} | ||
value={number} | ||
oldOriginalNumber={oldValue} | ||
newOriginalNumber={newValue} | ||
/> | ||
))} | ||
{isDef(max) && value > max && <span>+</span>} | ||
</span> | ||
); | ||
}); |
Oops, something went wrong.