UNPKG

@itsjonq/controls

Version:

A control panel to develop React UI

42 lines (33 loc) 2.58 kB
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import React, { useState } from 'react'; import { FieldVariants } from './Field.Variants'; import { Label, FieldGroup } from './Field.styles'; import { createUniqueIdFactory } from '../../utils'; var fieldId = createUniqueIdFactory('Field'); /** * Renders an interactive input component to control the associated * field from state. This component renders into the <Controls /> component. */ export function Field(props) { var _useState = useState(fieldId()), _useState2 = _slicedToArray(_useState, 1), id = _useState2[0]; var prop = props.prop, type = props.type, restProps = _objectWithoutProperties(props, ["prop", "type"]); var Component = FieldVariants[type]; if (!Component) return null; return React.createElement(FieldGroup, { marginBottom: 8 }, React.createElement(Label, { label: prop, htmlFor: id }), React.createElement(Component, Object.assign({}, restProps, { id: id }))); }