decentraland-ui
Version:
Decentraland's UI components and styles
173 lines (172 loc) • 9.04 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Field = void 0;
var React = __importStar(require("react"));
var Icon_1 = __importDefault(require("semantic-ui-react/dist/commonjs/elements/Icon/Icon"));
var react_semantic_ui_datepickers_1 = __importDefault(require("react-semantic-ui-datepickers"));
require("react-semantic-ui-datepickers/dist/react-semantic-ui-datepickers.css");
var Input_1 = __importDefault(require("semantic-ui-react/dist/commonjs/elements/Input/Input"));
var classnames_1 = __importDefault(require("classnames"));
var input_1 = require("../../lib/input");
var Blockie_1 = require("../Blockie/Blockie");
var Button_1 = require("../Button/Button");
var Header_1 = require("../Header/Header");
require("./Field.css");
var DATE_TYPE = 'date';
function renderMessage(props) {
var error = props.error, warning = props.warning, info = props.info, message = props.message;
if (error || message) {
return (React.createElement(React.Fragment, null,
error && message ? React.createElement(Icon_1.default, { name: "remove circle" }) : null,
" ",
message));
}
if (warning) {
return (React.createElement(React.Fragment, null,
React.createElement(Icon_1.default, { name: "warning sign" }),
" ",
warning));
}
if (info) {
return (React.createElement(React.Fragment, null,
React.createElement(Icon_1.default, { name: "info circle" }),
" ",
info));
}
}
var Field = /** @class */ (function (_super) {
__extends(Field, _super);
function Field() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.onChangeDatePicker = function (e, data) {
var newValue = '';
if (data.value) {
var year = data.value && data.value.getFullYear();
// Added 0 to dates to match format. e.g. 9-10-2000 -> 09-10-2000
var day = data.value &&
"".concat(data.value.getDate() < 10 ? '0' : '').concat(data.value.getDate());
var month = data.value &&
"".concat(data.value.getMonth() + 1 < 10 ? '0' : '').concat(data.value.getMonth() + 1);
newValue = "".concat(year, "-").concat(month, "-").concat(day);
}
var inputProps = __assign(__assign({}, _this.props), { value: newValue });
_this.props.onChange && _this.props.onChange(e, inputProps);
};
return _this;
}
Field.prototype.hasAction = function () {
var _a = this.props, loading = _a.loading, error = _a.error, action = _a.action;
var hasOnAction = this.props.onAction !== null && this.props.onAction !== undefined;
return !this.isAddress() && !loading && !error && action && hasOnAction;
};
Field.prototype.isAddress = function () {
var type = this.props.type;
return type === 'address';
};
Field.prototype.render = function () {
var _a;
var _b = this.props, value = _b.value, label = _b.label, error = _b.error, warning = _b.warning, message = _b.message, info = _b.info, type = _b.type, loading = _b.loading, action = _b.action, onAction = _b.onAction, disabled = _b.disabled, kind = _b.kind, fitContent = _b.fitContent, _c = _b.isClearable, isClearable = _c === void 0 ? true : _c, id = _b.id, maxLength = _b.maxLength, rest = __rest(_b, ["value", "label", "error", "warning", "message", "info", "type", "loading", "action", "onAction", "disabled", "kind", "fitContent", "isClearable", "id", "maxLength"]);
var isAddress = this.isAddress();
var icon = error && !message && !isAddress ? 'warning circle' : void 0;
var classes = (0, classnames_1.default)('dcl field', kind, (_a = {
error: error,
warning: !error && warning,
info: !error && !warning && info,
disabled: disabled,
address: isAddress,
resizable: fitContent
},
_a['has-label'] = label,
_a));
if (isAddress && action) {
console.warn("The address fields don't support actions, \"".concat(action, "\" will be ignored"));
}
return (React.createElement("div", { className: classes },
label || maxLength !== undefined ? (React.createElement(Header_1.Header, { sub: true },
label ? React.createElement("label", { htmlFor: id }, label) : null,
maxLength !== undefined ? (React.createElement("span", { className: "maxLength" },
(0, input_1.getInputValueLength)(value),
"/",
maxLength)) : null)) : null,
type === DATE_TYPE ? (React.createElement(react_semantic_ui_datepickers_1.default
// Added the time to the date to prevent timezone variations that would change the date
, __assign({
// Added the time to the date to prevent timezone variations that would change the date
value: value ? new Date("".concat(value, " 00:00:00")) : undefined, icon: icon ? icon : void 0, loading: loading && !isAddress, disabled: disabled, showOutsideDays: true, className: "datePickerWidth", clearable: isClearable }, rest, { onChange: this.onChangeDatePicker, id: id }))) : (React.createElement(Input_1.default, __assign({ id: id, value: value, type: isAddress ? 'text' : type, icon: icon, loading: loading && !isAddress, disabled: disabled, maxLength: maxLength }, rest))),
this.hasAction() && (React.createElement("div", { className: "overlay" },
React.createElement(Button_1.Button, { onClick: onAction, disabled: disabled, basic: true }, action))),
this.isAddress() && value ? React.createElement(Blockie_1.Blockie, { seed: value, scale: 4 }) : null,
React.createElement("p", { className: "message" },
renderMessage(this.props),
"\u00A0")));
};
Field.defaultProps = {
kind: 'simple',
fitContent: false
};
return Field;
}(React.PureComponent));
exports.Field = Field;