Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TextInput): Add Focus TextInput example #912

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ exports[`LoginForm with rememberMeLabel and no rememberMeAriaLabel generates con
label="Username"
>
<TextInput
aria-label={null}
className=""
id="pf-login-username-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-username-id"
Expand All @@ -41,11 +37,7 @@ exports[`LoginForm with rememberMeLabel and no rememberMeAriaLabel generates con
label="Password"
>
<TextInput
aria-label={null}
className=""
id="pf-login-password-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-password-id"
Expand Down Expand Up @@ -114,11 +106,7 @@ exports[`LoginForm with rememberMeLabel and rememberMeAriaLabel does not generat
label="Username"
>
<TextInput
aria-label={null}
className=""
id="pf-login-username-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-username-id"
Expand All @@ -136,11 +124,7 @@ exports[`LoginForm with rememberMeLabel and rememberMeAriaLabel does not generat
label="Password"
>
<TextInput
aria-label={null}
className=""
id="pf-login-password-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-password-id"
Expand Down Expand Up @@ -209,11 +193,7 @@ exports[`should render Login form 1`] = `
label="Username"
>
<TextInput
aria-label={null}
className=""
id="pf-login-username-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-username-id"
Expand All @@ -231,11 +211,7 @@ exports[`should render Login form 1`] = `
label="Password"
>
<TextInput
aria-label={null}
className=""
id="pf-login-password-id"
isDisabled={false}
isReadOnly={false}
isRequired={true}
isValid={true}
name="pf-login-password-id"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import TextInput from './TextInput';
import PropTypes from 'prop-types';

const ForwardRefWrapper = React.forwardRef((props, ref) => <TextInput innerRef={ref} {...props} />);
ForwardRefWrapper.displayName = 'TextInput';

export default ForwardRefWrapper;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface TextInputProps extends Omit<HTMLProps<HTMLInputElement>, 'onCha
isDisabled?: boolean;
onChange?(value: string, event: FormEvent<HTMLInputElement>): void;
isReadOnly?: boolean;
innerRef?: RefObject<HTMLInputElement>;
}

declare const TextInput: FunctionComponent<TextInputProps>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Simple from './examples/SimpleTextInput';
import Disabled from './examples/DisabledTextInput';
import ReadOnly from './examples/ReadOnlyTextInput';
import Invalid from './examples/InvalidTextInput';
import Focus from './examples/FocusTextInput';

export default {
title: 'TextInput',
Expand All @@ -14,6 +15,7 @@ export default {
{ component: Simple, title: 'Simple TextInput' },
{ component: Disabled, title: 'Disabled TextInput' },
{ component: ReadOnly, title: 'ReadOnly TextInput' },
{ component: Invalid, title: 'Invalid TextInput' }
{ component: Invalid, title: 'Invalid TextInput' },
{ component: Focus, title: 'Focus TextInput' }
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ class TextInput extends React.Component {
};

render() {
const { className, type, value, onChange, isValid, isReadOnly, isRequired, isDisabled, ...props } = this.props;
const {
className,
type,
value,
onChange,
isValid,
isReadOnly,
isRequired,
isDisabled,
innerRef,
...props
} = this.props;
return (
<input
{...props}
Expand All @@ -75,6 +86,7 @@ class TextInput extends React.Component {
required={isRequired}
disabled={isDisabled}
readOnly={isReadOnly}
ref={innerRef}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react';
import { TextInput, Button } from '@patternfly/react-core';

class FocusTextInput extends React.Component {
state = {
value: ''
};

inputRef = React.createRef();

componentDidUpdate() {
this.inputRef && this.inputRef.current.focus();
}

handleTextInputChange = value => {
this.setState({ value });
};

render() {
const { value } = this.state;
return (
<React.Fragment>
<TextInput
aria-label={'focus text input example'}
ref={this.inputRef}
value={value}
onChange={this.handleTextInputChange}
/>
<Button
onClick={() => {
this.inputRef && this.inputRef.current.focus();
}}
>
Focus
</Button>
</React.Fragment>
);
}
}

export default FocusTextInput;
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as TextInput, TextInputProps } from './TextInput';
export { TextInputProps } from './TextInput';
export { default as TextInput } from './ForwardRefTextInput';
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as TextInput } from './TextInput';
export { default as TextInput } from './ForwardRefTextInput';