Input.tsx 1.5 KB
Newer Older
P
Peter Pan 已提交
1
import {WithStyled, em, half, sameBorder, transitionProps} from '~/utils/style';
2

P
Peter Pan 已提交
3
import React from 'react';
4 5 6 7 8 9 10 11 12 13 14
import styled from 'styled-components';

export const padding = em(10);
export const height = em(36);

const StyledInput = styled.input<{rounded?: boolean}>`
    padding: ${padding};
    height: ${height};
    line-height: ${height};
    display: inline-block;
    outline: none;
15
    ${props => sameBorder({radius: !props.rounded || half(height)})};
P
Peter Pan 已提交
16 17 18 19
    background-color: var(--input-background-color);
    color: var(--text-color);
    caret-color: var(--text-color);
    ${transitionProps(['border-color', 'background-color', 'caret-color', 'color'])}
20 21 22

    &:hover,
    &:focus {
P
Peter Pan 已提交
23
        border-color: var(--border-focused-color);
24 25 26
    }

    &::placeholder {
P
Peter Pan 已提交
27 28
        color: var(--text-lighter-color);
        ${transitionProps('color')}
29 30 31
    }
`;

P
Peter Pan 已提交
32
type CustomeInputProps = {
33 34 35 36 37
    rounded?: boolean;
    value?: string;
    onChange?: (value: string) => unknown;
};

P
Peter Pan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
export type InputProps = Omit<React.ComponentPropsWithoutRef<'input'>, keyof CustomeInputProps | 'type' | 'className'> &
    CustomeInputProps;

const Input = React.forwardRef<HTMLInputElement, InputProps & WithStyled>(
    ({rounded, value, onChange, className, ...props}, ref) => (
        <StyledInput
            ref={ref}
            rounded={rounded}
            value={value}
            type="text"
            className={className}
            onChange={e => onChange?.(e.target.value)}
            {...props}
        />
    )
53 54
);

P
Peter Pan 已提交
55 56
Input.displayName = 'Input';

57
export default Input;