There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
refactor(component): input component (#2275)
Himself6553db6a6e9de3
diff --git a/packages/component/src/index.ts b/packages/component/src/index.tsindex c639b16950..2031c16b8c 100644--- a/packages/component/src/index.ts+++ b/packages/component/src/index.ts@@ -3,7 +3,6 @@ export * from './styles';export * from './ui/breadcrumbs';export * from './ui/button';export * from './ui/confirm';-export * from './ui/divider';export * from './ui/empty';export * from './ui/input';export * from './ui/layout';diff --git a/packages/component/src/ui/divider/index.ts b/packages/component/src/ui/divider/index.tsdeleted file mode 100644index 8a7925fd93..0000000000--- a/packages/component/src/ui/divider/index.ts+++ /dev/null@@ -1,9 +0,0 @@-import MuiDivider from '@mui/material/Divider';--import { styled } from '../../styles';--export const Divider = styled(MuiDivider)(() => {- return {- borderColor: 'var(--affine-border-color)',- };-});diff --git a/packages/component/src/ui/empty/empty-svg.tsx b/packages/component/src/ui/empty/empty-svg.tsxindex b18ad5624e..bf29ee34f8 100644--- a/packages/component/src/ui/empty/empty-svg.tsx+++ b/packages/component/src/ui/empty/empty-svg.tsx@@ -1,7 +1,7 @@-import type { SvgIconProps } from '@mui/material/SvgIcon';import SvgIcon from '@mui/material/SvgIcon';+import { memo } from 'react';-export const EmptySvg = (_props: SvgIconProps) => {+export const EmptySvg = memo(function EmptySvg() {return (<SvgIconwidth="200"@@ -411,4 +411,4 @@ export const EmptySvg = (_props: SvgIconProps) => {</defs></SvgIcon>);-};+});diff --git a/packages/component/src/ui/input/index.css.ts b/packages/component/src/ui/input/index.css.tsnew file mode 100644index 0000000000..84ca25ad5d--- /dev/null+++ b/packages/component/src/ui/input/index.css.ts@@ -0,0 +1,34 @@+import { createVar, style } from '@vanilla-extract/css';++export const heightVar = createVar('heightVar');+export const widthVar = createVar('widthVar');++export const inputStyle = style({+ vars: {+ [heightVar]: 'unset',+ [widthVar]: '100%',+ },+ width: widthVar,+ height: heightVar,+ lineHeight: '22px',+ padding: '8px 12px',+ color: 'var(--affine-text-primary-color)',+ border: '1px solid',+ borderColor: 'var(--affine-border-color)', // TODO: check out disableColor,+ backgroundColor: 'var(--affine-white)',+ borderRadius: '10px',+ selectors: {+ '&[data-no-border="true"]': {+ border: 'unset',+ },+ '&[data-disabled="true"]': {+ color: 'var(--affine-text-disable-color)',+ },+ '&::placeholder': {+ color: 'var(--affine-placeholder-color)',+ },+ '&:focus': {+ borderColor: 'var(--affine-primary-color)',+ },+ },+});diff --git a/packages/component/src/ui/input/index.stories.tsx b/packages/component/src/ui/input/index.stories.tsxnew file mode 100644index 0000000000..c5a12bcc3f--- /dev/null+++ b/packages/component/src/ui/input/index.stories.tsx@@ -0,0 +1,39 @@+import { expect } from '@storybook/jest';+import type { Meta, StoryFn } from '@storybook/react';+import { userEvent, within } from '@storybook/testing-library';++import { Input } from '.';++export default {+ title: 'AFFiNE/Input',+ component: Input,+} satisfies Meta<typeof Input>;++export const Basic: StoryFn<typeof Input> = () => {+ return <Input data-testid="test-input" defaultValue="test" />;+};++Basic.play = async ({ canvasElement }) => {+ const element = within(canvasElement);+ const item = element.getByTestId('test-input') as HTMLInputElement;+ expect(item).toBeTruthy();+ expect(item.value).toBe('test');+ userEvent.clear(item);+ userEvent.type(item, 'test 2');+ expect(item.value).toBe('test 2');+};++export const DynamicHeight: StoryFn<typeof Input> = () => {+ return <Input width={200} data-testid="test-input" />;+};++DynamicHeight.play = async ({ canvasElement }) => {+ const element = within(canvasElement);+ const item = element.getByTestId('test-input') as HTMLInputElement;+ expect(item).toBeTruthy();+ expect(item.getBoundingClientRect().width).toBe(200);+};++export const NoBorder: StoryFn<typeof Input> = () => {+ return <Input noBorder={true} data-testid="test-input" />;+};diff --git a/packages/component/src/ui/input/input.tsx b/packages/component/src/ui/input/input.tsxindex 43e8e2d15e..b97e6a475d 100644--- a/packages/component/src/ui/input/input.tsx+++ b/packages/component/src/ui/input/input.tsx@@ -1,14 +1,16 @@+import { assignInlineVars } from '@vanilla-extract/dynamic';+import clsx from 'clsx';import type {+ ChangeEventHandler,CSSProperties,FocusEventHandler,ForwardedRef,HTMLAttributes,- InputHTMLAttributes,KeyboardEventHandler,} from 'react';-import { forwardRef, useEffect, useState } from 'react';+import { forwardRef, useCallback } from 'react';-import { StyledInput } from './style';+import { heightVar, inputStyle, widthVar } from './index.css';type inputProps = {value?: string;@@ -27,39 +29,35 @@ type inputProps = {export const Input = forwardRef<HTMLInputElement, inputProps>(function Input({disabled,- value: valueProp,+ value,placeholder,maxLength,minLength,height,width,onChange,- onBlur,- onKeyDown,noBorder = false,...otherProps}: inputProps,ref: ForwardedRef<HTMLInputElement>) {- const [value, setValue] = useState<string>(valueProp || '');- const handleChange: InputHTMLAttributes<HTMLInputElement>['onChange'] = e => {- const { value } = e.target;- setValue(value);- onChange && onChange(value);- };-- const handleBlur: InputHTMLAttributes<HTMLInputElement>['onBlur'] = e => {- onBlur && onBlur(e);- };- const handleKeyDown: InputHTMLAttributes<HTMLInputElement>['onKeyDown'] =+ const handleChange = useCallback<ChangeEventHandler<HTMLInputElement>>(e => {- onKeyDown && onKeyDown(e);- };- useEffect(() => {- setValue(valueProp || '');- }, [valueProp]);+ const { value } = e.target;+ onChange && onChange(value);+ },+ [onChange]+ );+return (- <StyledInput+ <input+ className={clsx(inputStyle, otherProps.className)}+ style={assignInlineVars({+ [widthVar]: width ? `${width}px` : '100%',+ [heightVar]: height ? `${height}px` : 'unset',+ })}+ data-no-border={noBorder}+ data-disabled={disabled}ref={ref}value={value}disabled={disabled}@@ -68,10 +66,7 @@ export const Input = forwardRef<HTMLInputElement, inputProps>(function Input(maxLength={maxLength}minLength={minLength}onChange={handleChange}- onBlur={handleBlur}- onKeyDown={handleKeyDown}height={height}- noBorder={noBorder}{...otherProps}/>);diff --git a/packages/component/src/ui/input/style.ts b/packages/component/src/ui/input/style.tsdeleted file mode 100644index 123bf45a62..0000000000--- a/packages/component/src/ui/input/style.ts+++ /dev/null@@ -1,31 +0,0 @@-import type { CSSProperties } from 'react';--import { styled } from '../../styles';--export const StyledInput = styled('input')<{- disabled?: boolean;- value?: string;- width?: CSSProperties['width'];- height?: CSSProperties['height'];- noBorder?: boolean;-}>(({ width, disabled, height, noBorder }) => {- return {- width: width || '100%',- height,- lineHeight: '22px',- padding: '8px 12px',- color: disabled- ? 'var(--affine-text-disable-color)'- : 'var(--affine-text-primary-color)',- border: noBorder ? 'unset' : `1px solid`,- borderColor: 'var(--affine-border-color)', // TODO: check out disableColor,- backgroundColor: 'var(--affine-white)',- borderRadius: '10px',- '&::placeholder': {- color: 'var(--affine-placeholder-color)',- },- '&:focus': {- borderColor: 'var(--affine-primary-color)',- },- };-});