Skip to content

feat(core-react): add text field #179

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

Merged
merged 3 commits into from
Aug 15, 2019
Merged
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
65 changes: 65 additions & 0 deletions packages/core-react/src/components/TextArea/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';

export default function TextArea({
id,
type,
placeholder,
disabled,
required,
value,
error,
compact,
label,
className,
...rest
}) {
const wrapperClass = clsx(
'ray-text-area',
{
'ray-text-area--has-value': value,
'ray-text-area--error': error,
'ray-text-area--disabled': disabled,
'ray-text-area--required': required,
'ray-text-area--compact': compact
},
className
);

return (
<div className={wrapperClass}>
<textarea
className="ray-text-area__input"
id={id}
type={type}
value={value}
placeholder={placeholder}
disabled={disabled}
{...rest}
/>
{label && (
<label className="ray-text-area__label" htmlFor={id}>
{label}
</label>
)}
</div>
);
}

TextArea.propTypes = {
id: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
placeholder: PropTypes.string,
disabled: PropTypes.bool,
required: PropTypes.bool,
compact: PropTypes.bool,
value: PropTypes.string,
error: PropTypes.string,
label: PropTypes.string,
className: PropTypes.string
};

TextArea.defaultProps = {
type: 'text'
};
16 changes: 16 additions & 0 deletions packages/core-react/src/components/TextArea/textArea.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { mount } from 'enzyme';

import TextArea from '.';

describe('TextArea', () => {
test('it renders a text field', () => {
const wrapper = mount(<TextArea id="email" />);
expect(wrapper.find('textarea').length).toBe(1);
});

test('it renders a text area with a label', () => {
const wrapper = mount(<TextArea id="email" label="email address" />);
expect(wrapper.find('label.ray-text-area__label').length).toBe(1);
});
});
65 changes: 65 additions & 0 deletions packages/core-react/src/components/TextField/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';

export default function TextField({
id,
type,
placeholder,
disabled,
required,
value,
error,
compact,
label,
className,
...rest
}) {
const wrapperClass = clsx(
'ray-text-field',
{
'ray-text-field--has-value': value,
'ray-text-field--error': error,
'ray-text-field--disabled': disabled,
'ray-text-field--required': required,
'ray-text-field--compact': compact
},
className
);

return (
<div className={wrapperClass}>
<input
className="ray-text-field__input"
id={id}
type={type}
value={value}
placeholder={placeholder}
disabled={disabled}
{...rest}
/>
{label && (
<label className="ray-text-field__label" htmlFor={id}>
{label}
</label>
)}
</div>
Copy link

Choose a reason for hiding this comment

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

should we also include the error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i was considering it... this pr is still wip

);
}

TextField.propTypes = {
id: PropTypes.string.isRequired,
type: PropTypes.string.isRequired,
placeholder: PropTypes.string,
disabled: PropTypes.bool,
required: PropTypes.bool,
compact: PropTypes.bool,
value: PropTypes.string,
error: PropTypes.string,
label: PropTypes.string,
className: PropTypes.string
};

TextField.defaultProps = {
type: 'text'
};
16 changes: 16 additions & 0 deletions packages/core-react/src/components/TextField/textField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { mount } from 'enzyme';

import TextField from '.';

describe('TextField', () => {
test('it renders a text field', () => {
const wrapper = mount(<TextField id="email" />);
expect(wrapper.find('input[type="text"]').length).toBe(1);
});

test('it renders a text field with a label', () => {
const wrapper = mount(<TextField id="email" label="email address" />);
expect(wrapper.find('label').length).toBe(1);
});
});
28 changes: 28 additions & 0 deletions packages/core-react/stories/TextField.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import TextField from '../src/components/TextField';

storiesOf('TextField', module)
.add('default', () => (
<TextField id="example" value={undefined} placeholder="hello" />
))
.add('required', () => (
<TextField id="example" value={undefined} placeholder="hello" required />
))
.add('error', () => (
<TextField
id="example"
value={undefined}
placeholder="hello"
error="something went wrong"
/>
))
.add('with value', () => (
<TextField id="example" value="worlds" placeholder="hello" />
))
.add('disabled', () => (
<TextField id="example" value="worlds" placeholder="hello" disabled />
))
.add('compact', () => (
<TextField id="example" value="worlds" placeholder="hello" compact />
));
56 changes: 56 additions & 0 deletions packages/core-react/stories/textArea.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import TextArea from '../src/components/TextArea';

storiesOf('TextArea', module)
.add('default', () => (
<TextArea
id="example"
value={undefined}
placeholder="hello"
label="im a label"
/>
))
.add('required', () => (
<TextArea
id="example"
value={undefined}
placeholder="hello"
required
label="im a label"
/>
))
.add('error', () => (
<TextArea
id="example"
value={undefined}
placeholder="hello"
error="something went wrong"
/>
))
.add('with value', () => (
<TextArea
id="example"
value="worlds"
placeholder="hello"
label="im a label"
/>
))
.add('disabled', () => (
<TextArea
id="example"
value="worlds"
placeholder="hello"
disabled
label="im a label"
/>
))
.add('compact', () => (
<TextArea
id="example"
value="worlds"
placeholder="hello"
compact
label="im a label"
/>
));