Input.tsx 1.4 KB
Newer Older
1 2
import {WithStyled, borderFocusedColor, em, half, sameBorder, textLighterColor, transitionProps} from '~/utils/style';

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 16
    ${props => sameBorder({radius: !props.rounded || half(height)})};
    ${transitionProps('border-color')}
17 18 19 20 21 22 23 24 25 26 27

    &:hover,
    &:focus {
        border-color: ${borderFocusedColor};
    }

    &::placeholder {
        color: ${textLighterColor};
    }
`;

P
Peter Pan 已提交
28
type CustomeInputProps = {
29 30 31 32 33
    rounded?: boolean;
    value?: string;
    onChange?: (value: string) => unknown;
};

P
Peter Pan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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}
        />
    )
49 50
);

P
Peter Pan 已提交
51 52
Input.displayName = 'Input';

53
export default Input;