Skip to content

Commit 6116a3d

Browse files
committed
feat: fix suffix won't get updated on prop change
1 parent 220774a commit 6116a3d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/@next/TextInput/TextInput.stories.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import { Story, Meta } from '@storybook/react';
33
import { BaseContainer } from '../../Layout/GlintsContainer/GlintsContainer';
44
import { TextInputProps, TextInput } from './TextInput';
@@ -21,14 +21,22 @@ Interactive.args = {
2121
disabled: false,
2222
};
2323

24-
const WithIconsTemplate: Story<TextInputProps> = args => (
25-
<TextInput {...args} />
26-
);
27-
export const WithPrefixAndSuffix = WithIconsTemplate.bind({});
24+
const WithPrefixAndSuffixTemplate: Story<TextInputProps> = args => {
25+
const [value, setValue] = useState('Hi');
26+
27+
return (
28+
<TextInput
29+
{...args}
30+
value={value}
31+
onChange={e => setValue(e.target.value)}
32+
suffix={<div>{value.length}/20</div>}
33+
/>
34+
);
35+
};
36+
37+
export const WithPrefixAndSuffix = WithPrefixAndSuffixTemplate.bind({});
2838
WithPrefixAndSuffix.args = {
2939
placeholder: 'Placeholder',
3040
prefix: <Icon name="ri-search" />,
31-
suffix: <div>2/20</div>,
32-
value: 'Hi',
3341
disabled: false,
3442
};

src/@next/TextInput/TextInput.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { Icon } from '../Icon';
33
import { Input, InputProps } from '../Input/Input';
44

@@ -32,6 +32,10 @@ export const TextInput = React.forwardRef<HTMLInputElement, TextInputProps>(
3232
onChange?.(e);
3333
};
3434

35+
useEffect(() => {
36+
setSuffixValue(suffix);
37+
}, [suffix]);
38+
3539
return (
3640
<Input
3741
ref={ref}

0 commit comments

Comments
 (0)