@gechiui/components
Version:
UI components for GeChiUI.
135 lines (122 loc) • 3.43 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import { createElement } from "@gechiui/element";
/**
* External dependencies
*/
import { isEmpty, noop } from 'lodash';
import classNames from 'classnames'; // eslint-disable-next-line no-restricted-imports
/**
* GeChiUI dependencies
*/
import { useInstanceId } from '@gechiui/compose';
import { useState, forwardRef } from '@gechiui/element';
import { Icon, chevronDown } from '@gechiui/icons';
/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import InputBase from '../input-control/input-base';
import { Select, DownArrowWrapper } from './styles/select-control-styles';
function useUniqueId(idProp) {
const instanceId = useInstanceId(SelectControl);
const id = `inspector-select-control-${instanceId}`;
return idProp || id;
}
function SelectControl(_ref, ref) {
let {
className,
disabled = false,
help,
hideLabelFromVision,
id: idProp,
label,
multiple = false,
onBlur = noop,
onChange = noop,
onFocus = noop,
options = [],
size = 'default',
value: valueProp,
labelPosition = 'top',
children,
prefix,
suffix,
...props
} = _ref;
const [isFocused, setIsFocused] = useState(false);
const id = useUniqueId(idProp);
const helpId = help ? `${id}__help` : undefined; // Disable reason: A select with an onchange throws a warning
if (isEmpty(options) && !children) return null;
const handleOnBlur = event => {
onBlur(event);
setIsFocused(false);
};
const handleOnFocus = event => {
onFocus(event);
setIsFocused(true);
};
const handleOnChange = event => {
if (multiple) {
const selectedOptions = Array.from(event.target.options).filter(_ref2 => {
let {
selected
} = _ref2;
return selected;
});
const newValues = selectedOptions.map(_ref3 => {
let {
value
} = _ref3;
return value;
});
onChange(newValues);
return;
}
onChange(event.target.value, {
event
});
};
const classes = classNames('components-select-control', className);
/* eslint-disable jsx-a11y/no-onchange */
return createElement(BaseControl, {
help: help,
id: id
}, createElement(InputBase, {
className: classes,
disabled: disabled,
hideLabelFromVision: hideLabelFromVision,
id: id,
isFocused: isFocused,
label: label,
size: size,
suffix: suffix || createElement(DownArrowWrapper, null, createElement(Icon, {
icon: chevronDown,
size: 18
})),
prefix: prefix,
labelPosition: labelPosition
}, createElement(Select, _extends({}, props, {
"aria-describedby": helpId,
className: "components-select-control__input",
disabled: disabled,
id: id,
multiple: multiple,
onBlur: handleOnBlur,
onChange: handleOnChange,
onFocus: handleOnFocus,
ref: ref,
selectSize: size,
value: valueProp
}), children || options.map((option, index) => {
const key = option.id || `${option.label}-${option.value}-${index}`;
return createElement("option", {
key: key,
value: option.value,
disabled: option.disabled
}, option.label);
}))));
/* eslint-enable jsx-a11y/no-onchange */
}
const ForwardedComponent = forwardRef(SelectControl);
export default ForwardedComponent;
//# sourceMappingURL=index.js.map