Skip to content

Commit 816dade

Browse files
committed
feat: add TextInputFocus
1 parent 2919f97 commit 816dade

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

src/text-input/text-input.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { css } from 'emotion';
22
import * as React from 'react';
33
import { TypographyProps } from '../typography/typography';
44
import { merge, mergeProps, pipe } from '../util/hoc.util';
5-
import { ThemeContext, PaletteTheme } from '../util/theme';
5+
import { PaletteTheme, ThemeContext } from '../util/theme';
66

77
export enum LabelType {
88
float,
@@ -251,7 +251,15 @@ export const TextInput: React.FunctionComponent<TextInputPropsExtended> = React.
251251
...defaultHostProps
252252
} = props;
253253
const theme = defaultTheme(React.useContext(ThemeContext));
254-
const hostProps = mergeProps(TextInputRootClass(theme), defaultHostProps);
254+
const hostProps = mergeProps(
255+
TextInputRootClass(theme),
256+
{
257+
onClick: () => setActive(true),
258+
onFocus: () => setActive(true),
259+
onBlur: () => setActive(false),
260+
},
261+
defaultHostProps
262+
);
255263
const labelProps = mergeProps(
256264
TypographyProps({ scale: 'Caption', baselineTop: 20 }),
257265
LabelClass(theme, labelType, value, status, isActive)
@@ -262,17 +270,24 @@ export const TextInput: React.FunctionComponent<TextInputPropsExtended> = React.
262270
});
263271
const activIndicatorProps = ActiveIndicatorClass(theme, status, isActive);
264272
return (
265-
<div
266-
ref={forwardedRef}
267-
{...hostProps}
268-
onClick={() => setActive(true)}
269-
onFocus={() => setActive(true)}
270-
onBlur={() => setActive(false)}
271-
>
273+
<div ref={forwardedRef} {...hostProps}>
272274
<div {...labelProps}>{label}</div>
273275
<input {...inputProps} ref={inputRef} />
274276
<div {...activIndicatorProps} />
275277
</div>
276278
);
277279
}
278280
);
281+
282+
export const TextInputFocus: React.FunctionComponent<TextInputPropsExtended> = React.forwardRef(
283+
(props: TextInputPropsExtended, forwardedRef) => {
284+
const { value, onNewVal, ...defaultHostProps } = props;
285+
const [inputVal, setInputVal] = React.useState(value);
286+
React.useEffect(() => setInputVal(value), [value]);
287+
const inputOnNewVal = React.useCallback(e => setInputVal(e), []);
288+
const hostProps = mergeProps(defaultHostProps, { onBlur: () => onNewVal(inputVal) });
289+
return (
290+
<TextInput ref={forwardedRef} {...hostProps} value={inputVal} onNewVal={inputOnNewVal} />
291+
);
292+
}
293+
);

stories/text-input.stories.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { number } from '@storybook/addon-knobs';
22
import { storiesOf } from '@storybook/react';
33
import { css } from 'emotion';
44
import * as React from 'react';
5-
import { LabelType, TextInput, TextInputStatus } from '../src/text-input/text-input';
5+
import { LabelType, TextInput, TextInputStatus, TextInputFocus } from '../src/text-input/text-input';
66
import { ThemeContext } from '../src/util/theme';
77

88
const customTheme = () => ({
@@ -37,6 +37,13 @@ const DumpTextInput: React.FunctionComponent<{ label: string; [key: string]: any
3737
return <TextInput label={label} value={val} onNewVal={v => setVal(v)} {...otherProps} />;
3838
};
3939

40+
const DumpTextInputFocus: React.FunctionComponent<{ label: string; [key: string]: any }> = props => {
41+
const { label, ...otherProps } = props;
42+
const [val, setVal] = React.useState('');
43+
const onNewVal = (v: string) => setVal(v.toUpperCase());
44+
return <TextInputFocus label={label} value={val} onNewVal={onNewVal} {...otherProps} />;
45+
}
46+
4047
storiesOf('Component/Material/TextInput', module)
4148
.add('default textInput', () => <DumpTextInput label={'Default textInput'} />)
4249
.add('label fixed', () => <DumpTextInput label={'Fixed Label'} labelType={LabelType.fixed} />)
@@ -47,6 +54,7 @@ storiesOf('Component/Material/TextInput', module)
4754
.add('error textInput', () => (
4855
<DumpTextInput label={'Disabled textInput'} status={TextInputStatus.error} />
4956
))
57+
.add('on blur', () => <DumpTextInputFocus label={'updated on blur'} />)
5058
.add('Custom theme', () => (
5159
<ThemeContext.Provider value={customTheme()}>
5260
<DumpTextInput label={'My Input'} />

0 commit comments

Comments
 (0)