UNPKG

@trellixio/roaster-coffee

Version:
70 lines (64 loc) 2.31 kB
'use strict'; var React = require('react'); var index$1 = require('../../utils/classNames/index.js'); var index = require('../../utils/useUid/index.js'); require('@floating-ui/react'); function _interopNamespaceDefault(e) { var n = Object.create(null); if (e) { for (var k in e) { n[k] = e[k]; } } n.default = e; return Object.freeze(n); } var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React); function defaultOptionResolver(option) { if (typeof option === "string") { return { label: option, value: option }; } if ("id" in option && ("name" in option || "title" in option)) { return { label: option.name || option.title, value: option.id }; } return option; } const Select = React__namespace.forwardRef((props, ref) => { const { label, data, helpText, prefix, prefixWidth = prefix ? 24 : 12, onChange, disabled, id, selectClassName, labelClassName, error, optionResolver = defaultOptionResolver, defaultValue } = props; const uid = index.useUid(id); const normalizedData = data.map(optionResolver); const options = normalizedData.map((item) => /* @__PURE__ */ React__namespace.createElement("option", { key: item.value, value: item.value, disabled: item.disabled }, item.label)); const handleChange = (event) => { onChange?.(event.currentTarget.value); }; return /* @__PURE__ */ React__namespace.createElement("label", { htmlFor: uid, className: index$1.classNames({ error }, labelClassName) }, /* @__PURE__ */ React__namespace.createElement("p", null, label), /* @__PURE__ */ React__namespace.createElement("div", { className: index$1.classNames({ "prefixed-input-container": prefix }) }, prefix && /* @__PURE__ */ React__namespace.createElement("span", { className: "input-prefix" }, prefix), /* @__PURE__ */ React__namespace.createElement( "select", { id: uid, ref, className: index$1.classNames({ prefixed: prefix }, selectClassName), disabled, defaultValue, style: { paddingLeft: prefixWidth }, onChange: handleChange }, options )), /* @__PURE__ */ React__namespace.createElement("small", null, error || helpText)); }); Select.displayName = "Select"; exports.Select = Select; //# sourceMappingURL=Select.js.map