UNPKG

@onesy/ui-react

Version:
90 lines (88 loc) 5.43 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _utils = require("@onesy/utils"); var _styleReact = require("@onesy/style-react"); var _AutoComplete = _interopRequireDefault(require("../AutoComplete")); var _ListItem = _interopRequireDefault(require("../ListItem")); var _Type = _interopRequireDefault(require("../Type")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["value", "valueDefault", "valueInputDefault", "size", "className", "children"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const useStyle = (0, _styleReact.style)(theme => ({ root: {} }), { name: 'onesy-AutoCompleteCountry' }); const AutoCompleteCountry = props_ => { var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2, _theme$elements3; const theme = (0, _styleReact.useOnesyTheme)(); const l = theme.l; const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyAutoCompleteCountry) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const AutoComplete = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.AutoComplete) || _AutoComplete.default; const ListItem = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.ListItem) || _ListItem.default; const Type = (theme === null || theme === void 0 || (_theme$elements3 = theme.elements) === null || _theme$elements3 === void 0 ? void 0 : _theme$elements3.Type) || _Type.default; const { value, valueDefault, valueInputDefault: valueInputDefault_, size = 'regular', className, children } = props, other = (0, _objectWithoutProperties2.default)(props, _excluded); const { classes } = useStyle(); // { // flag: string; // name: string; // full_name: string; // sovereignty: string; // 'alpha-2': string; // 'alpha-3': string; // numeric: string; // subdivision: string; // tlds: string[]; // } const options = _react.default.useMemo(() => { return _utils.countries.map(item => _objectSpread(_objectSpread({}, item), {}, { name: l(item.name), value: item['alpha-2'] })); }, [_utils.countries]); const valueInputDefault = _react.default.useMemo(() => { var _options$find; return valueInputDefault_ !== undefined ? valueInputDefault_ : (_options$find = options.find(item_0 => item_0.value === (value !== undefined ? value : valueDefault))) === null || _options$find === void 0 ? void 0 : _options$find.name; }, []); return /*#__PURE__*/(0, _jsxRuntime.jsx)(AutoComplete, _objectSpread({ valueInputDefault: valueInputDefault, valueDefault: valueDefault, value: value, options: options, size: size, renderOption: (item_1, index_, propsItem) => /*#__PURE__*/(0, _jsxRuntime.jsx)(ListItem, _objectSpread({ start: item_1.flag, startAlign: "center", primary: /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, { version: "b3", children: item_1.name }), value: item_1.value, selected: item_1.value === ((value === null || value === void 0 ? void 0 : value.value) !== undefined ? value === null || value === void 0 ? void 0 : value.value : value), size: "small", button: true }, propsItem), index_), className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('AutoCompleteCountry', theme) && ['onesy-AutoCompleteCountry-root', `onesy-AutoCompleteCountry-size-${size}`], className, classes.root]) }, other)); }; AutoCompleteCountry.displayName = 'onesy-AutoCompleteCountry'; var _default = exports.default = AutoCompleteCountry;