refactor: input component (#2999)

This commit is contained in:
Alex Yang
2023-07-04 14:52:46 +08:00
committed by GitHub
parent 8d2ffe3936
commit e871ffcba0
6 changed files with 30 additions and 28 deletions
@@ -14,7 +14,7 @@ import { useSetAtom } from 'jotai';
import type { KeyboardEvent } from 'react'; import type { KeyboardEvent } from 'react';
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useLayoutEffect } from 'react'; import { useLayoutEffect } from 'react';
import { useCallback, useRef, useState } from 'react'; import { useCallback, useState } from 'react';
import { openDisableCloudAlertModalAtom } from '../../../atoms'; import { openDisableCloudAlertModalAtom } from '../../../atoms';
import { useAppHelper } from '../../../hooks/use-workspaces'; import { useAppHelper } from '../../../hooks/use-workspaces';
@@ -45,7 +45,6 @@ const NameWorkspaceContent = ({
onClose, onClose,
}: NameWorkspaceContentProps) => { }: NameWorkspaceContentProps) => {
const [workspaceName, setWorkspaceName] = useState(''); const [workspaceName, setWorkspaceName] = useState('');
const isComposition = useRef(false);
const handleCreateWorkspace = useCallback(() => { const handleCreateWorkspace = useCallback(() => {
onConfirmName(workspaceName); onConfirmName(workspaceName);
@@ -53,7 +52,7 @@ const NameWorkspaceContent = ({
const handleKeyDown = useCallback( const handleKeyDown = useCallback(
(event: KeyboardEvent<HTMLInputElement>) => { (event: KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter' && workspaceName && !isComposition.current) { if (event.key === 'Enter' && workspaceName) {
handleCreateWorkspace(); handleCreateWorkspace();
} }
}, },
@@ -76,12 +75,6 @@ const NameWorkspaceContent = ({
maxLength={64} maxLength={64}
minLength={0} minLength={0}
onChange={setWorkspaceName} onChange={setWorkspaceName}
onCompositionStart={() => {
isComposition.current = true;
}}
onCompositionEnd={() => {
isComposition.current = false;
}}
/> />
<div className={style.buttonGroup}> <div className={style.buttonGroup}>
<Button <Button
@@ -86,7 +86,6 @@ export const WorkspaceDeleteModal = ({
onChange={setDeleteStr} onChange={setDeleteStr}
data-testid="delete-workspace-input" data-testid="delete-workspace-input"
placeholder={t['Placeholder of delete workspace']()} placeholder={t['Placeholder of delete workspace']()}
value={deleteStr}
width={315} width={315}
height={42} height={42}
/> />
@@ -71,7 +71,7 @@ export const ProfilePanel: FC<{
<Input <Input
width={280} width={280}
height={32} height={32}
value={input} defaultValue={input}
data-testid="workspace-name-input" data-testid="workspace-name-input"
placeholder={t['Workspace Name']()} placeholder={t['Workspace Name']()}
maxLength={64} maxLength={64}
@@ -219,7 +219,7 @@ export const EditCollection = ({
<Input <Input
data-testid="input-collection-title" data-testid="input-collection-title"
placeholder="Untitled Collection" placeholder="Untitled Collection"
value={value.name} defaultValue={value.name}
onChange={text => onChange={text =>
onChange({ onChange({
...value, ...value,
+25 -15
View File
@@ -1,7 +1,7 @@
import { assignInlineVars } from '@vanilla-extract/dynamic'; import { assignInlineVars } from '@vanilla-extract/dynamic';
import clsx from 'clsx'; import clsx from 'clsx';
import { useCompositionInput } from 'foxact/use-composition-input';
import type { import type {
ChangeEventHandler,
CSSProperties, CSSProperties,
FocusEventHandler, FocusEventHandler,
ForwardedRef, ForwardedRef,
@@ -12,8 +12,10 @@ import { forwardRef, useCallback } from 'react';
import { heightVar, inputStyle, widthVar } from './index.css'; import { heightVar, inputStyle, widthVar } from './index.css';
type inputProps = { type InputProps = {
value?: string; // We don't have `value` props here,
// see https://foxact.skk.moe/use-composition-input
defaultValue?: string | undefined;
placeholder?: string; placeholder?: string;
disabled?: boolean; disabled?: boolean;
width?: CSSProperties['width']; width?: CSSProperties['width'];
@@ -24,12 +26,19 @@ type inputProps = {
onBlur?: FocusEventHandler<HTMLInputElement>; onBlur?: FocusEventHandler<HTMLInputElement>;
onKeyDown?: KeyboardEventHandler<HTMLInputElement>; onKeyDown?: KeyboardEventHandler<HTMLInputElement>;
noBorder?: boolean; noBorder?: boolean;
} & Omit<HTMLAttributes<HTMLInputElement>, 'onChange'>; } & Omit<
HTMLAttributes<HTMLInputElement>,
| 'onChange'
| 'value'
| 'defaultValue'
| 'onCompositionStart'
| 'onCompositionEnd'
>;
export const Input = forwardRef<HTMLInputElement, inputProps>(function Input( export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
{ {
disabled, disabled,
value, defaultValue,
placeholder, placeholder,
maxLength, maxLength,
minLength, minLength,
@@ -39,15 +48,16 @@ export const Input = forwardRef<HTMLInputElement, inputProps>(function Input(
noBorder = false, noBorder = false,
className, className,
...otherProps ...otherProps
}: inputProps, }: InputProps,
ref: ForwardedRef<HTMLInputElement> ref: ForwardedRef<HTMLInputElement>
) { ) {
const handleChange = useCallback<ChangeEventHandler<HTMLInputElement>>( const inputProps = useCompositionInput(
e => { useCallback(
const { value } = e.target; (value: string) => {
onChange && onChange(value); onChange && onChange(value);
}, },
[onChange] [onChange]
)
); );
return ( return (
@@ -60,15 +70,15 @@ export const Input = forwardRef<HTMLInputElement, inputProps>(function Input(
data-no-border={noBorder} data-no-border={noBorder}
data-disabled={disabled} data-disabled={disabled}
ref={ref} ref={ref}
value={value} defaultValue={defaultValue}
disabled={disabled} disabled={disabled}
placeholder={placeholder} placeholder={placeholder}
width={width} width={width}
maxLength={maxLength} maxLength={maxLength}
minLength={minLength} minLength={minLength}
onChange={handleChange}
height={height} height={height}
{...otherProps} {...otherProps}
{...inputProps}
/> />
); );
}); });
+1 -1
View File
@@ -12,7 +12,7 @@ export const DebugContent: PluginUIAdapter['debugContent'] = () => {
<div> <div>
<span>OpenAI API Key:</span> <span>OpenAI API Key:</span>
<Input <Input
value={key ?? ''} defaultValue={key ?? undefined}
onChange={useCallback( onChange={useCallback(
(newValue: string) => { (newValue: string) => {
setKey(newValue); setKey(newValue);