Skip to content

Commit

Permalink
feat(text-input): controled input
Browse files Browse the repository at this point in the history
  • Loading branch information
Draeken committed Oct 25, 2018
1 parent 4dfaf9c commit 3f6e03b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/web/text-input/text-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum TextInputStatus {
export interface TextInputProps extends CustomableProps {
label: string;
value: string;
onNewVal: (val: string) => void;
labelType?: LabelType;
status?: TextInputStatus;
assistiveMsg?: string;
Expand Down Expand Up @@ -214,17 +215,28 @@ interface TextInputState {
* icons (leading & trailing)
*/
class TextInputImpl extends React.PureComponent<TextInputProps> {
inputRef: React.RefObject<any>;
state: TextInputState = {
isActive: false,
};
handleFocus = isFocused => () => {
this.setState({ isActive: isFocused });
this.setState({ isActive: isFocused }, () => {
if (!isFocused) {
return;
}
this.inputRef.current.focus();
});
};
constructor(props) {
super(props);
this.inputRef = React.createRef();
}

render() {
const {
label,
value,
onNewVal,
labelType = LabelType.float,
status = TextInputStatus.enabled,
assistiveMsg,
Expand All @@ -240,17 +252,21 @@ class TextInputImpl extends React.PureComponent<TextInputProps> {
TypographyProps({ scale: 'Caption', baselineTop: 20 }),
LabelClass(theme, labelType, value, status, this.state.isActive)
);
const inputProps = mergeProps(TypographyProps({ scale: 'Subtitle1' }), InputClass(theme));
const inputProps = mergeProps(TypographyProps({ scale: 'Subtitle1' }), InputClass(theme), {
value,
onChange: onNewVal,
});
const activIndicatorProps = ActiveIndicatorClass(theme, status, this.state.isActive);
return (
<div
ref={forwardedRef}
{...hostProps}
onClick={this.handleFocus(true)}
onFocus={this.handleFocus(true)}
onBlur={this.handleFocus(false)}
>
<div {...labelProps}>{label}</div>
<input {...inputProps} />
<input {...inputProps} ref={this.inputRef} />
<div {...activIndicatorProps} />
</div>
);
Expand Down

0 comments on commit 3f6e03b

Please sign in to comment.