Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
forest-lynx committed Jun 17, 2024
2 parents 15ae397 + 267269a commit d12b815
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ Text::make('Price')
При активном калькуляторе осуществляется отслеживание нажатий клавиш, доступны следующие значения - `[0-9]`, `+`, `-`, `*`, `/`, `=`, `%`, `^`, `.`, `,` , `(`, `)`, `Backspace`, `Enter`, `Escape`.
Описание некоторых значений:
`%` - вычисление процента,
`^` - возведение в степень,
`Backspace` - удаление последнего символа,
`Enter` - вычисление,
`Escape` - очистка поля.
- `%` - вычисление процента,
- `^` - возведение в степень,
- `Backspace` - удаление последнего символа,
- `Enter` - вычисление,
- `Escape` - очистка поля.

> Имеется поддержка поля с типом `number`, это значит, что учитывается минимальное(min), максимальное(max) значение, а так же шаг(step) поля.
## Лицензия
[Лицензия MIT](LICENSE).
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 32 additions & 7 deletions resources/js/flCalculator.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
export default (el) => ({
el: el,
input: null,
input: {
el: null,
isNumber: false,
min: null,
max: null,
step: null,
},
calculatorShow: false,
formula: "",
displayField: null,
allowedKeys: "0123456789+-*/(),.%^",
operators: "+-*/%^",

//TODO добавить логику при работе с полем number min max step
//TODO обработка локали для корректного вывода чисел
init() {
this.input = el.querySelector("input");
this.input.el = el.querySelector("input");
this.input.isNumber = this.input.el.type === "number";
if (this.input.isNumber) {
this.input.min = this.input.el.min;
this.input.max = this.input.el.max;
this.input.step = this.input.el.step;
}
this.displayField = el.querySelector(".calculator input.formula");
this.el.addEventListener("keydown", this.handleKeyPress.bind(this));
this.calculatorShow = false;
Expand All @@ -20,14 +31,14 @@ export default (el) => ({
this.calculatorShow = !this.calculatorShow;

if (this.calculatorShow) {
this.formula = this.input.value
? this.input.value.replace(/\s/g, "")
this.formula = this.input.el.value
? this.input.el.value.replace(/\s/g, "")
: this.formula;
this.setDisplayFormula();
} else {
this.input.value = this.calculate(this.formula) ?? 0;
this.input.el.value = this.formatValue(this.calculate(this.formula) ?? 0);
this.formula = "";
setTimeout(() => this.input.focus(), 10);
setTimeout(() => this.input.el.focus(), 10);
}
},
keyPress(v) {
Expand All @@ -53,6 +64,20 @@ export default (el) => ({
this.displayField.value = this.formula;
},

formatValue(value) {
if (this.input.isNumber) {
const fractionDigits = (s) => s.toString().split(".")[1].length || 0;
return Math.min(
Math.max(
value.toFixed(fractionDigits(this.input.step)),
this.input.min
),
this.input.max
);
}

return value;
},
setFormula(v) {
if (
(this.formula === "" || this.formula === 0) &&
Expand Down

0 comments on commit d12b815

Please sign in to comment.