13.0.2 • Published 24 days ago

@leafygreen-ui/text-input v13.0.2

Weekly downloads
420
License
Apache-2.0
Repository
github
Last release
24 days ago

Text Input

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/text-input

NPM

npm install @leafygreen-ui/text-input

Peer Dependencies

PackageVersion
@leafygreen-ui/leafygreen-provider^1.1.0

Example

import TextInput from '@leafygreen-ui/text-input';

const [value, setValue] = useState('');

return (
  <TextInput
    label="Email"
    description="Enter your email below"
    placeholder="your.email@example.com"
    onChange={event => {
      /* Something to handle the change event */
    }}
    value={value}
  />
);

Properties

PropTypeDescriptionDefault
idstringid associated with the TextInput component.
labelstringText shown in bold above the input element.
descriptionstringText that gives more detail about the requirements for the input.
optionalbooleanMarks the input as optionalfalse
disabledbooleanDisabled the inputfalse
onChangefunctionThe event handler function for the 'onchange' event. Accepts the change event object as its argument and returns nothing.
onBlurfunctionThe event handler function for the 'onblur' event. Accepts the focus event object as its argument and returns nothing.
placeholderstringThe placeholder text shown in the input field before the user begins typing.
errorMessagestringText that gives more detail about the requirements for the input.'This input needs your attention'
successMessagestringText that indicates that the requirements for the input are met.'Success'
state'none', 'valid', 'error'Describes the state of the TextInput element before and after the input has been validated'none'
valuestringSets the HTML value attribute.''
classNamestringAdds a className to the class attribute.''
type'email', 'password', 'search', 'text', 'url', 'tel', 'number'Sets type for TextInput'text'
darkModebooleanDetermines whether or not the component will appear in dark mode.false
sizeVariant'xsmall', 'small', 'default', 'large',Sets the side padding, text size, and input height.default
baseFontSize14, 16Determines the base font-size of the component if the sizeVariant prop is set to default14
...native input attributesAny other props will be spread on the root input element

Special Case: Aria Labels

Either label or aria-labelledby must be provided a string, or there will be a console error. This is to ensure that screenreaders have a description for what the Text Input does.

Test Harnesses

getTestUtils()

getTestUtils() is a util that allows consumers to reliably interact with LG TextInput in a product test suite. If the TextInput component cannot be found, an error will be thrown.

Usage

import TextInput, { getTestUtils } from '@leafygreen-ui/text-input';

const utils = getTestUtils(lgId?: string); // lgId refers to the custom `data-lgid` attribute passed to `TextInput`. It defaults to 'lg-text_input' if left empty.

Single TextInput

import { render } from '@testing-library/react';
import TextInput, { getTestUtils } from '@leafygreen-ui/text-input';

...

test('text-input', () => {
  render(<TextInput label="label" value="text input" />);
  const { getInput, getInputValue } = getTestUtils();

  expect(getInput()).toBeInTheDocument();
  expect(getInputValue()).toBe('text input');
});

Multiple TextInput's

When testing multiple TextInput's it is recommended to add the custom data-lgid attribute to each TextInput.

import { render } from '@testing-library/react';
import TextInput, { getTestUtils } from '@leafygreen-ui/text-input';

...

test('text-input', () => {
  render(
    <>
      <TextInput data-lgid="text-input-1" label="label 1" />
      <TextInput data-lgid="text-input-2" label="label 2" value="text input" />
    </>,
  );
  const utilsOne = getTestUtils('text-input-1'); // data-lgid
  const utilsTwo = getTestUtils('text-input-2'); // data-lgid

  // First TextInput
  expect(utilsOne.getInput()).toBeInTheDocument();
  expect(utilsOne.getInputValue()).toBe('');

  // Second TextInput
  expect(utilsTwo.getInput()).toBeInTheDocument();
  expect(utilsTwo.getInputValue()).toBe('text input');
});

TextInput with other LG form elements

