on 261ea40 help sign in
124 lines 3.0 kB history raw
1import React, { useCallback } from 'react';
2import cn from 'classnames';
4import Flex from './Flex';
5import { ErrorText } from './Typography';
7export default React.memo(function Input({
8 // visuals
9 type,
10 name,
11 label,
12 className,
13 accessory,
14 mono = false,
16 // callbacks
17 onEnter,
19 // state from hook
20 focused,
21 pass,
22 syncPass,
23 visiblyPassed,
24 error,
25 hintError,
26 data,
27 bind,
28 autoFocus,
29 disabled,
30 touched,
32 // ignored
33 initialValue,
34 validators,
35 transformers,
37 // extra
38 textarea = false,
39 ...rest
40}) {
41 // NB(shrugs): we disable exhaustive deps because we don't want the callbacks
42 // (whose identity might change between renders) to trigger a re-notify of
43 // state that we already know
44 // this also happens to prevent render loops when rendering a map of inputs
46 // notify parent of enter keypress iff not disabled and passing
47 const onKeyPress = useCallback(
48 e => !disabled && pass && e.key === 'Enter' && onEnter && onEnter(),
49 [disabled, pass] // eslint-disable-line react-hooks/exhaustive-deps
50 );
52 return (
53 <Flex
54 className={cn(className, 'mb1')}
55 col
56 style={{
57 ...(disabled && {
58 pointerEvents: 'none',
59 cursor: 'not-allowed',
60 }),
61 }}>
62 <Flex.Item
63 as="label"
64 className={cn('f6 lh-tall', {
65 black: !disabled,
66 gray4: disabled,
67 })}
68 htmlFor={name}>
69 {label}
70 </Flex.Item>
71 <Flex.Item as={Flex} className="rel" row>
72 <Flex.Item
73 as={type === 'textarea' ? 'textarea' : 'input'}
74 type={type === 'textarea' ? undefined : type}
75 {...rest}
76 // NOTE: 24px = 12px * 2 (from p3 styling)
77 style={type === 'textarea' ? { minHeight: 'calc(1rem + 24px)' } : {}}
78 className={cn(
79 'b b1 p3 outline-none',
80 { mono },
81 {
82 'bg-white': !disabled,
83 'bg-gray1': disabled,
84 },
85 {
86 gray4: !focused && !touched,
87 black: focused || touched,
88 },
89 {
90 'b-green3': visiblyPassed,
91 'b-black': focused && !visiblyPassed,
92 'b-yellow3': !focused && hintError,
93 'b-gray2': !focused && !hintError && !visiblyPassed,
94 }
95 // TODO: inputClassName ?
96 )}
97 id={name}
98 name={name}
99 onKeyPress={onKeyPress}
100 {...bind}
101 flex
102 />
103 {accessory && (
104 <div
105 className="abs"
106 style={{
107 top: 0,
108 right: 0,
109 height: '100%',
110 width: '44px',
111 overflow: 'hidden',
112 }}>
113 {accessory}
114 </div>
115 )}
116 </Flex.Item>
117 {error && (
118 <Flex.Item as={ErrorText} className="mv1">
119 {error}
120 </Flex.Item>
121 )}
122 </Flex>
123 );
124});