Skip to content

Commit

Permalink
fix(Input): input value by Chinese (#1632)
Browse files Browse the repository at this point in the history
* fix(Input): input value by Chinese

* fix(test): remove unnecessary code

Co-authored-by: Zhang Rui <zhangrui@growingio.com>
  • Loading branch information
Ryan Zhang and Zhang Rui committed Dec 7, 2021
1 parent da06b67 commit 1b9cfc8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/input/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useCallback, useMemo } from 'react';
import classNames from 'classnames';
import { InputProps } from './interface';
import usePrefixCls from '../utils/hooks/use-prefix-cls';
import useControlledState from '../utils/hooks/useControlledState';

const Input = React.forwardRef<HTMLSpanElement, InputProps>((props, ref) => {
const {
Expand All @@ -13,17 +12,15 @@ const Input = React.forwardRef<HTMLSpanElement, InputProps>((props, ref) => {
disabled,
className,
placeholder,
onChange,
onPressEnter,
onKeyPress,
style,
value: valueProp,
defaultValue,
value,
inputRef: propsInputRef,
...rest
} = props;

const [value, setValue] = useControlledState(valueProp, Array.isArray(defaultValue) ? undefined : defaultValue);

const prefixCls = usePrefixCls('input', customizePrefixCls);
const inputClass = useMemo(
() =>
Expand All @@ -49,6 +46,13 @@ const Input = React.forwardRef<HTMLSpanElement, InputProps>((props, ref) => {
[onPressEnter, onKeyPress]
);

const handleChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
onChange?.(e);
},
[onChange]
);

const prefixFcCls = useMemo(
() =>
classNames(`${prefixCls}__prefix`, {
Expand Down Expand Up @@ -81,7 +85,7 @@ const Input = React.forwardRef<HTMLSpanElement, InputProps>((props, ref) => {
{...rest}
value={value ?? ''}
disabled={disabled}
onChange={(event) => setValue(event.target.value)}
onChange={handleChange}
onKeyPress={handleKeyPress}
placeholder={placeholder}
ref={propsInputRef}
Expand Down
15 changes: 14 additions & 1 deletion src/input/demos/Input.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useCallback, useRef, useState } from 'react';
import { Story, Meta } from '@storybook/react/types-6-0';
import { action } from '@storybook/addon-actions';
import { PlusOutlined, FilterOutlined, EventsPresetOutlined } from '@gio-design/icons';
Expand Down Expand Up @@ -337,3 +337,16 @@ InputButtonWidth.args = {
prefix: <EventsPresetOutlined />,
};
InputButtonWidth.storyName = 'Input Button Width 100%';

const ControlInputExample = (args: InputProps) => {
const [value, setValue] = useState('');
const onChange = useCallback((e) => {
console.log('value', e.target.value);
setValue(e.target.value);
}, []);

return <Input {...args} value={value} onChange={onChange} style={{ width: 300 }} />;
};

export const ControlInput = ControlInputExample.bind({});
ControlInput.args = {};
15 changes: 14 additions & 1 deletion src/search-bar/demos/SearchBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useState } from 'react';
import { Story, Meta } from '@storybook/react/types-6-0';
import { action } from '@storybook/addon-actions';
import SearchBar from '../SearchBar';
Expand Down Expand Up @@ -136,3 +136,16 @@ DisableValue.args = {
value: '数据可视化',
disabled: true,
};

const ControlInputExample = (args: SearchBarProps) => {
const [value, setValue] = useState('');
const onChange = useCallback((e) => {
console.log('value', e.target.value);
setValue(e.target.value);
}, []);

return <SearchBar {...args} value={value} onChange={onChange} style={{ width: 300 }} />;
};

export const ControlInput = ControlInputExample.bind({});
ControlInput.args = {};

1 comment on commit 1b9cfc8

@vercel
Copy link

@vercel vercel bot commented on 1b9cfc8 Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.