Skip to content

Commit e78af1f

Browse files
authored
feat: add value parameter to input showCount.formatter (#20)
1 parent d82402c commit e78af1f

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ render(<Input placeholder="input" allowClear />, mountNode);
4848
| disabled | boolean | false | Whether the input is disabled |
4949
| id | string | - | The ID for input |
5050
| maxLength | number | - | The max length |
51-
| showCount | boolean &#124; { formatter: ({ count: number, maxLength?: number }) => ReactNode } | false | Whether show text count |
51+
| showCount | boolean &#124; { formatter: ({ value: string, count: number, maxLength?: number }) => ReactNode } | false | Whether show text count |
5252
| prefix | ReactNode | - | The prefix icon for the Input |
5353
| suffix | ReactNode | - | The suffix icon for the Input |
5454
| type | string | `text` | The type of input, see: [MDN](https://developer.mozilla.org/docs/Web/HTML/Element/input#Form_%3Cinput%3E_types)( use `Input.TextArea` instead of `type="textarea"`) |

src/Input.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ const Input = forwardRef<InputRef, InputProps>((props, ref) => {
156156
const hasMaxLength = Number(maxLength) > 0;
157157

158158
if (suffix || showCount) {
159-
const valueLength = [...fixControlledValue(value)].length;
159+
const val = fixControlledValue(value);
160+
const valueLength = [...val].length;
160161
const dataCount =
161162
typeof showCount === 'object'
162-
? showCount.formatter({ count: valueLength, maxLength })
163+
? showCount.formatter({ value: val, count: valueLength, maxLength })
163164
: `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
164165

165166
return (

src/interface.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export interface BaseInputProps extends CommonInputProps {
3636
}
3737

3838
export interface ShowCountProps {
39-
formatter: (args: { count: number; maxLength?: number }) => ReactNode;
39+
formatter: (args: {
40+
value: string;
41+
count: number;
42+
maxLength?: number;
43+
}) => ReactNode;
4044
}
4145

4246
export interface InputProps

tests/index.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,16 @@ describe('should support showCount', () => {
133133
prefixCls="rc-input"
134134
maxLength={5}
135135
showCount={{
136-
formatter: ({ count, maxLength }) => `${count}, ${maxLength}`,
136+
formatter: ({ value, count, maxLength }) =>
137+
`${value}, ${count}, ${maxLength}`,
137138
}}
138139
value="12345"
139140
/>,
140141
);
141142
expect(container.querySelector('input')?.value).toBe('12345');
142143
expect(
143144
container.querySelector('.rc-input-show-count-suffix')?.innerHTML,
144-
).toBe('5, 5');
145+
).toBe('12345, 5, 5');
145146
});
146147
});
147148

0 commit comments

Comments
 (0)