@trellixio/roaster-coffee
Version:
Beans' product component library
55 lines (52 loc) • 1.93 kB
JavaScript
import * as React from 'react';
import { classNames } from '../../utils/classNames/index.js';
import { useUid } from '../../utils/useUid/index.js';
import '@floating-ui/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.forwardRef((props, ref) => {
const {
label,
data,
helpText,
prefix,
prefixWidth = prefix ? 24 : 12,
onChange,
disabled,
id,
selectClassName,
labelClassName,
error,
optionResolver = defaultOptionResolver,
defaultValue
} = props;
const uid = useUid(id);
const normalizedData = data.map(optionResolver);
const options = normalizedData.map((item) => /* @__PURE__ */ React.createElement("option", { key: item.value, value: item.value, disabled: item.disabled }, item.label));
const handleChange = (event) => {
onChange?.(event.currentTarget.value);
};
return /* @__PURE__ */ React.createElement("label", { htmlFor: uid, className: classNames({ error }, labelClassName) }, /* @__PURE__ */ React.createElement("p", null, label), /* @__PURE__ */ React.createElement("div", { className: classNames({ "prefixed-input-container": prefix }) }, prefix && /* @__PURE__ */ React.createElement("span", { className: "input-prefix" }, prefix), /* @__PURE__ */ React.createElement(
"select",
{
id: uid,
ref,
className: classNames({ prefixed: prefix }, selectClassName),
disabled,
defaultValue,
style: { paddingLeft: prefixWidth },
onChange: handleChange
},
options
)), /* @__PURE__ */ React.createElement("small", null, error || helpText));
});
Select.displayName = "Select";
export { Select };
//# sourceMappingURL=Select.js.map