-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,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
16
packages/core-react/src/components/TextArea/textArea.test.js
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,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); | ||
}); | ||
}); |
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,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> | ||
); | ||
} | ||
|
||
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
16
packages/core-react/src/components/TextField/textField.test.js
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,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); | ||
}); | ||
}); |
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,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 /> | ||
)); |
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,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" | ||
/> | ||
)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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