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

fix: inputs: adds forward ref support, adjusts default wait interval #177

Merged
merged 1 commit into from
Jun 13, 2022
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
30 changes: 18 additions & 12 deletions src/components/Inputs/Input.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { Ref } from 'react';
import { Placement, Strategy } from '@floating-ui/react-dom';
import { IconName, IconProps } from '../Icon';
import { LabelProps } from '../Label';
Expand Down Expand Up @@ -146,6 +146,10 @@ export interface TextAreaProps
* @default false
*/
enableExpand?: boolean;
/**
* The text area component ref.
*/
ref?: Ref<HTMLTextAreaElement>;
/**
* The text area required attribute.
* @default false
Expand All @@ -164,6 +168,12 @@ export interface TextAreaProps
}

export interface TextInputProps extends InputProps<HTMLInputElement> {
/**
* option to show the clear input button.
* default is true for backward compatibility
* @default true
*/
clearable?: boolean;
/**
* The input html type.
* @default 'text'
Expand All @@ -175,22 +185,18 @@ export interface TextInputProps extends InputProps<HTMLInputElement> {
*/
numbersOnly?: boolean;
/**
* The input required attribute.
* @default false
* onclear event handler.
*/
required?: boolean;

onClear?: React.MouseEventHandler<Element>;
/**
* option to show the clear input button.
* default is true for backward compatibility
* @default true
* The input component ref.
*/
clearable?: boolean;

ref?: Ref<HTMLInputElement>;
/**
* onclear event handler.
* The input required attribute.
* @default false
*/
onClear?: React.MouseEventHandler<Element>;
required?: boolean;
}

export interface InputProps<T>
Expand Down
128 changes: 68 additions & 60 deletions src/components/Inputs/SearchBox/SearchBox.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, Ref } from 'react';
import { IconName } from '../../Icon';
import {
TextInputWidth,
Expand All @@ -8,63 +8,71 @@ import {
TextInputTheme,
} from '../index';

export const SearchBox: FC<SearchBoxProps> = ({
allowDisabledFocus = false,
ariaLabel,
autoFocus = false,
classNames,
clearButtonAriaLabel,
disabled = false,
iconProps,
iconButtonProps = {
allowDisabledFocus: false,
disabled: false,
iconProps: { path: IconName.mdiMagnify },
},
inputWidth = TextInputWidth.fitContent,
labelProps,
maxlength,
minlength,
name,
onBlur,
onChange,
onFocus,
onKeyDown,
placeholder = 'Search',
shape = TextInputShape.Rectangle,
style,
theme = TextInputTheme.light,
value,
waitInterval = 500,
...rest
}) => (
<form role="search">
<TextInput
{...rest}
allowDisabledFocus={allowDisabledFocus}
ariaLabel={ariaLabel}
autoFocus={autoFocus}
classNames={classNames}
clearButtonAriaLabel={clearButtonAriaLabel}
disabled={disabled}
htmlType="search"
iconProps={iconProps}
iconButtonProps={iconButtonProps}
inputWidth={inputWidth}
labelProps={labelProps}
maxlength={maxlength}
minlength={minlength}
name={name}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
onKeyDown={onKeyDown}
placeholder={placeholder}
shape={shape}
style={style}
theme={theme}
value={value}
waitInterval={waitInterval}
/>
</form>
export const SearchBox: FC<SearchBoxProps> = React.forwardRef(
(
{
allowDisabledFocus = false,
ariaLabel,
autoFocus = false,
classNames,
clearButtonAriaLabel,
disabled = false,
iconProps,
iconButtonProps = {
allowDisabledFocus: false,
disabled: false,
iconProps: { path: IconName.mdiMagnify },
},
inputWidth = TextInputWidth.fitContent,
labelProps,
maxlength,
minlength,
name,
onBlur,
onChange,
onFocus,
onKeyDown,
placeholder = 'Search',
shape = TextInputShape.Rectangle,
style,
theme = TextInputTheme.light,
value,
waitInterval = 500,
...rest
},
ref: Ref<HTMLInputElement>
) => {
return (
<form role="search">
<TextInput
{...rest}
ref={ref}
allowDisabledFocus={allowDisabledFocus}
ariaLabel={ariaLabel}
autoFocus={autoFocus}
classNames={classNames}
clearButtonAriaLabel={clearButtonAriaLabel}
disabled={disabled}
htmlType="search"
iconProps={iconProps}
iconButtonProps={iconButtonProps}
inputWidth={inputWidth}
labelProps={labelProps}
maxlength={maxlength}
minlength={minlength}
name={name}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
onKeyDown={onKeyDown}
placeholder={placeholder}
shape={shape}
style={style}
theme={theme}
value={value}
waitInterval={waitInterval}
/>
</form>
);
}
);
183 changes: 97 additions & 86 deletions src/components/Inputs/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from 'react';
import React, { FC, Ref, useState } from 'react';
import { Icon, IconName } from '../../Icon';
import { Label } from '../../Label';
import { TextInputWidth, TextAreaProps, TextInputTheme } from '../index';
Expand All @@ -7,94 +7,105 @@ import { mergeClasses, uniqueId } from '../../../shared/utilities';

import styles from '../input.module.scss';

export const TextArea: FC<TextAreaProps> = ({
allowDisabledFocus = false,
ariaLabel,
autoFocus = false,
classNames,
disabled = false,
enableExpand = false,
id,
inputWidth = TextInputWidth.fitContent,
labelProps,
maxlength,
minlength,
name,
onBlur,
onChange,
onFocus,
onKeyDown,
placeholder,
required = false,
style,
textAreaCols = 50,
textAreaRows = 5,
theme = TextInputTheme.light,
value,
waitInterval = 10,
...rest
}) => {
const [textAreaId] = useState<string>(uniqueId(id || 'textarea-'));

const textAreaClassNames: string = mergeClasses([
classNames,
styles.textArea,
{ [styles.textAreaNoExpand]: !enableExpand },
{ [styles.dark]: theme === TextInputTheme.dark },
{ [styles.inputStretch]: inputWidth === TextInputWidth.fill },
]);

const textAreaWrapperClassNames: string = mergeClasses([
styles.inputWrapper,
export const TextArea: FC<TextAreaProps> = React.forwardRef(
(
{
[styles.inputStretch]: inputWidth === TextInputWidth.fill,
allowDisabledFocus = false,
ariaLabel,
autoFocus = false,
classNames,
disabled = false,
enableExpand = false,
id,
inputWidth = TextInputWidth.fitContent,
labelProps,
maxlength,
minlength,
name,
onBlur,
onChange,
onFocus,
onKeyDown,
placeholder,
required = false,
style,
textAreaCols = 50,
textAreaRows = 5,
theme = TextInputTheme.light,
value,
waitInterval = 10,
...rest
},
]);
ref: Ref<HTMLTextAreaElement>
) => {
const [textAreaId] = useState<string>(uniqueId(id || 'textarea-'));

const handleChange = useDebounce<React.ChangeEvent<HTMLTextAreaElement>>(
(_event?: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) =>
triggerChange(_event),
waitInterval
);
const textAreaClassNames: string = mergeClasses([
classNames,
styles.textArea,
{ [styles.textAreaNoExpand]: !enableExpand },
{ [styles.dark]: theme === TextInputTheme.dark },
{ [styles.inputStretch]: inputWidth === TextInputWidth.fill },
]);

const triggerChange = (
_event?: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
) => {
onChange && onChange(_event);
};
const textAreaWrapperClassNames: string = mergeClasses([
styles.inputWrapper,
{
[styles.inputStretch]: inputWidth === TextInputWidth.fill,
},
]);

const handleChange = useDebounce<
React.ChangeEvent<HTMLTextAreaElement>
>(
(
_event?: React.ChangeEvent<
HTMLTextAreaElement | HTMLInputElement
>
) => triggerChange(_event),
waitInterval
);

const triggerChange = (
_event?: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>
) => {
onChange && onChange(_event);
};

return (
<div className={textAreaWrapperClassNames}>
{labelProps && <Label {...labelProps} />}
<textarea
{...rest}
aria-disabled={allowDisabledFocus}
aria-label={ariaLabel}
autoFocus={autoFocus}
className={textAreaClassNames}
cols={textAreaCols}
disabled={disabled}
id={textAreaId}
maxLength={maxlength}
minLength={minlength}
name={name}
onChange={!allowDisabledFocus ? handleChange : null}
onBlur={!allowDisabledFocus ? onBlur : null}
onFocus={!allowDisabledFocus ? onFocus : null}
onKeyDown={!allowDisabledFocus ? onKeyDown : null}
placeholder={placeholder}
required={required}
style={style}
rows={textAreaRows}
tabIndex={0}
value={value}
/>
{enableExpand && (
<Icon
classNames={styles.textAreaResizeIcon}
path={IconName.mdiResizeBottomRight}
return (
<div className={textAreaWrapperClassNames}>
{labelProps && <Label {...labelProps} />}
<textarea
{...rest}
ref={ref}
aria-disabled={allowDisabledFocus}
aria-label={ariaLabel}
autoFocus={autoFocus}
className={textAreaClassNames}
cols={textAreaCols}
disabled={disabled}
id={textAreaId}
maxLength={maxlength}
minLength={minlength}
name={name}
onChange={!allowDisabledFocus ? handleChange : null}
onBlur={!allowDisabledFocus ? onBlur : null}
onFocus={!allowDisabledFocus ? onFocus : null}
onKeyDown={!allowDisabledFocus ? onKeyDown : null}
placeholder={placeholder}
required={required}
style={style}
rows={textAreaRows}
tabIndex={0}
value={value}
/>
)}
</div>
);
};
{enableExpand && (
<Icon
classNames={styles.textAreaResizeIcon}
path={IconName.mdiResizeBottomRight}
/>
)}
</div>
);
}
);
Loading