SearchInput.tsx 2.5 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.
 */

17
import Input, {InputProps, padding} from '~/components/Input';
P
Peter Pan 已提交
18
import React, {FunctionComponent, useCallback, useRef} from 'react';
P
Peter Pan 已提交
19
import {WithStyled, math, position, transitionProps} from '~/utils/style';
20

21
import Icon from '~/components/Icon';
22
import styled from 'styled-components';
23

P
Peter Pan 已提交
24 25
const searchIconSize = '1.142857143';
const closeIconSize = '0.857142857';
26 27

const StyledInput = styled(Input)`
P
Peter Pan 已提交
28 29
    padding-left: ${math(`1em * ${searchIconSize} + ${padding} * 2`)};
    padding-right: ${math(`1em * ${closeIconSize} + ${padding} * 2`)};
30 31 32 33 34 35 36 37 38
    width: 100%;
`;

const Control = styled.div`
    position: relative;
`;

const SearchIcon = styled(Icon)`
    display: block;
P
Peter Pan 已提交
39 40 41
    transform: translateY(-50%) scale(${searchIconSize});
    transform-origin: center;
    ${position('absolute', '50%', null, null, padding)}
42
    pointer-events: none;
P
Peter Pan 已提交
43 44
    color: var(--text-lighter-color);
    ${transitionProps('color')}
45 46
`;

P
Peter Pan 已提交
47 48 49 50 51 52
const CloseIcon = styled(Icon)`
    display: block;
    transform: translateY(-50%) scale(${closeIconSize});
    transform-origin: center;
    ${position('absolute', '50%', padding, null, null)}
    cursor: pointer;
P
Peter Pan 已提交
53 54
    color: var(--text-lighter-color);
    ${transitionProps('color')}
P
Peter Pan 已提交
55 56

    &:hover {
P
Peter Pan 已提交
57
        color: var(--text-light-color);
P
Peter Pan 已提交
58 59 60
    }

    &:active {
P
Peter Pan 已提交
61
        color: var(--text-color);
P
Peter Pan 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    }
`;

const SearchInput: FunctionComponent<InputProps & WithStyled> = ({className, value, onChange, ...props}) => {
    const input = useRef<HTMLInputElement | null>(null);
    const clear = useCallback(() => {
        onChange?.('');
        input.current?.focus();
    }, [onChange]);

    return (
        <Control className={className}>
            <SearchIcon type="search" />
            <StyledInput ref={input} value={value} onChange={onChange} {...props} />
            {!!value && <CloseIcon type="close" onClick={clear} />}
        </Control>
    );
};
80 81

export default SearchInput;