Автофокус, выключение кнопок, валидация, переход по enter
This commit is contained in:
@ -22,15 +22,17 @@
|
|||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="GamePage d-none">
|
<div class="row justify-content-center GamePage d-none">
|
||||||
<div class="input-group example">
|
<form class="col GamePage__container" id="form-example">
|
||||||
<input class="form-control" id="first-input" type="number" pattern="\d*" />
|
<div class="input-group example">
|
||||||
<span class="input-group-text" id="sign-span">+</span>
|
<input class="form-control" id="first-input" type="number" pattern="\d*" />
|
||||||
<input class="form-control" id="second-input" type="number" pattern="\d*" />
|
<span class="input-group-text" id="sign-span">+</span>
|
||||||
<span class="input-group-text">=</span>
|
<input class="form-control" id="second-input" type="number" pattern="\d*" />
|
||||||
<input class="form-control" id="sum-input" type="number" pattern="\d*" />
|
<span class="input-group-text">=</span>
|
||||||
</div>
|
<input class="form-control" id="sum-input" type="number" pattern="\d*" />
|
||||||
<button class="btn btn-warning" id="check-button" type="button">Проверить</button>
|
</div>
|
||||||
|
<button class="btn btn-warning" id="check-button" type="submit">Проверить</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="ResultPage d-none">
|
<div class="ResultPage d-none">
|
||||||
|
|||||||
@ -12,6 +12,7 @@ export const CHECK_BUTTON_ID = 'check-button';
|
|||||||
export const REPEAT_BUTTON_ID = 'repeat-button';
|
export const REPEAT_BUTTON_ID = 'repeat-button';
|
||||||
|
|
||||||
export const START_FORM_ID = 'start-form';
|
export const START_FORM_ID = 'start-form';
|
||||||
|
export const EXAMPLE_FORM_ID = 'form-example';
|
||||||
|
|
||||||
export const FIRST_INPUT_ID = 'first-input';
|
export const FIRST_INPUT_ID = 'first-input';
|
||||||
export const SECOND_INPUT_ID = 'second-input';
|
export const SECOND_INPUT_ID = 'second-input';
|
||||||
|
|||||||
@ -21,6 +21,7 @@ import {
|
|||||||
MAX_COUNT_EXAMPLES,
|
MAX_COUNT_EXAMPLES,
|
||||||
EVENTS,
|
EVENTS,
|
||||||
START_FORM_ID,
|
START_FORM_ID,
|
||||||
|
EXAMPLE_FORM_ID,
|
||||||
} from './consts';
|
} from './consts';
|
||||||
import './style.css';
|
import './style.css';
|
||||||
|
|
||||||
@ -45,6 +46,7 @@ const repeatButton = document.querySelector(`#${REPEAT_BUTTON_ID}`);
|
|||||||
const resultContainer = document.querySelector(`#${RESULT_CONTAINER_ID}`);
|
const resultContainer = document.querySelector(`#${RESULT_CONTAINER_ID}`);
|
||||||
const signSpan = document.querySelector(`#${SIGN_SPAN_ID}`);
|
const signSpan = document.querySelector(`#${SIGN_SPAN_ID}`);
|
||||||
const startForm = document.querySelector(`#${START_FORM_ID}`);
|
const startForm = document.querySelector(`#${START_FORM_ID}`);
|
||||||
|
const exampleForm = document.querySelector(`#${EXAMPLE_FORM_ID}`);
|
||||||
|
|
||||||
const getRandomNumber = maxNumber => Math.round(Math.random() * maxNumber);
|
const getRandomNumber = maxNumber => Math.round(Math.random() * maxNumber);
|
||||||
|
|
||||||
@ -95,6 +97,9 @@ const removeRandom = ({first, second, sum, type}) => {
|
|||||||
const setInput = (input, value) => {
|
const setInput = (input, value) => {
|
||||||
input.value = value;
|
input.value = value;
|
||||||
input.disabled = value !== '';
|
input.disabled = value !== '';
|
||||||
|
if (value === '') {
|
||||||
|
input.focus();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const setTextContainer = (container, value) => {
|
const setTextContainer = (container, value) => {
|
||||||
@ -121,6 +126,7 @@ const setExample = () => {
|
|||||||
const {first, second, sum, type} = getExample();
|
const {first, second, sum, type} = getExample();
|
||||||
store.currentExample = {first, second, sum, type};
|
store.currentExample = {first, second, sum, type};
|
||||||
setInputs(removeRandom({first, second, sum, type}));
|
setInputs(removeRandom({first, second, sum, type}));
|
||||||
|
checkButton.disabled = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const isEqual = (origin, answer) => {
|
const isEqual = (origin, answer) => {
|
||||||
@ -170,6 +176,10 @@ const renderPage = () => {
|
|||||||
page.classList.add(NO_DISPLAY_CLASS);
|
page.classList.add(NO_DISPLAY_CLASS);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (store.page === START_PAGE) {
|
||||||
|
nameInput.focus();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const startGame = () => {
|
const startGame = () => {
|
||||||
@ -184,9 +194,22 @@ const startGame = () => {
|
|||||||
};
|
};
|
||||||
renderPage();
|
renderPage();
|
||||||
setExample();
|
setExample();
|
||||||
|
checkButton.disabled = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isNotEmptyInputs = ({first, second, sum}) => {
|
||||||
|
return first !== '' && second !== '' && sum !== '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const checkInput = () => {
|
||||||
|
checkButton.disabled = !isNotEmptyInputs(getInputs());
|
||||||
|
};
|
||||||
|
|
||||||
|
firstInput.addEventListener(EVENTS.INPUT, checkInput);
|
||||||
|
secondInput.addEventListener(EVENTS.INPUT, checkInput);
|
||||||
|
sumInput.addEventListener(EVENTS.INPUT, checkInput);
|
||||||
|
|
||||||
nameInput.addEventListener(EVENTS.INPUT, () => {
|
nameInput.addEventListener(EVENTS.INPUT, () => {
|
||||||
startButton.disabled = !nameInput.value;
|
startButton.disabled = !nameInput.value;
|
||||||
});
|
});
|
||||||
@ -196,17 +219,20 @@ startForm.addEventListener(EVENTS.SUBMIT, event => {
|
|||||||
startGame();
|
startGame();
|
||||||
});
|
});
|
||||||
|
|
||||||
checkButton.addEventListener(EVENTS.CLICK, () => {
|
exampleForm.addEventListener(EVENTS.SUBMIT, event => {
|
||||||
|
event.preventDefault();
|
||||||
const origin = store.currentExample;
|
const origin = store.currentExample;
|
||||||
const answer = getInputs();
|
const answer = getInputs();
|
||||||
store.results.push({origin, answer});
|
if (isNotEmptyInputs(answer)) {
|
||||||
store.answerCount += 1;
|
store.results.push({origin, answer});
|
||||||
if (store.answerCount < MAX_COUNT_EXAMPLES) {
|
store.answerCount += 1;
|
||||||
setExample();
|
if (store.answerCount < MAX_COUNT_EXAMPLES) {
|
||||||
} else {
|
setExample();
|
||||||
store.page = RESULT_PAGE;
|
} else {
|
||||||
renderResult();
|
store.page = RESULT_PAGE;
|
||||||
renderPage();
|
renderResult();
|
||||||
|
renderPage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,18 @@
|
|||||||
.input-group.example {
|
.input-group.example {
|
||||||
width: 240px;
|
width: 240px;
|
||||||
|
margin-bottom: 80px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.StartPage__container {
|
.StartPage__container {
|
||||||
max-width: 350px;
|
max-width: 350px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.GamePage__container {
|
||||||
|
max-width: 350px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input::-webkit-outer-spin-button,
|
||||||
|
input::-webkit-inner-spin-button {
|
||||||
|
-webkit-appearance: none;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user