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

Updated InlineInput #160

Closed
wants to merge 6 commits into from
Closed
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
12 changes: 0 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
"name-initials": "^0.1.3",
"pluralize": "^8.0.0",
"popper.js": "^1.16.1",
"react-18-input-autosize": "^3.0.0",
"react-fast-compare": "^3.2.2",
"react-highlight-words": "^0.20.0",
"react-syntax-highlighter": "^15.5.0",
Expand Down
63 changes: 53 additions & 10 deletions src/form/Input/InlineInput/InlineInput.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { FC, forwardRef, Ref, InputHTMLAttributes } from 'react';
import AutosizeInput from 'react-18-input-autosize';
import {
FC,
forwardRef,
RefObject,
InputHTMLAttributes,
useState
} from 'react';
import { twMerge } from 'tailwind-merge';
import { InputTheme } from '../InputTheme';
import { useComponentTheme } from '../../../utils';
import { InputRef } from '../Input';

export interface InlineInputProps
extends InputHTMLAttributes<HTMLInputElement> {
Expand All @@ -21,6 +27,11 @@ export interface InlineInputProps
*/
inputClassName?: string;

/**
* Placeholder content
*/
placeholder?: string;

/**
* Don't collapse size to less than the placeholder
*/
Expand All @@ -40,22 +51,54 @@ export interface InlineInputProps
export const InlineInput: FC<InlineInputProps> = forwardRef(
(
{
className,
inputClassName,
placeholderIsMinWidth = true,
placeholder,
placeholderIsMinWidth,
theme: customTheme,
extraWidth,
onChange,
...rest
},
ref: Ref<HTMLInputElement>
ref: RefObject<InputRef>
) => {
const theme: InputTheme = useComponentTheme('input', customTheme);
const spanStyles = 'relative opacity-0 whitespace-pre select-none';
const [inputValue, setInputValue] = useState('');
const [placeholderValue, setPlaceholderValue] = useState(placeholder);

const onInputValueChange = event => {
setInputValue(event.target.value);
onChange(event);
};

const setRef = (node: HTMLInputElement) => {
ref?.current?.inputRef;

const placeholder = node?.placeholder;
const value = node?.value;

placeholder && setPlaceholderValue(placeholder);
value && setInputValue(value);
};
devconn99 marked this conversation as resolved.
Show resolved Hide resolved

return (
<AutosizeInput
devconn99 marked this conversation as resolved.
Show resolved Hide resolved
inputRef={ref}
inputClassName={twMerge(theme.inline, inputClassName)}
placeholderIsMinWidth={placeholderIsMinWidth}
{...rest}
/>
<div className={twMerge(className, theme.inline.base)}>
<span className={spanStyles} style={{ paddingRight: extraWidth ?? 0 }}>
{inputValue}
</span>
{!placeholderIsMinWidth === true && !inputValue && (
<span className={spanStyles}>{placeholder}</span>
)}
Comment on lines +90 to +92
Copy link
Contributor

Choose a reason for hiding this comment

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

This !placeholderIsMinWidth === true seems backwards. Now a component like this has a width of 0 and the placeholder isn't visible

InlineInput
  placeholder="Type here..."
  placeholderIsMinWidth
/>

<input
value={inputValue}
placeholder={placeholderValue}
className={twMerge(theme.inline.input, inputClassName)}
ref={setRef}
onChange={onInputValueChange}
{...rest}
/>
</div>
);
}
);
11 changes: 9 additions & 2 deletions src/form/Input/InputTheme.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export interface InputTheme {
base: string;
input: string;
inline: string;
inline: {
base: string;
input: string;
};
disabled: string;
focused: string;
fullWidth: string;
Expand All @@ -23,7 +26,11 @@ const baseTheme: InputTheme = {
focused: '',
input:
'flex-1 font-normal font-sans bg-transparent border-0 p-0 m-0 disabled:pointer-events-none outline-none px-0.5 disabled:cursor-not-allowed disabled:text-disabled',
inline: 'bg-transparent border-0 outline-none',
inline: {
base: 'min-w-[76px] relative',
input:
'w-full h-full absolute top-0 left-0 bg-transparent border-0 outline-none'
},
disabled: '',
fullWidth: 'w-full',
error: 'border-error',
Expand Down
3 changes: 2 additions & 1 deletion src/form/Select/SelectInput/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
Ref,
RefObject,
useCallback,
useEffect,
devconn99 marked this conversation as resolved.
Show resolved Hide resolved
useImperativeHandle,
useMemo,
useRef
Expand Down Expand Up @@ -499,7 +500,7 @@ export const SelectInput: FC<Partial<SelectInputProps>> = ({
>
{renderPrefix()}
<InlineInput
inputRef={el => (inputRef.current = el)}
ref={el => (inputRef.current = el)}
id={id}
style={{ fontSize }}
name={name}
Expand Down
Loading