-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
472 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
"prepub", | ||
"renderprops", | ||
"rerender", | ||
"scrollbars", | ||
"secondaryfill", | ||
"shorthash", | ||
"tabindex", | ||
|
This file contains 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
This file contains 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,101 @@ | ||
import { Description, DoDont, Meta, Props } from 'docsComponents'; | ||
|
||
<Meta title="TextArea" /> | ||
|
||
# TextArea | ||
|
||
<Description of="TextArea" /> | ||
|
||
## Props | ||
|
||
<Props of="TextArea" /> | ||
|
||
## Examples | ||
|
||
### Basic | ||
|
||
A basic TextArea. | ||
|
||
```js live | ||
<TextArea placeholder="Type here..."> | ||
Children are placed under the textarea | ||
</TextArea> | ||
``` | ||
|
||
### Autosize | ||
|
||
Textarea autosize can be on or off. | ||
|
||
```js live | ||
<Row gutter={1}> | ||
<Col> | ||
<TextArea placeholder="I will grow in height as my input grows" /> | ||
</Col> | ||
<Col> | ||
<TextArea autoSize={false} placeholder="I will not grow in height" /> | ||
</Col> | ||
</Row> | ||
``` | ||
|
||
### Disabled | ||
|
||
TextArea can be disabled. | ||
|
||
```js live | ||
<Row gutter={1}> | ||
<Col> | ||
<TextArea disabled value="Disabled" /> | ||
</Col> | ||
<Col> | ||
<TextArea readOnly value="Read Only" /> | ||
</Col> | ||
</Row> | ||
``` | ||
|
||
### Size | ||
|
||
TextArea can have different sizes. | ||
|
||
```js live | ||
<Row gutter={1}> | ||
<Col span={8}> | ||
<TextArea size="sm" placeholder="sm" /> | ||
</Col> | ||
<Col span={8}> | ||
<TextArea size="md" placeholder="md" /> | ||
</Col> | ||
<Col span={8}> | ||
<TextArea size="lg" placeholder="lg" /> | ||
</Col> | ||
</Row> | ||
``` | ||
|
||
### Error | ||
|
||
A textarea can indicate it has an error. | ||
|
||
```js live | ||
<Row gutter={1}> | ||
<Col span={12}> | ||
<TextArea hasError placeholder="Error..." /> | ||
</Col> | ||
<Col span={12}> | ||
<TextArea hasError placeholder="Error..."> | ||
Text indicating error | ||
</TextArea> | ||
</Col> | ||
</Row> | ||
``` | ||
|
||
## Design Guidelines | ||
|
||
<DoDont | ||
do={[ | ||
'Use TextArea to get multiline data input from a user.', | ||
'Make use of a label when possible otherwise set aria-label.' | ||
]} | ||
dont={[ | ||
'Use TextArea in an inaccessible way for screen readers.', | ||
'Use TextArea when only a single line of data is required.' | ||
]} | ||
/> |
This file contains 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
This file contains 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
This file contains 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,105 @@ | ||
import { render } from '@testing-library/react'; | ||
import React, { createRef } from 'react'; | ||
import { TextArea } from './TextArea'; | ||
|
||
describe('Textarea', () => { | ||
it('renders a textarea element and sets child text', () => { | ||
render(<TextArea>Hello world!</TextArea>); | ||
const container = document.querySelector('.mvn--text-area'); | ||
const textarea = document.querySelector('textarea'); | ||
expect(container).toBeInTheDocument(); | ||
expect(textarea).toBeInTheDocument(); | ||
expect(container).toHaveTextContent('Hello world!'); | ||
}); | ||
|
||
it('sets className', () => { | ||
render(<TextArea className="textarea-class">Hello world!</TextArea>); | ||
const element = document.querySelector('.mvn--text-area'); | ||
expect(element).toHaveClass('textarea-class'); | ||
}); | ||
|
||
it('passes props', () => { | ||
render( | ||
<TextArea id="TextareaId" rows={4} data-test="textarea-data"> | ||
Hello world! | ||
</TextArea> | ||
); | ||
const element = document.querySelector('textarea'); | ||
expect(element).toHaveAttribute('id', 'TextareaId'); | ||
expect(element).toHaveAttribute('data-test', 'textarea-data'); | ||
expect(element).toHaveAttribute('rows', '4'); | ||
}); | ||
|
||
describe('autoSize', () => { | ||
it('is autosize by default', () => { | ||
render(<TextArea />); | ||
const textarea = document.querySelector('textarea'); | ||
expect(textarea).not.toHaveClass('no-autosize'); | ||
}); | ||
|
||
it('unset autosize', () => { | ||
render(<TextArea autoSize={false} />); | ||
const textarea = document.querySelector('textarea'); | ||
expect(textarea).toHaveClass('no-autosize'); | ||
}); | ||
}); | ||
|
||
describe('disabled', () => { | ||
it('is not disabled by default', () => { | ||
render(<TextArea />); | ||
const textarea = document.querySelector('textarea'); | ||
expect(textarea).not.toHaveAttribute('disabled'); | ||
}); | ||
|
||
it('sets disabled', () => { | ||
render(<TextArea disabled />); | ||
const textarea = document.querySelector('textarea'); | ||
expect(textarea).toHaveAttribute('disabled'); | ||
}); | ||
}); | ||
|
||
describe('hasError', () => { | ||
it('has no error styling by default', () => { | ||
render(<TextArea />); | ||
const container = document.querySelector('.mvn--text-area'); | ||
expect(container).not.toHaveClass('has-error'); | ||
}); | ||
|
||
it('sets error styling', () => { | ||
render(<TextArea hasError>Error Text</TextArea>); | ||
const container = document.querySelector('.mvn--text-area'); | ||
expect(container).toHaveClass('has-error'); | ||
}); | ||
}); | ||
|
||
describe('size', () => { | ||
it('is md by default', () => { | ||
render(<TextArea />); | ||
const container = document.querySelector('.mvn--text-area'); | ||
expect(container).toHaveClass('md'); | ||
expect(container).not.toHaveClass('sm'); | ||
expect(container).not.toHaveClass('lg'); | ||
}); | ||
|
||
it('sets sm', () => { | ||
render(<TextArea size="sm" />); | ||
const container = document.querySelector('.mvn--text-area'); | ||
expect(container).toHaveClass('sm'); | ||
}); | ||
|
||
it('sets lg', () => { | ||
render(<TextArea size="lg" />); | ||
const container = document.querySelector('.mvn--text-area'); | ||
expect(container).toHaveClass('lg'); | ||
}); | ||
}); | ||
|
||
describe('forwarding ref', () => { | ||
it('sets ref', () => { | ||
const ref = createRef<HTMLTextAreaElement>(); | ||
render(<TextArea ref={ref} />); | ||
const element = document.querySelector('textarea'); | ||
expect(ref.current).toBe(element); | ||
}); | ||
}); | ||
}); |
This file contains 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,101 @@ | ||
import clsx from 'clsx'; | ||
import React, { forwardRef, ReactNode, TextareaHTMLAttributes } from 'react'; | ||
import TextAreaAutosize from 'react-autosize-textarea'; | ||
import { useId } from '../../../hooks'; | ||
import { Block } from '../../Block'; | ||
import { OptionalField } from '../Form'; | ||
import classes from './text-area.module.scss'; | ||
|
||
/** | ||
* TextArea allows for multiline text input. | ||
*/ | ||
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>( | ||
( | ||
{ | ||
autoSize = true, | ||
children, | ||
className, | ||
disabled = false, | ||
hasError = false, | ||
label, | ||
maxRows, | ||
size = 'md', | ||
style, | ||
rows = 3, | ||
...props | ||
}, | ||
ref | ||
) => { | ||
const id = useId(props); | ||
const labelId = useId(); | ||
|
||
return ( | ||
<OptionalField | ||
hasError={hasError} | ||
htmlFor={id} | ||
label={label} | ||
labelId={labelId} | ||
size={size} | ||
> | ||
<div | ||
className={clsx( | ||
'mvn--text-area', | ||
classes.container, | ||
classes[size], | ||
{ [classes['has-error']]: hasError }, | ||
className | ||
)} | ||
style={style} | ||
> | ||
<label className={classes.label}> | ||
{autoSize ? ( | ||
<TextAreaAutosize | ||
{...props} | ||
className={classes.textarea} | ||
disabled={disabled} | ||
id={id} | ||
maxRows={maxRows} | ||
ref={ref} | ||
rows={rows} | ||
/> | ||
) : ( | ||
<textarea | ||
{...props} | ||
className={clsx(classes.textarea, classes['no-autosize'])} | ||
disabled={disabled} | ||
id={id} | ||
ref={ref} | ||
rows={rows} | ||
/> | ||
)} | ||
</label> | ||
{children ? <Block className={classes.text}>{children}</Block> : null} | ||
</div> | ||
</OptionalField> | ||
); | ||
} | ||
); | ||
|
||
export interface TextAreaProps | ||
extends TextareaHTMLAttributes<HTMLTextAreaElement> { | ||
/** Wether textarea grows in size when more lines are added */ | ||
autoSize?: boolean; | ||
|
||
/** Wether textarea is disabled */ | ||
disabled?: boolean; | ||
|
||
/** Wether textarea contains an error */ | ||
hasError?: boolean; | ||
|
||
/** Label text */ | ||
label?: ReactNode; | ||
|
||
/** Maximum number of rows */ | ||
maxRows?: number; | ||
|
||
/** (min) number of rows */ | ||
rows?: number; | ||
|
||
/** TextArea size */ | ||
size?: 'sm' | 'md' | 'lg'; | ||
} |
This file contains 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,2 @@ | ||
export { TextArea } from './TextArea'; | ||
export type { TextAreaProps } from './TextArea'; |
Oops, something went wrong.