Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | 176x 176x 176x 176x 176x 30x 176x 176x 30x 30x 30x 30x 3x 30x 1x 1x 30x 2x 2x 1x 1x 1x 30x 6x 6x 6x 6x 6x 6x 4x 4x 30x 2x 2x 2x 1x 2x 30x 3x 3x 3x 3x 3x 2x 1x 2x 30x 30x 7x 2x 2x 1x 1x 40x 40x 40x 40x | import React from 'react'; import PropTypes from 'react-peek/prop-types'; import _ from 'lodash'; import { lucidClassNames } from '../../util/style-helpers'; import { omitProps, StandardProps, Overwrite, } from '../../util/component-types'; import reducers from './TextField.reducers'; import * as KEYCODE from '../../constants/key-code'; const cx = lucidClassNames.bind('&-TextField'); const { bool, string, func, number, object, oneOfType } = PropTypes; export interface ITextFieldProps extends StandardProps { /** Set the TextField to multi line mode. Under the hood this will use a `textarea` instead of an `input` if set to `true`. */ isMultiLine?: boolean; /** Disables the TextField by greying it out. */ isDisabled?: boolean; /** Initial number of rows a multi line TextField should have. Ignored when not in multi-line mode. */ rows?: number; /** Fires an event every time the user types text into the TextField. */ onChange?: ( value: string, { event, props, }: { event: React.FormEvent; props: ITextFieldProps; } ) => void; /** Fires an on the `input`'s onBlur. */ onBlur?: ( currentValue: string, { event, props, }: { event: React.FocusEvent; props: ITextFieldProps; } ) => void; /** Fires an event, debounced by `debounceLevel` when the user types text into the TextField. */ onChangeDebounced?: ( value: string, { event, props, }: { event: React.FormEvent; props: ITextFieldProps; } ) => void; /** Fires an event on every keydown */ onKeyDown?: ({ event, props, }: { event: React.KeyboardEvent; props: ITextFieldProps; }) => void; /** Fires an event when the user hits "enter" from the TextField. You shouldn't use it if you're using `isMultiLine`. */ onSubmit?: ( value: string, { event, props, }: { event: React.FormEvent; props: ITextFieldProps; } ) => void; /** Set the value of the input. */ value: string | number; /** Number of milliseconds to debounce the `onChangeDebounced` callback. Only useful if you provide an `onChangeDebounced` handler. */ debounceLevel?: number; /** Set the holding time, in milliseconds, that the component will wait if the user is typing and the component gets a new `value` prop. Any time the user hits a key, it starts a timer that prevents state changes from flowing in to the component until the timer has elapsed. This was heavily inspired by the [lazy-input](https:/docs.npmjs.com/package/lazy-input) component. */ lazyLevel?: number; } export type ITextFieldPropsWithPassThroughs = Overwrite< React.InputHTMLAttributes<HTMLInputElement>, ITextFieldProps >; export interface ITextFieldState { value: number | string; isHolding: boolean; isMounted: boolean; } class TextField extends React.Component< ITextFieldPropsWithPassThroughs, ITextFieldState, {} > { static displayName = 'TextField'; static peek = { description: ` TextField should cover all your text input needs. It is able to handle single and multi line inputs. `, categories: ['controls', 'text'], }; static propTypes = { style: object` Styles that are passed through to native control. `, isMultiLine: bool` Set the TextField to multi line mode. Under the hood this will use a \`textarea\` instead of an \`input\` if set to \`true\`. `, isDisabled: bool` Disables the TextField by greying it out. `, rows: number` Initial number of rows a multi line TextField should have. Ignored when not in multi-line mode. `, className: string` Class names that are appended to the defaults. `, onChange: func` Fires an event every time the user types text into the TextField. Signature: \`(value, { event, props }) => {}\` `, onBlur: func` Fires an on the \`input\`'s onBlur. Signature: \`(currentValue, { event, props }) => {}\` `, onChangeDebounced: func` Fires an event, debounced by \`debounceLevel\`, when the user types text into the TextField. Signature: \`(value, { event, props }) => {}\` `, onKeyDown: func` Fires an event on every keydown Signature: \`({ event, props }) => {}\` `, onSubmit: func` Fires an event when the user hits "enter" from the TextField. You shouldn't use it if you're using \`isMultiLine\`. Signature: \`(value, { event, props }) => {}\` `, value: oneOfType([number, string])` Set the value of the input. `, debounceLevel: number` Number of milliseconds to debounce the \`onChangeDebounced\` callback. Only useful if you provide an \`onChangeDebounced\` handler. `, lazyLevel: number` Set the holding time, in milliseconds, that the component will wait if the user is typing and the component gets a new \`value\` prop. Any time the user hits a key, it starts a timer that prevents state changes from flowing in to the component until the timer has elapsed. This was heavily inspired by the [lazy-input](https:/docs.npmjs.com/package/lazy-input) component. `, }; state = { value: this.props.value, isHolding: false, isMounted: false, }; static defaultProps = { style: undefined, isDisabled: false, isMultiLine: false, onBlur: _.noop, onChange: _.noop, onChangeDebounced: _.noop, onSubmit: _.noop, onKeyDown: _.noop, rows: 5, debounceLevel: 500, lazyLevel: 1000, value: '', }; static reducers = reducers; private textareaElement = React.createRef<HTMLTextAreaElement>(); private inputElement = React.createRef<HTMLInputElement>(); private nativeElement = this.props.isMultiLine ? this.textareaElement : this.inputElement; private handleChangeDebounced = _.debounce( ( value: string, { event, props, }: { event: React.FormEvent; props: ITextFieldProps; } ): void => { this.props.onChangeDebounced && this.props.onChangeDebounced(value, { event, props }); }, this.props.debounceLevel ); private releaseHold = _.debounce((): void => { Iif (!this.state.isMounted) { return; } this.setState({ isHolding: false }); }, this.props.lazyLevel); private updateWhenReady = _.debounce((newValue): void => { Iif (!this.state.isMounted) { return; } if (this.state.isHolding) { this.updateWhenReady(newValue); } else Eif (newValue !== this.state.value) { this.setState({ value: newValue }); } }, this.props.lazyLevel); handleChange = (event: React.FormEvent): void => { const { onChange, onChangeDebounced } = this.props; const value = _.get(event, 'target.value', ''); this.setState({ value, isHolding: true }); this.releaseHold(); onChange && onChange(value, { event, props: this.props }); // Also call the debounced handler in case the user wants debounced change // events. if (onChangeDebounced !== _.noop) { event.persist(); // https://facebook.github.io/react/docs/events.html#event-pooling this.handleChangeDebounced(value, { event, props: this.props }); } }; handleBlur = (event: React.FocusEvent): void => { const { onBlur, onChangeDebounced } = this.props; const value = _.get(event, 'target.value', ''); if (onChangeDebounced !== _.noop) { this.handleChangeDebounced.flush(); } onBlur && onBlur(value, { event, props: this.props }); }; handleKeyDown = (event: React.KeyboardEvent): void => { const { props, props: { onSubmit, onKeyDown, onChangeDebounced }, } = this; const value = _.get(event, 'target.value', ''); // If the consumer passed an onKeyDown, we call it Eif (onKeyDown) { onKeyDown({ event, props }); } if (event.keyCode === KEYCODE.Enter) { if (onChangeDebounced !== _.noop) { this.handleChangeDebounced.flush(); } onSubmit && onSubmit(value, { event, props: this.props }); } }; focus = (): void => { /* istanbul ignore next */ (this.nativeElement.current as HTMLElement).focus(); }; UNSAFE_componentWillMount(): void { this.setState({ isMounted: true }); } componentWillUnmount(): void { this.setState({ isMounted: false }); } UNSAFE_componentWillReceiveProps(nextProps: ITextFieldProps): void { // Allow consumer to optionally control state Eif (_.has(nextProps, 'value')) { if (this.state.isHolding) { this.updateWhenReady(nextProps.value); } else { this.setState({ value: nextProps.value }); } } } render(): React.ReactNode { const { className, isDisabled, isMultiLine, rows, style, ...passThroughs } = this.props; const { value } = this.state; const finalProps = { ...omitProps(passThroughs, undefined, [ ...Object.keys(TextField.propTypes), 'children', ]), className: cx( '&', { '&-is-disabled': isDisabled, '&-is-multi-line': isMultiLine, '&-is-single-line': !isMultiLine, }, className ), disabled: isDisabled, onChange: this.handleChange, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, style, rows, value, }; return isMultiLine ? ( <textarea ref={this.textareaElement} {...finalProps} /> ) : ( <input type='text' ref={this.inputElement} {...finalProps} /> ); } } export default TextField; |