FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(TextInput): Convert text input to TypeScript by rebeccaalpert · Pull Request #1914 · patternfly/patternfly-react · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (3) .md  (1) .snap  (1) .ts  (6) .tsx  (2) All 5 file types selected
Deleted files Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • TextInput.d.ts
      • TextInput.js
      • TextInput.md
      • TextInput.test.js
      • TextInput.tsx
      • index.d.ts
      • index.js
      • index.ts
      • TableTextInput.test.js.snap
        • Demos.ts
            • TextInputDemo.tsx
          • index.ts

This file was deleted.

This file was deleted.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
title: 'Text input'
cssPrefix: 'pf-c-form-control'
propComponents: ['TextInput']
typescript: true
---

import { TextInput } from '@patternfly/react-core';
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Change the extension on this file to .tsx

import { shallow } from 'enzyme';
import TextInput from './TextInput';
import { TextInput } from './TextInput';

const props = {
onChange: jest.fn(),
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as React from 'react';
import styles from '@patternfly/patternfly/components/FormControl/form-control.css';
import { css } from '@patternfly/react-styles';
import { Omit } from '../../helpers/typeUtils';
import { FormEvent } from 'react';

export enum TextInputTypes {
text = 'text',
date = 'date',
datetimeLocal = 'datetime-local',
email = 'email',
month = 'month',
number = 'number',
password = 'password',
search = 'search',
tel = 'tel',
time = 'time',
url = 'url'
}

export interface TextInputProps extends Omit<React.HTMLProps<HTMLInputElement>, 'onChange' | 'disabled'> {
/** Additional classes added to the TextInput. */
className?: string;
/** Flag to show if the input is disabled. */
isDisabled?: boolean;
/** Flag to show if the input is read only. */
isReadOnly?: boolean;
/** Flag to show if the input is required. */
isRequired?: boolean;
/** Flag to show if the input is valid or invalid. */
isValid?: boolean;
/** A callback for when the input value changes. */
onChange?: (value: string, event: React.FormEvent<HTMLInputElement>) => void;
/** Type that the input accepts. */
type?: 'text' | 'date' | 'datetime-local' | 'email' | 'month' | 'number' | 'password' | 'search' | 'tel' | 'time' | 'url';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

To allow use of the enum, I believe you need to add | TextInputTypes

/** Value of the input. */
value?: string | number;
/** Aria-label. The input requires an associated id or aria-label. */
'aria-label'?: string;
Comment thread
rebeccaalpert marked this conversation as resolved.
}

export class TextInput extends React.Component<TextInputProps> {
static defaultProps = {
Comment thread
rebeccaalpert marked this conversation as resolved.
'aria-label': null as string,
className: '',
isRequired: false,
isValid: true,
isDisabled: false,
isReadOnly: false,
type: 'text'
Comment thread
rebeccaalpert marked this conversation as resolved.
Comment thread
rebeccaalpert marked this conversation as resolved.
}

constructor(props: TextInputProps) {
super(props);
if (!props.id && !props['aria-label'] && !props['aria-labelledby']) {
// tslint:disable-next-line:no-console
console.error('Text input:', 'Text input requires either an id or aria-label to be specified');
Comment thread
rebeccaalpert marked this conversation as resolved.
}
}

handleChange = (event: React.FormEvent<HTMLInputElement>) => {
this.props.onChange(event.currentTarget.value, event);
}

render() {
const { className, type, value, onChange, isValid, isReadOnly, isRequired, isDisabled, ...props } = this.props;
return (
<input
{...props}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's spread the props at the end to be consistent across components.

className={css(styles.formControl, className)}
onChange={this.handleChange}
type={type}
value={value}
aria-invalid={!isValid}
required={isRequired}
disabled={isDisabled}
readOnly={isReadOnly}
/>
);
}
}

This file was deleted.

This file was deleted.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './TextInput';
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ exports[`focused table text input 1`] = `
isRequired={false}
isValid={true}
onBlur={[Function]}
onChange={[Function]}
type="text"
/>
</Fragment>
Expand All @@ -30,7 +29,6 @@ exports[`simple table text input 1`] = `
isRequired={false}
isValid={true}
onBlur={[Function]}
onChange={[Function]}
type="text"
/>
</Fragment>
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe('Text Input Demo Test', () => {
it('Navigate to demo section', () => {
cy.visit('http://localhost:3000/');
cy.get('#text-input-demo-nav-item-link').click();
cy.url().should('eq', 'http://localhost:3000/text-input-demo-nav-link')
});

it('Verify text input', () => {
cy.get('#text').type('Hello world');
Comment thread
rebeccaalpert marked this conversation as resolved.
cy.get('#text').should('have.value', 'Hello world');
});

it('Verify disabled text input', () => {
cy.get('#text-disabled').should('be.disabled');
});

it('Verify read-only input', () => {
cy.get('#text-read-only').should('have.attr', 'readonly');
});

it('Verify invalid text input', () => {
cy.get('#text-invalid').should('have.attr', 'aria-invalid');
});
});
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ export const Demos: DemoInterface[] = [
name: 'Text Demo',
componentType: Examples.TextDemo
},
{
id: 'text-input-demo',
name: 'Text Input Demo',
componentType: Examples.TextInputDemo
},
{
id: 'title-demo',
name: 'Title Demo',
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TextInput, TextInputProps, TextInputTypes } from '@patternfly/react-core';
import React, { Component } from 'react';

export class TextInputDemo extends Component {
state = {
value: ''
};

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

myTextInputProps: TextInputProps = {
onChange: this.handleTextInputChange,
};

myDisabledTextInputProps: TextInputProps = {
isDisabled: true,
value: 'disabled text input example'
};

myReadOnlyTextInputProps: TextInputProps = {
isReadOnly: true,
value: 'read only text input example'
};

myValidTextInputProps: TextInputProps = {
isValid: false
};

render() {
return (
<React.Fragment>
<TextInput id="text" onChange={this.myTextInputProps.onChange} />
<TextInput id="text-disabled" isDisabled={this.myDisabledTextInputProps.isDisabled} value={this.myDisabledTextInputProps.value} />
<TextInput id="text-read-only" isReadOnly={this.myReadOnlyTextInputProps.isReadOnly} value={this.myReadOnlyTextInputProps.value} />
<TextInput id="text-invalid" isValid={this.myValidTextInputProps.isValid} />
</React.Fragment>
);
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './PopoverDemo/PopoverDemo';
export * from './RadioDemo/RadioDemo';
export * from './TabsDemo/TabsDemo';
export * from './TextDemo/TextDemo';
export * from './TextInputDemo/TextInputDemo';
export * from './TitleDemo/TitleDemo';
export * from './TooltipDemo/TooltipDemo';
export * from './WizardDemo/WizardDemo';

Back | FazBrowse Home | New Git URL