Input.tsx 2.1 KB
Newer Older
P
Peter Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright 2020 Baidu Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

P
Peter Pan 已提交
17
import {WithStyled, em, half, sameBorder, transitionProps} from '~/utils/style';
18

P
Peter Pan 已提交
19
import React from 'react';
20 21 22 23 24 25 26 27 28 29 30
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;
31
    ${props => sameBorder({radius: !props.rounded || half(height)})};
P
Peter Pan 已提交
32 33 34 35
    background-color: var(--input-background-color);
    color: var(--text-color);
    caret-color: var(--text-color);
    ${transitionProps(['border-color', 'background-color', 'caret-color', 'color'])}
36 37 38

    &:hover,
    &:focus {
P
Peter Pan 已提交
39
        border-color: var(--border-focused-color);
40 41 42
    }

    &::placeholder {
P
Peter Pan 已提交
43 44
        color: var(--text-lighter-color);
        ${transitionProps('color')}
45 46 47
    }
`;

P
Peter Pan 已提交
48
type CustomeInputProps = {
49 50 51 52 53
    rounded?: boolean;
    value?: string;
    onChange?: (value: string) => unknown;
};

P
Peter Pan 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
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}
        />
    )
69 70
);

P
Peter Pan 已提交
71 72
Input.displayName = 'Input';

73
export default Input;