kitchen-simulator
Version:
It is a kitchen simulator (self-contained micro-frontend).
183 lines • 7.25 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
var _templateObject;
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { KEYBOARD_BUTTON_CODE, TEXT_COLOR_NEUTRAL_0, SECONDARY_PURPLE_COLOR, DEFAULT_FONT_FAMILY } from "../../constants";
import styled from 'styled-components';
import { isValidNumber } from "../../utils/helper";
var StyledInput = styled.input(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n display: block;\n padding: 15px 25px 12px 0px;\n width: 120px;\n color: ", ";\n background-color: rgb(255, 255, 255);\n border: 2px solid;\n text-align: right;\n float: right;\n font-family: ", ";\n font-size: 16px;\n font-weight: 600;\n line-height: 17px;\n border-radius: 5px;\n outline: 0;\n :focus {\n border-color: ", ";\n }\n"])), TEXT_COLOR_NEUTRAL_0, DEFAULT_FONT_FAMILY, SECONDARY_PURPLE_COLOR);
var FormNumberInput = /*#__PURE__*/function (_Component) {
function FormNumberInput(props, context) {
var _this;
_classCallCheck(this, FormNumberInput);
_this = _callSuper(this, FormNumberInput, [props, context]);
_this.state = {
focus: false,
valid: true,
showedValue: props.value,
focusOn: false,
flag: true
};
_this.input = /*#__PURE__*/React.createRef(null);
return _this;
}
_inherits(FormNumberInput, _Component);
return _createClass(FormNumberInput, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.input.current && ['width', 'length'].includes(this.props.labelName.toLowerCase())) {
this.input.current.focus();
this.input.current.select();
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (this.props.value !== nextProps.value || this.props.focus !== nextProps.focus) {
this.setState({
showedValue: nextProps.value,
focusOn: nextProps.focus
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, preprevState) {
if (this.props.value !== prevProps.value && prevProps.labelName !== 'Ceiling Height') {
this.input.current.focus();
this.input.current.select();
}
var showedValue = this.state.showedValue;
var _this$props = this.props,
min = _this$props.min,
max = _this$props.max,
isCeiling = _this$props.isCeiling;
if (!isCeiling) {
if (isValidNumber(min) && showedValue < min) {
this.setState({
showedValue: min
});
} else if (isValidNumber(max) && showedValue > max) {
this.setState({
showedValue: max
});
}
}
return null;
}
}, {
key: "render",
value: function render() {
var _this2 = this;
var _this$props2 = this.props,
value = _this$props2.value,
min = _this$props2.min,
max = _this$props2.max,
precision = _this$props2.precision,
onChange = _this$props2.onChange,
onValid = _this$props2.onValid,
onInvalid = _this$props2.onInvalid,
style = _this$props2.style,
placeholder = _this$props2.placeholder,
isCeiling = _this$props2.isCeiling;
var regexp = new RegExp("^-?([0-9]+)?\\.?([0-9]{0,".concat(precision, "})?$"));
var currValue = regexp.test(this.state.showedValue) ? this.state.showedValue : parseFloat(this.state.showedValue).toFixed(precision);
var different = parseFloat(this.props.value).toFixed(precision) !== parseFloat(this.state.showedValue).toFixed(precision);
var saveFn = function saveFn(e) {
e.stopPropagation();
if (_this2.state.valid) {
var savedValue = _this2.state.showedValue !== '' && _this2.state.showedValue !== '-' ? parseFloat(_this2.state.showedValue) : 0;
_this2.setState({
showedValue: savedValue
});
onChange({
target: {
value: savedValue
}
});
}
};
return /*#__PURE__*/React.createElement(StyledInput, {
id: "form_number_input",
autoFocus: this.state.focusOn,
readOnly: !!this.props.disabled,
type: "text",
value: currValue,
style: style,
onChange: function onChange(evt) {
_this2.context.projectActions.copyProperties('ddddd');
var valid = regexp.test(evt.nativeEvent.target.value);
if (valid) {
_this2.setState({
showedValue: evt.nativeEvent.target.value,
flag: false
});
if (onValid) onValid(evt.nativeEvent);
} else {
if (onInvalid) onInvalid(evt.nativeEvent);
}
_this2.setState({
valid: valid
});
},
onFocus: function onFocus(e) {
return _this2.setState({
focusOn: true
});
},
onBlur: function onBlur(e) {
return _this2.setState({
focusOn: false
});
},
ref: this.input,
onKeyDown: function onKeyDown(e) {
var keyCode = e.keyCode || e.which;
if (keyCode == KEYBOARD_BUTTON_CODE.ENTER || keyCode == KEYBOARD_BUTTON_CODE.TAB) {
saveFn(e);
_this2.input.current.blur();
}
if (keyCode == KEYBOARD_BUTTON_CODE.ESC) {
_this2.context.projectActions.rollback();
}
},
placeholder: placeholder
});
}
}]);
}(Component);
export { FormNumberInput as default };
FormNumberInput.propTypes = {
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
style: PropTypes.object,
onChange: PropTypes.func.isRequired,
onValid: PropTypes.func,
onInvalid: PropTypes.func,
min: PropTypes.number,
max: PropTypes.number,
precision: PropTypes.number,
placeholder: PropTypes.string,
labelName: PropTypes.string,
isCeiling: PropTypes.string
};
FormNumberInput.contextTypes = {
translator: PropTypes.object.isRequired,
projectActions: PropTypes.object.isRequired,
linesActions: PropTypes.object.isRequired
};
FormNumberInput.defaultProps = {
value: 0,
style: {},
min: Number.MIN_SAFE_INTEGER,
max: Number.MAX_SAFE_INTEGER,
precision: 2,
disabled: false,
labelName: 'default'
};