forked from react95-io/React95
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextField.js
122 lines (113 loc) · 2.67 KB
/
TextField.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React from 'react';
import propTypes from 'prop-types';
import styled, { css } from 'styled-components';
import {
createDisabledTextStyles,
createFlatBoxStyles,
createScrollbars
} from '../common';
import { blockSizes } from '../common/system';
import { StyledCutout } from '../Cutout/Cutout';
const sharedWrapperStyles = css`
display: flex;
align-items: center;
width: ${({ fullWidth }) => (fullWidth ? '100%' : 'auto')};
min-height: ${blockSizes.md};
`;
const Wrapper = styled(StyledCutout).attrs({
'data-testid': 'variant-default'
})`
${sharedWrapperStyles}
background: ${({ theme, isDisabled }) =>
isDisabled ? theme.material : theme.canvas};
`;
const FlatWrapper = styled.div.attrs({
'data-testid': 'variant-flat'
})`
${createFlatBoxStyles()}
${sharedWrapperStyles}
position: relative;
`;
const sharedInputStyles = css`
display: block;
box-sizing: border-box;
width: 100%;
height: 100%;
outline: none;
border: none;
background: none;
font-size: 1rem;
min-height: 27px;
font-family: inherit;
color: ${({ theme }) => theme.canvasText};
${({ disabled, variant }) =>
variant !== 'flat' && disabled && createDisabledTextStyles()}
`;
const StyledTextInput = styled.input`
${sharedInputStyles}
padding: 0 8px;
`;
const StyledTextArea = styled.textarea`
${sharedInputStyles}
padding: 8px;
resize: none;
${({ variant }) => createScrollbars(variant)}
`;
const TextField = React.forwardRef(function TextField(props, ref) {
const {
className,
disabled,
fullWidth,
multiline,
onChange,
shadow,
style,
type,
variant,
...otherProps
} = props;
const WrapperComponent = variant === 'flat' ? FlatWrapper : Wrapper;
const Input = multiline ? StyledTextArea : StyledTextInput;
return (
<WrapperComponent
className={className}
fullWidth={fullWidth}
isDisabled={disabled}
shadow={shadow}
style={style}
>
<Input
disabled={disabled}
onChange={disabled ? undefined : onChange}
readOnly={disabled}
ref={ref}
type={type}
variant={variant}
{...otherProps}
/>
</WrapperComponent>
);
});
TextField.defaultProps = {
className: '',
disabled: false,
fullWidth: null,
multiline: false,
onChange: () => {},
shadow: false,
style: {},
type: 'text',
variant: 'default'
};
TextField.propTypes = {
className: propTypes.string,
disabled: propTypes.bool,
fullWidth: propTypes.bool,
multiline: propTypes.bool,
onChange: propTypes.func,
shadow: propTypes.bool,
style: propTypes.object,
type: propTypes.string,
variant: propTypes.oneOf(['default', 'flat'])
};
export default TextField;