generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
count
for customize count logic (#47)
* chore: init of len * chore: support len of demo * feat: support cuter * test: add test case * test: use rc-test * test: update snapshot * test: add test case * chore: clean up
- Loading branch information
Showing
11 changed files
with
291 additions
and
75 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
.rc-input { | ||
&-out-of-range { | ||
color: red; | ||
} | ||
|
||
&-affix-wrapper { | ||
padding: 2px 8px; | ||
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 |
---|---|---|
@@ -1,10 +1,69 @@ | ||
import Input from 'rc-input'; | ||
import type { FC } from 'react'; | ||
import React from 'react'; | ||
import '../../assets/index.less'; | ||
import Input from 'rc-input'; | ||
|
||
const sharedHeadStyle: React.CSSProperties = { | ||
margin: 0, | ||
padding: 0, | ||
}; | ||
|
||
const Demo: FC = () => { | ||
return <Input prefixCls="rc-input" showCount />; | ||
return ( | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: 16, | ||
alignItems: 'start', | ||
}} | ||
> | ||
<h3 style={sharedHeadStyle}>Native</h3> | ||
<Input prefixCls="rc-input" showCount defaultValue="👨👩👧👦" /> | ||
<Input prefixCls="rc-input" showCount defaultValue="👨👩👧👦" maxLength={20} /> | ||
<h3 style={sharedHeadStyle}>Count</h3> | ||
<h4 style={sharedHeadStyle}>Only Max</h4> | ||
<Input | ||
placeholder="count.max" | ||
prefixCls="rc-input" | ||
defaultValue="🔥" | ||
count={{ | ||
show: true, | ||
max: 5, | ||
}} | ||
/> | ||
<h4 style={sharedHeadStyle}>Customize strategy</h4> | ||
<Input | ||
placeholder="Emoji count 1" | ||
prefixCls="rc-input" | ||
defaultValue="🔥" | ||
count={{ | ||
show: true, | ||
max: 5, | ||
strategy: (val) => | ||
[...new (Intl as any).Segmenter().segment(val)].length, | ||
}} | ||
/> | ||
<h4 style={sharedHeadStyle}>Customize exceedFormatter</h4> | ||
<Input | ||
placeholder="Emoji count 1" | ||
prefixCls="rc-input" | ||
defaultValue="🔥" | ||
count={{ | ||
show: true, | ||
max: 5, | ||
exceedFormatter: (val, { max }) => { | ||
const segments = [...new (Intl as any).Segmenter().segment(val)]; | ||
|
||
return segments | ||
.filter((seg) => seg.index + seg.segment.length <= max) | ||
.map((seg) => seg.segment) | ||
.join(''); | ||
}, | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Demo; |
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,48 @@ | ||
import * as React from 'react'; | ||
import type { InputProps } from '..'; | ||
import type { CountConfig, ShowCountFormatter } from '../interface'; | ||
|
||
type ForcedCountConfig = Omit<CountConfig, 'show'> & | ||
Pick<Required<CountConfig>, 'strategy'> & { | ||
show: boolean; | ||
showFormatter?: ShowCountFormatter; | ||
}; | ||
|
||
/** | ||
* Cut `value` by the `count.max` prop. | ||
*/ | ||
export function inCountRange(value: string, countConfig: ForcedCountConfig) { | ||
if (!countConfig.max) { | ||
return true; | ||
} | ||
|
||
const count = countConfig.strategy(value); | ||
return count <= countConfig.max; | ||
} | ||
|
||
export default function useCount( | ||
count?: CountConfig, | ||
showCount?: InputProps['showCount'], | ||
) { | ||
return React.useMemo<ForcedCountConfig>(() => { | ||
let mergedConfig = count; | ||
|
||
if (!count) { | ||
mergedConfig = { | ||
show: | ||
typeof showCount === 'object' && showCount.formatter | ||
? showCount.formatter | ||
: !!showCount, | ||
}; | ||
} | ||
|
||
const { show, ...rest } = mergedConfig!; | ||
|
||
return { | ||
...rest, | ||
show: !!show, | ||
showFormatter: typeof show === 'function' ? show : undefined, | ||
strategy: rest.strategy || ((value) => value.length), | ||
}; | ||
}, [count, showCount]); | ||
} |
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.