Skip to content

Commit

Permalink
refactor(text-input): migrate to TypeScript (#1868)
Browse files Browse the repository at this point in the history
  • Loading branch information
adnasa authored Apr 27, 2021
1 parent 4acb66d commit 7d56aa9
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 95 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-tips-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@commercetools-uikit/text-input': patch
---

Migrate `<TextInput />` to TypeScript
34 changes: 17 additions & 17 deletions packages/components/inputs/text-input/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ export default Example;

## Properties

| Props | Type | Required | Default | Description |
| ---------------------- | -------------------------------------------------------------------------------------------------- | :------: | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | | | Used as HTML id property. An id is auto-generated when it is not specified. |
| `autoComplete` | `string` | | | Used as HTML autocomplete property |
| `className` | `string` | | | |
| `name` | `string` | | | Used as HTML name of the input component. property |
| `value` | `string` || | Value of the input component. |
| `onChange` | `custom` | | | Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value.&#xA;<br />&#xA;Signature: `(event) => void` |
| `onBlur` | `func` | | | Called when input is blurred&#xA;Signature: `(event) => void` |
| `onFocus` | `func` | | | Called when input is focused&#xA;Signature: `(event) => void` |
| `isAutofocussed` | `bool` | | | Focus the input on initial render |
| `isDisabled` | `bool` | | | Indicates that the input cannot be modified (e.g not authorized, or changes currently saving). |
| `isReadOnly` | `bool` | | | Indicates that the field is displaying read-only content |
| `hasError` | `bool` | | | Indicates if the input has invalid values |
| `hasWarning` | `bool` | | | |
| `placeholder` | `string` | | | Placeholder text for the input |
| `horizontalConstraint` | `enum`<br/>Possible values:<br/>`3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'` | | `'scale'` | Horizontal size limit of the input fields. |
| Props | Type | Required | Default | Description |
| ---------------------- | ----------------------------------------------------------------------------------------------------- | :------: | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id` | `string` | | | Used as HTML id property. An id is auto-generated when it is not specified. |
| `autoComplete` | `string` | | | Used as HTML autocomplete property |
| `className` | `string` | | | `className` forwarded to the underlying `<input />`. |
| `name` | `string` | | | Used as HTML name of the input component. property |
| `value` | `string` || | Value of the input component. |
| `onChange` | `ChangeEventHandler` | | | Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value.&#xA;<br />&#xA;Signature: `(event) => void` |
| `onBlur` | `FocusEventHandler` | | | Called when input is blurred&#xA;Signature: `(event) => void` |
| `onFocus` | `FocusEventHandler` | | | Called when input is focused&#xA;Signature: `(event) => void` |
| `isAutofocussed` | `boolean` | | | Focus the input on initial render |
| `isDisabled` | `boolean` | | | Indicates that the input cannot be modified (e.g not authorized, or changes currently saving). |
| `isReadOnly` | `boolean` | | | Indicates that the field is displaying read-only content |
| `hasError` | `boolean` | | | Indicates if the input has invalid values |
| `hasWarning` | `boolean` | | | |
| `placeholder` | `string` | | | Placeholder text for the input |
| `horizontalConstraint` | `union`<br/>Possible values:<br/>`, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 'scale', 'auto'` | | `'scale'` | Horizontal size limit of the input fields. |

## `data-*` props

Expand Down
3 changes: 1 addition & 2 deletions packages/components/inputs/text-input/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"@commercetools-uikit/utils": "12.0.0",
"@emotion/react": "^11.1.1",
"@emotion/styled": "^11.0.0",
"prop-types": "15.7.2",
"react-required-if": "1.0.3"
"prop-types": "15.7.2"
},
"devDependencies": {
"react": "17.0.1"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,127 +1,131 @@
import React from 'react';
import PropTypes from 'prop-types';
import requiredIf from 'react-required-if';
import React, { FocusEventHandler, ChangeEventHandler } from 'react';
import { useTheme } from '@emotion/react';
import { filterDataAttributes } from '@commercetools-uikit/utils';
import { filterDataAttributes, warning } from '@commercetools-uikit/utils';
import Constraints from '@commercetools-uikit/constraints';
import { getInputStyles } from '@commercetools-uikit/input-utils';

const TextInput = (props) => {
const theme = useTheme();
return (
<Constraints.Horizontal max={props.horizontalConstraint}>
<input
id={props.id}
name={props.name}
type="text"
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur}
onFocus={props.onFocus}
disabled={props.isDisabled}
placeholder={props.placeholder}
readOnly={props.isReadOnly}
autoFocus={props.isAutofocussed}
autoComplete={props.autoComplete}
css={getInputStyles(props, theme)}
// Allow to override the styles by passing a `className` prop.
// Custom styles can also be passed using the `css` prop from emotion.
// https://emotion.sh/docs/css-prop#style-precedence
className={props.className}
{...filterDataAttributes(props)}
/* ARIA */
aria-readonly={props.isReadOnly}
role="textbox"
contentEditable={!props.isReadOnly}
/>
</Constraints.Horizontal>
);
};

TextInput.displayName = 'TextInput';

TextInput.propTypes = {
type TTextInputProps = {
/**
* Used as HTML id property. An id is auto-generated when it is not specified.
*/
id: PropTypes.string,
id?: string;
/**
* Used as HTML autocomplete property
*/
autoComplete: PropTypes.string,
className: PropTypes.string,
autoComplete?: string;
/**
* `className` forwarded to the underlying `<input />`.
*/
className?: string;
/**
* Used as HTML name of the input component. property
*/
name: PropTypes.string,
name?: string;
/**
* Value of the input component.
*/
value: PropTypes.string.isRequired,
value: string;
/**
* Called with an event containing the new value. Required when input is not read only. Parent should pass it back as value.
* <br />
* Signature: `(event) => void`
*/
onChange: requiredIf(PropTypes.func, (props) => !props.isReadOnly),
onChange: ChangeEventHandler;
/**
* Called when input is blurred
* Signature: `(event) => void`
*/
onBlur: PropTypes.func,
onBlur?: FocusEventHandler;
/**
* Called when input is focused
* Signature: `(event) => void`
*/
onFocus: PropTypes.func,
onFocus?: FocusEventHandler;
/**
* Focus the input on initial render
*/
isAutofocussed: PropTypes.bool,
isAutofocussed?: boolean;
/**
* Indicates that the input cannot be modified (e.g not authorized, or changes currently saving).
*/
isDisabled: PropTypes.bool,
isDisabled?: boolean;
/**
* Indicates that the field is displaying read-only content
*/
isReadOnly: PropTypes.bool,
isReadOnly?: boolean;
/**
* Indicates if the input has invalid values
*/
hasError: PropTypes.bool,
hasWarning: PropTypes.bool,
hasError?: boolean;
hasWarning?: boolean;
/**
* Placeholder text for the input
*/
placeholder: PropTypes.string,
placeholder?: string;
/**
* Horizontal size limit of the input fields.
*/
horizontalConstraint: PropTypes.oneOf([
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
'scale',
'auto',
]),
horizontalConstraint:
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
| 16
| 'scale'
| 'auto';
};

TextInput.defaultProps = {
const defaultProps: Pick<TTextInputProps, 'horizontalConstraint'> = {
horizontalConstraint: 'scale',
};

TextInput.isEmpty = (value) => !value || value.trim().length === 0;
const TextInput = (props: TTextInputProps) => {
const theme = useTheme();
if (!props.isReadOnly) {
warning(
Boolean(props.onChange),
'TextInput: `onChange` is required when is not read only.'
);
}
return (
<Constraints.Horizontal max={props.horizontalConstraint}>
<input
id={props.id}
name={props.name}
type="text"
value={props.value}
onChange={props.onChange}
onBlur={props.onBlur}
onFocus={props.onFocus}
disabled={props.isDisabled}
placeholder={props.placeholder}
readOnly={props.isReadOnly}
autoFocus={props.isAutofocussed}
autoComplete={props.autoComplete}
css={getInputStyles(props, theme)}
// Allow to override the styles by passing a `className` prop.
// Custom styles can also be passed using the `css` prop from emotion.
// https://emotion.sh/docs/css-prop#style-precedence
className={props.className}
{...filterDataAttributes(props)}
/* ARIA */
aria-readonly={props.isReadOnly}
role="textbox"
contentEditable={!props.isReadOnly}
/>
</Constraints.Horizontal>
);
};

TextInput.displayName = 'TextInput';
TextInput.defaultProps = defaultProps;
TextInput.isEmpty = (value: TTextInputProps['value']) =>
!value || value.trim().length === 0;

export default TextInput;

1 comment on commit 7d56aa9

@vercel
Copy link

@vercel vercel bot commented on 7d56aa9 Apr 27, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.