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

/**
* onChange handler
*/
onChange?: React.ChangeEventHandler<HTMLInputElement>;

devconn99 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Placeholder content
*/
placeholder?: string;

/**
* Don't collapse size to less than the placeholder
*/
Expand All @@ -40,22 +50,45 @@ export interface InlineInputProps
export const InlineInput: FC<InlineInputProps> = forwardRef(
(
{
className,
inputClassName,
placeholder = 'Type here...',
placeholderIsMinWidth = true,
devconn99 marked this conversation as resolved.
Show resolved Hide resolved
theme: customTheme,
extraWidth,
onChange,
...rest
},
ref: Ref<HTMLInputElement>
) => {
const theme: InputTheme = useComponentTheme('input', customTheme);
const [inputValue, setInputValue] = useState('');
const spanStyles =
'relative opacity-0 font-[inherit] text-[inherit] leading-none whitespace-pre select-none';
amcdnl marked this conversation as resolved.
Show resolved Hide resolved

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 ? extraWidth + 'px' : 0 }}
>
devconn99 marked this conversation as resolved.
Show resolved Hide resolved
{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 font-[inherit] text-[inherit] leading-[inherit] bg-transparent border-0 outline-none'
},
disabled: '',
fullWidth: 'w-full',
error: 'border-error',
Expand Down
Loading