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 5 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
39 changes: 31 additions & 8 deletions src/form/Input/InlineInput/InlineInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FC, forwardRef, Ref, InputHTMLAttributes } from 'react';
import AutosizeInput from 'react-18-input-autosize';
import { FC, forwardRef, Ref, InputHTMLAttributes, useState } from 'react';
import { twMerge } from 'tailwind-merge';
import { InputTheme } from '../InputTheme';
import { useComponentTheme } from '../../../utils';
Expand All @@ -21,6 +20,11 @@ export interface InlineInputProps
*/
inputClassName?: string;

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

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

return (
<AutosizeInput
inputRef={ref}
devconn99 marked this conversation as resolved.
Show resolved Hide resolved
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 && !inputValue && (
<span className={spanStyles}>{placeholder}</span>
)}
<input
value={inputValue}
placeholder={placeholder}
className={twMerge(theme.inline.input, inputClassName)}
ref={ref}
onChange={e => {
setInputValue(e.target.value);
onChange(e);
}}
{...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
Loading