UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

56 lines (55 loc) 2.11 kB
import _ from 'lodash'; import React from 'react'; import PropTypes from 'react-peek/prop-types'; import Validation from '../Validation/Validation'; import TextField from '../TextField/TextField'; import reducers from '../TextField/TextField.reducers'; import { lucidClassNames } from '../../util/style-helpers'; import { findTypes, omitProps } from '../../util/component-types'; const cx = lucidClassNames.bind('&-TextFieldValidated'); const { any, object, string } = PropTypes; const TextFieldValidatedError = (_props) => null; TextFieldValidatedError.displayName = 'TextFieldValidated.Error'; TextFieldValidatedError.peek = { description: ` Content that will be displayed as an error message. `, }; TextFieldValidatedError.propName = 'Error'; class TextFieldValidated extends React.Component { constructor() { super(...arguments); this.textFieldRef = React.createRef(); this.focus = () => { this.textFieldRef.current && this.textFieldRef.current.focus(); }; } render() { const { className, style, ...passThroughs } = this.props; const errorChildProps = _.map(findTypes(this.props, TextFieldValidated.Error), 'props'); return (React.createElement(Validation, { className: cx('&', className), style: style, Error: errorChildProps }, React.createElement(TextField, Object.assign({}, omitProps(passThroughs, undefined, _.keys(TextFieldValidated.propTypes), false), { ref: this.textFieldRef })))); } } TextFieldValidated.displayName = 'TextFieldValidated'; TextFieldValidated.Error = TextFieldValidatedError; TextFieldValidated.peek = { description: ` A composition of TextField and Validation. `, categories: ['controls', 'text'], }; TextFieldValidated.reducers = reducers; TextFieldValidated.propTypes = { style: object ` Passed to the root container. `, className: string ` Passed to the root container. `, Error: any ` Prop alternative to Error child component `, }; TextFieldValidated.defaultProps = TextField.defaultProps; export default TextFieldValidated;