Skip to content
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

Add support for other "text-like" input types to TextInput #189

Merged
merged 1 commit into from
Sep 9, 2021
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
10 changes: 5 additions & 5 deletions src/components/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import classnames from 'classnames';
* the rendered `input` element.
* @prop {boolean} [hasError] - There is an error associated with this input. Will
* set some error styling.
* @prop {'email'|'search'|'text'|'url'} [type="text"] - Set the <input> type:
Copy link
Contributor

Choose a reason for hiding this comment

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

We could add 'password' too (while not having a specific component for that).

Something to notice about the 'search' is that in Chrome it adds a 'X' cancel symbol. I am not sure if we mind that:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/search#differences_between_search_and_text_types

* restricted to the "text-like" type values.
*/

/**
* `TextInput` sets the `type` attribute on the `input`, but any other valid
* `input` attribute should be forwarded.
* @typedef {Omit<import('preact').JSX.HTMLAttributes<HTMLInputElement>, 'type'>} HTMLInputElementProps
* @typedef {TextInputBaseProps & HTMLInputElementProps} TextInputProps
* @typedef {TextInputBaseProps & import('preact').JSX.HTMLAttributes<HTMLInputElement>} TextInputProps
*/

/**
Expand All @@ -33,6 +32,7 @@ export function TextInput({
classes = '',
inputRef,
hasError = false,
type = 'text',
...restProps
}) {
return (
Expand All @@ -44,7 +44,7 @@ export function TextInput({
)}
{...restProps}
ref={inputRef}
type="text"
type={type}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/test/TextInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ describe('TextInput', () => {
assert.isFalse(wrapper.find('input').hasClass('has-error'));
});

it('ignores `type` property and sets `type` to `text`', () => {
const wrapper = createComponent({ type: 'checkbox' });
it('sets input type based on `type` prop', () => {
const wrapper = createComponent({ type: 'url' });

assert.equal(wrapper.getDOMNode().getAttribute('type'), 'text');
assert.equal(wrapper.getDOMNode().getAttribute('type'), 'url');
});

it('applies an error class when in error', () => {
Expand Down
12 changes: 12 additions & 0 deletions src/pattern-library/components/patterns/FormComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ export default function FormComponents() {
</Library.Demo>
</Library.Example>

<Library.Example title="As type='url'">
<p>
<code>TextInput</code> renders an <code>input</code> field of{' '}
<code>type=&quot;text&quot;</code> by default, but text-like `type`
values are also supported (<code>email</code>, <code>search</code>,{' '}
<code>url</code>).
</p>
<Library.Demo withSource>
<TextInput name="my-input" type="url" />
</Library.Demo>
</Library.Example>

<Library.Example title="Error state">
<Library.Demo withSource>
<TextInput name="my-input" hasError />
Expand Down