const form = document.querySelector('form');
for (const element of form.elements) {
const wrapper = element.closest('.form_block');
if (wrapper !== null) {
element.addEventListener('blur', (event) => {
if (!wrapper.contains(event.relatedTarget)) {
wrapper.classList.add('dd');
}
});
}
}
tabindex="0"
убрать можно. position: absolute
), скрываете (opacity: 0; visibility: hidden;
). При (:focus
, :focus-within
и :hover
) показываете (opacity: 1; visibility: visible;
). .blur
, если пользователь сфокусирован). const obj = Object.freeze({
name: 'John',
age: 31
});
obj.age = 32;
obj.email = 'john@example.com';
console.log(obj); // { name: 'John', age: 31 }
const obj = {
name: 'John',
age: 31
};
Object.defineProperty(obj, 'other', {
value: 'some data',
writable: true,
enumerable: true
});
Object.defineProperty(obj, 'email', {
value: 'john@example.com',
enumerable: true
});
Object.defineProperty(obj, 'secret', {
value: 'Woop-woop'
});
console.log(obj);
/* {
name: 'John',
age: 31,
other: 'some data',
email: 'john@example.com'
} */
obj.other = 'another data';
console.log(obj);
/* {
name: 'John',
age: 31,
other: 'another data',
email: 'john@example.com'
} */
console.log(obj.secret); // 'Woop-woop'
obj.secret = 'Boop-boop';
console.log(obj.secret); // 'Woop-woop'
Object.defineProperties(obj, {
other: {
value: 'some data',
writable: true,
enumerable: true
},
email: {
value: 'john@example.com',
enumerable: true
},
secret: {
value: 'Woop-woop'
}
});
const merge = (a, b) => {
const combined = new Set([...Object.values(a), ...Object.values(b)]);
return Object.fromEntries(
[...combined.values()].map((value, index) => [index, value])
);
};
merge(
{ "0": 1, "1": 2, "2": 3 },
{ "0": 4, "1": 5, "2": 1 }
);
const general = {
style: ['palace', 'flat', 'house', 'bungalow', 'hotel'],
register: ['12:00', '13:00', '14:00'],
check: ['12:00', '13:00', '14:00'],
};
- function random(style, register , check) {
+ function random({ style, register, check }) {
- console.log(random(style, register , check));
+ console.log(random(general));
const pick = array => array[Math.floor(Math.random() * array.length)];
pick(['Apple', 'Orange']); // 'Apple' или 'Orange'
function random({ style, register , check }) {
return pick(style) + ', ' + pick(register) + ', ' + pick(check);
};
// Или так:
// const random = ({ style, register , check }) => `${pick(style)}, ${pick(register)}, ${pick(check)}`;
import React, { useCallback } from 'react';
import { Formik, Form, useField } from 'formik';
import * as yup from 'yup';
const initialValues = {
firstName: '',
lastName: '',
nickName: '',
email: '',
password: '',
confirmPassword: ''
};
const validationSchema = yup.object().shape({
firstName: yup.string().required('Имя является обязательным'),
lastName: yup.string().required('Фамилия является обязательной'),
nickName: yup.string().required('Имя пользователя является обязательным'),
email: yup
.string()
.email('Введенное значение не является почтой')
.required('Почта является обязательной'),
password: yup
.string()
.min(8, 'Минимальная длина пароля 8 символов')
.required('Пароль является обязательным'),
confirmPassword: yup
.string()
.oneOf([yup.ref('password'), null], 'Пароли не совпадают')
.min(8, 'Минимальная длина пароля 8 символов')
.required('Пароль является обязательным')
});
const ExampleField = ({ name, label, ...props }) => {
const [field, meta, helpers] = useField(name);
return (
<label>
{label}
<input
name={name}
value={field.value}
onChange={field.onChange}
onBlur={field.onBlur}
{...props}
/>
{meta.error !== undefined && meta.touched && <div>{meta.error}</div>}
</label>
);
};
const ExampleForm = () => {
const handleSubmit = useCallback((values, helpers) => {
console.log(values);
helpers.setSubmitting(false);
}, []);
return (
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
validationOnBlur
onSubmit={handleSubmit}
>
{({ values, errors, touched, isValid, isSubmitting }) => (
<Form>
<ExampleField label="Имя" name="firstName" />
<ExampleField label="Фамилия" name="lastName" />
<ExampleField label="Имя пользователя" name="nickName" />
<ExampleField label="Почта" type="email" name="email" />
<ExampleField label="Пароль" type="password" name="password" />
<ExampleField label="Подтверждение пароля" type="password" name="confirmPassword" />
<button type="submit" disabled={!isValid || isSubmitting}>
Отправить
</button>
<pre>{JSON.stringify({ values, errors, touched }, null, 2)}</pre>
</Form>
)}
</Formik>
);
};
const openInNewTab = (base64Image) => {
const fixedBase64Image = base64Image.startsWith('data:image')
? base64Image
: `data:image/png;base64,${base64Image}`;
const image = new Image();
image.addEventListener('load', () => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
[canvas.width, canvas.height] = [image.width, image.height];
context.drawImage(image, 0, 0);
canvas.toBlob(blob => {
const link = document.createElement('a');
const url = URL.createObjectURL(blob);
[link.href, link.target] = [url, '_blank'];
link.addEventListener('click', () => {
setTimeout(() => {
URL.revokeObjectURL(url);
});
});
link.click();
});
});
image.src = fixedBase64Image;
};
openInNewTab('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==');
const store = new Proxy({
a: 50
}, {
set(target, property, value, receiver) {
console.log(`Поле <${property}> было обновлено. Новое значение:`, value);
return Reflect.set(target, property, value, receiver);
}
});
store.a = 10;
// Поле <a> было обновлено. Новое значение: 10