import { render } from '@testing-library/react';
import Toggle, { getTestUtils as getToggleTestUtils } from '@leafygreen-ui/toggle';
import TextInput, { getTestUtils as getTextInputTestUtils } from '@leafygreen-ui/text-input';
import TextArea, { getTestUtils as getTextAreaTestUtils } from '@leafygreen-ui/text-area';

...

test('Form', () => {
  render(
    <Form>
      <Toggle aria-label="Toggle label" />
      <TextInput label="TextInput label" />
      <TextArea label="TextArea label" />
    </Form>,
  );
  const toggleInputUtils = getToggleTestUtils();
  const textInputUtils = getTextInputTestUtils();
  const textAreaUtils = getTextAreaTestUtils();

  // LG Toggle
  expect(toggleInputUtils.getInput()).toBeInTheDocument();
  expect(toggleInputUtils.getInputValue()).toBe('false');

  // LG TextInput
  expect(textInputUtils.getInput()).toBeInTheDocument();
  expect(textInputUtils.getInputValue()).toBe('');

  // LG TextArea
  expect(textAreaUtils.getInput()).toBeInTheDocument();
  expect(textAreaUtils.getInputValue()).toBe('');
});

Test Utils

const {
  getInput,
  getLabel,
  getDescription,
  getErrorMessage,
  getInputValue,
  isDisabled,
  isValid,
  isError,
  isOptional,
} = getTestUtils();
UtilDescriptionReturns
getInputReturns the input nodeHTMLButtonElement
getLabelReturns the label nodeHTMLButtonElement | null
getDescriptionReturns the description nodeHTMLButtonElement | null
getErrorMessageReturns the error message nodeHTMLButtonElement | null
getInputValueReturns the input valuestring
isDisabledReturns whether the input is disabledboolean
isValidReturns whether the input state is validboolean
isErrorReturns whether the input state is errorboolean
isOptionalReturns whether the input is optionalboolean
13.0.2

24 days ago

13.0.1

1 month ago

13.0.0

1 month ago

12.1.27

1 month ago

12.1.26

2 months ago

12.1.25

4 months ago

12.1.24

7 months ago

12.1.21

8 months ago

12.1.20

9 months ago

12.1.23

7 months ago

12.1.22

8 months ago

12.1.18

9 months ago

12.1.17

9 months ago

12.1.19

9 months ago

12.1.16

10 months ago

12.1.15

10 months ago

13.0.0-alpha.1

10 months ago

13.0.0-alpha.0

10 months ago

12.1.13-next.0

12 months ago

12.1.13-next.1

12 months ago

12.1.13-next.6

12 months ago

12.1.13-next.7

12 months ago

12.1.13-next.4

12 months ago

12.1.13-next.5

12 months ago

12.1.13-next.2

12 months ago

12.1.13-next.3

12 months ago

12.1.14

11 months ago

12.1.13

11 months ago

12.1.12

12 months ago

12.1.11

12 months ago

12.1.10

1 year ago

12.1.7

1 year ago

12.1.8

1 year ago

12.1.9

1 year ago

12.1.6

1 year ago

12.1.5

1 year ago

12.0.0

1 year ago

12.1.2

1 year ago

12.1.3

1 year ago

12.1.4

1 year ago

12.1.0

1 year ago

12.1.1

1 year ago

10.2.0

2 years ago

10.2.1

2 years ago

11.0.0

2 years ago

11.0.1

2 years ago

10.2.0-test.0

2 years ago

10.2.0-next.2

2 years ago

10.2.0-next.1

2 years ago

10.2.0-next.0

2 years ago

10.1.1

2 years ago

10.1.2

2 years ago

10.1.3

2 years ago

10.0.0

2 years ago

10.1.0

2 years ago

7.0.1

2 years ago

8.0.0

2 years ago

9.0.0

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

7.0.0

2 years ago

6.1.0

2 years ago

6.0.5

3 years ago

6.0.4

3 years ago

6.0.3

3 years ago

5.0.12

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

6.0.2

3 years ago

5.0.11

3 years ago

5.0.10

3 years ago

5.0.9

3 years ago

5.0.8

3 years ago

5.0.7

3 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago