Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Added TextareaField
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 22, 2018
1 parent f4d5993 commit bbb263e
Show file tree
Hide file tree
Showing 6 changed files with 2,831 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Textarea/Textarea.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ menu: Form
import { Playground, PropsTable } from 'docz';
import { Box } from '../primitives/index';
import Textarea from './index';
import TextareaField from './TextareaField';
import Component from '@reactions/component';

# Textarea
Expand Down Expand Up @@ -93,12 +94,40 @@ Using an accessibility label (`a11yLabel`), will enable screen readers to read t

<PropsTable of={Textarea} />

# TextareaField

## Import

`import { TextareaField } from 'fannypack'`

## Basic Usage

The `<TextareaField>` component contains of the [`<Textarea>` component](#input) wrapped by the [`<FieldWrapper>` component](/form/fieldwrapper).

It accepts a combination of [`<Textarea>` props](/form/textarea#textarea-props) and [`<FieldWrapper>` props](/form/fieldwrapper#props).

<Playground>
<TextareaField a11yId="description" label="Description" />
</Playground>

## `<TextareaField>` Props

<PropsTable of={TextareaField} />

## Theming

### Schema

```jsx
{
base: string | Object
base: string | Object,
disabled: string | Object,
focus: string | Object,
placeholder: string | Object,
sizes: {
small: string | Object,
medium: string | Object,
large: string | Object
}
}
```
126 changes: 126 additions & 0 deletions src/Textarea/TextareaField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import React, { type Node } from 'react';

import type { Size } from '../types';
import Textarea from './Textarea';
import FieldWrapper from '../FieldWrapper';

export type Props = {
/** An accessible identifier for the textarea */
a11yId?: string,
/** An accessible label for the textarea */
a11yLabel?: string,
as?: any,
autoComplete?: string,
/** Automatically focus on the textarea */
autoFocus?: boolean,
children: Node,
className?: string,
/** Default value of the textarea */
defaultValue?: string,
description?: string | Element<any>,
/** Disables the textrea */
disabled?: boolean,
hint?: string | Element<any>,
/** Makes the textarea span full width */
isFullWidth?: boolean,
/** Adds a cute loading indicator to the textarea */
isLoading?: boolean,
isOptional?: boolean,
/** Makes the texrea required and sets aria-invalid to true */
isRequired?: boolean,
label?: string | Element<any>,
/** Name of the textarea */
name?: string,
/** Alters the size of the textarea. Can be "small", "medium" or "large" */
size?: Size,
/** If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the maximum number of characters (in UTF-16 code units) that the user can enter. For other control types, it is ignored. */
maxLength?: number,
/** If the value of the type attribute is text, email, search, password, tel, or url, this attribute specifies the minimum number of characters (in UTF-16 code points) that the user can enter. For other control types, it is ignored. */
minLength?: number,
/** Regex pattern to apply to the textarea */
pattern?: string,
/** Hint text to display */
placeholder?: string,
/** This prop prevents the user from modifying the value of the textarea. It is ignored if the value of the type attribute is hidden, range, color, checkbox, radio, file, or a button type (such as button or submit). */
readOnly?: boolean,
/** The minimum number of rows in the text area. */
rows?: number,
/** Setting the value of this attribute to true indicates that the element needs to have its spelling and grammar checked. The value default indicates that the element is to act according to a default behavior, possibly based on the parent element's own spellcheck value. The value false indicates that the element should not be checked. */
spellCheck?: boolean,
/** State of the input. Can be any color in the palette. */
state?: string,
validationText?: string,
/** Value of the input */
value?: string,
/** Function to invoke when focus is lost */
onBlur?: Function,
/** Function to invoke when input has changed */
onChange?: Function,
/** Function to invoke when input is focused */
onFocus?: Function
};

const TextareaField = ({
a11yId,
a11yLabel,
description,
hint,
isFullWidth,
isLoading,
isOptional,
isRequired,
label,
size,
state,
validationText,
...props
}: Props) => (
<FieldWrapper
a11yId={a11yId}
description={description}
hint={hint}
isFullWidth={isFullWidth}
isOptional={isOptional}
isRequired={isRequired}
label={label}
state={state}
validationText={validationText}
>
{({ elementProps }) => <Textarea {...props} {...elementProps} />}
</FieldWrapper>
);

TextareaField.defaultProps = {
a11yId: undefined,
a11yLabel: undefined,
as: undefined,
autoComplete: undefined,
autoFocus: false,
className: undefined,
defaultValue: undefined,
description: undefined,
disabled: false,
hint: undefined,
isFullWidth: false,
isLoading: false,
isOptional: false,
isRequired: false,
label: undefined,
maxLength: undefined,
minLength: undefined,
name: undefined,
onBlur: undefined,
onChange: undefined,
onFocus: undefined,
pattern: undefined,
placeholder: undefined,
readOnly: undefined,
rows: '3',
spellCheck: undefined,
size: 'default',
state: undefined,
validationText: undefined,
value: undefined
};

export default TextareaField;
69 changes: 69 additions & 0 deletions src/Textarea/__tests__/TextareaField.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from 'react';
import render from '../../_utils/tests/render';
import TextareaField from '../TextareaField';
import 'jest-styled-components';

it('renders correctly for a basic input field', () => {
const { container } = render(<TextareaField />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for an input field with a placeholder', () => {
const { container } = render(<TextareaField placeholder="Awesome placeholder" />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for a disabled input field', () => {
const { container } = render(<TextareaField disabled />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for an input field with a default value', () => {
const { container } = render(<TextareaField defaultValue="Awesome value" />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for a full width input field', () => {
const { container } = render(<TextareaField isFullWidth />);
expect(container.firstChild).toMatchSnapshot();
});

describe('sizes', () => {
['small', 'medium', 'large'].forEach(size => {
it(`renders correctly for an input field with size ${size}`, () => {
const { container } = render(<TextareaField size={size} />);
expect(container.firstChild).toMatchSnapshot();
});
});
});

describe('states', () => {
['danger', 'success', 'warning', 'primary'].forEach(state => {
it(`renders correctly for an input field with state ${state}`, () => {
const { container } = render(<TextareaField state={state} />);
expect(container.firstChild).toMatchSnapshot();
});
});
});

it('renders correctly for an input field with a description', () => {
const { container } = render(
<TextareaField a11yId="username" label="Username" description="Required for your fannypack" />
);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for an input field with a hint', () => {
const { container } = render(<TextareaField a11yId="username" label="Username" hint="Must be awesome" />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for an optional input field', () => {
const { container } = render(<TextareaField a11yId="username" label="Username" isOptional />);
expect(container.firstChild).toMatchSnapshot();
});

it('renders correctly for a required field', () => {
const { container } = render(<TextareaField a11yId="username" label="Username" isRequired />);
expect(container.firstChild).toMatchSnapshot();
});
Loading

0 comments on commit bbb263e

Please sign in to comment.