-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate
input-utils
to TypeScript (#1867)
* refactor(packages/input-util): migrate to TypeScript * refactor(multiline-input): remove superfluous `@types` dependency * refactor: update signature * fix(input-utils): make `theme` optional * style(index): rename to `.ts` * refactor: revert removal of ref * fix(input-utils): ensure `remainingLocalizations` is optional * chore: add changeset * fix(multiline-input): introduce theme and forward to `getStyles` * fix(input-toggle): set `onClick` to required * refactor(input-utils/src/multiline-input/multiline-input.tsx): assign type to `useRef` * refactor(multiline-input): extend useRef, add warning
- Loading branch information
Showing
16 changed files
with
142 additions
and
107 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-uikit/input-utils': patch | ||
--- | ||
|
||
migrate `input-utils` to TypeScript |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 0 additions & 74 deletions
74
packages/components/inputs/input-utils/src/multiline-input/multiline-input.js
This file was deleted.
Oops, something went wrong.
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
89 changes: 89 additions & 0 deletions
89
packages/components/inputs/input-utils/src/multiline-input/multiline-input.tsx
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,89 @@ | ||
import React, { ChangeEventHandler } from 'react'; | ||
import { useTheme } from '@emotion/react'; | ||
import TextareaAutosize, { | ||
TextareaHeightChangeMeta, | ||
} from 'react-textarea-autosize'; | ||
import { filterDataAttributes, warning } from '@commercetools-uikit/utils'; | ||
import { getTextareaStyles } from './multiline-input.styles'; | ||
|
||
const MIN_ROW_COUNT = 1; | ||
|
||
export type TMultiLineInputProps = { | ||
autoComplete?: string; | ||
className?: string; | ||
hasError?: boolean; | ||
hasWarning?: boolean; | ||
id?: string; | ||
isAutofocussed?: boolean; | ||
isDisabled?: boolean; | ||
isReadOnly?: boolean; | ||
name?: string; | ||
onBlur?: ChangeEventHandler; | ||
onChange?: ChangeEventHandler; | ||
onFocus?: ChangeEventHandler; | ||
placeholder?: string; | ||
value: string; | ||
isOpen: boolean; | ||
onHeightChange?: (height: number, rowCount: number) => void; | ||
}; | ||
|
||
const MultilineInput = (props: TMultiLineInputProps) => { | ||
const theme = useTheme(); | ||
const { onHeightChange } = props; | ||
const ref = React.useRef<HTMLTextAreaElement | null>(null); | ||
const handleHeightChange = React.useCallback< | ||
(height: number, meta: TextareaHeightChangeMeta) => void | ||
>( | ||
(height: number, meta: TextareaHeightChangeMeta) => { | ||
const rowCount = Math.floor( | ||
ref.current?.scrollHeight || 0 / meta.rowHeight | ||
); | ||
if (onHeightChange) { | ||
onHeightChange(height, rowCount); | ||
} | ||
}, | ||
[ref, onHeightChange] | ||
); | ||
|
||
if (!props.isReadOnly) { | ||
warning( | ||
typeof props.onChange === 'function', | ||
'MultilineInput: "onChange" is required when is not read only.' | ||
); | ||
} | ||
|
||
return ( | ||
<TextareaAutosize | ||
ref={ref} | ||
name={props.name} | ||
onHeightChange={handleHeightChange} | ||
autoComplete={props.autoComplete} | ||
value={props.value} | ||
onChange={props.onChange} | ||
id={props.id} | ||
onBlur={props.onBlur} | ||
onFocus={props.onFocus} | ||
disabled={props.isDisabled} | ||
placeholder={props.placeholder} | ||
readOnly={props.isReadOnly} | ||
autoFocus={props.isAutofocussed} | ||
css={getTextareaStyles(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} | ||
/* ARIA */ | ||
aria-readonly={props.isReadOnly} | ||
aria-multiline="true" | ||
role="textbox" | ||
minRows={MIN_ROW_COUNT} | ||
maxRows={props.isOpen ? undefined : MIN_ROW_COUNT} | ||
cacheMeasurements={true} | ||
{...filterDataAttributes(props)} | ||
/> | ||
); | ||
}; | ||
|
||
MultilineInput.displayName = 'MultilineInput'; | ||
|
||
export default MultilineInput; |
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
File renamed without changes.
4acb66d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: