Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
ui-components: Input component
Browse files Browse the repository at this point in the history
- TextInput component
- NumberInput component
- BaseInput - can be used to compose own
type of Input component.
  • Loading branch information
koorosh committed May 13, 2020
1 parent e30ed9b commit b0def8e
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 257 deletions.
93 changes: 93 additions & 0 deletions packages/ui-components/src/Input/BaseInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, {
ChangeEvent,
CSSProperties,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import classNames from "classnames/bind";
import styles from "./styles.module.scss";

export interface InputProps<T = string | number> {
type?: T extends string ? "text" : "number";
initialValue?: T;
value?: T;
onChange?: (value: T) => void;
className?: string;
style?: CSSProperties;
autoComplete?: string;
placeholder?: string;
name?: string;
disabled?: boolean;
invalid?: boolean;
}

const cx = classNames.bind(styles);

export const BaseInput: React.FC<InputProps> = ({
name,
type = "text",
autoComplete = "off",
className,
style,
value: outerValue = "",
initialValue = "",
placeholder,
onChange,
disabled = false,
invalid = false,
}) => {
const [value, setValue] = useState<string | number>(
outerValue || initialValue,
);
const [isDirty, setDirtyState] = useState<boolean>(false);

useEffect(() => {
if (!outerValue && !isDirty) {
return;
}
setDirtyState(true);
setValue(outerValue);
}, [outerValue, isDirty]);

const onChangeHandler = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const nextValue = event.target.value;
setDirtyState(true);
setValue(nextValue);
if (onChange && !disabled) {
onChange(nextValue);
}
},
[onChange, disabled],
);

const classnames = useMemo(
() =>
cx(
"input",
{
disabled,
invalid,
active: !disabled && !invalid,
},
className,
),
[className, invalid, disabled],
);

return (
<input
name={name}
type={type}
value={value}
placeholder={placeholder}
className={classnames}
style={style}
onChange={onChangeHandler}
autoComplete={autoComplete}
disabled={disabled}
/>
);
};
89 changes: 0 additions & 89 deletions packages/ui-components/src/Input/Input.module.scss

This file was deleted.

167 changes: 0 additions & 167 deletions packages/ui-components/src/Input/Input.tsx

This file was deleted.

Loading

0 comments on commit b0def8e

Please sign in to comment.