diff --git a/src/s2-homeworks/hw05/pages/JuniorPlus.tsx b/src/s2-homeworks/hw05/pages/JuniorPlus.tsx index c29f15d..12e626c 100644 --- a/src/s2-homeworks/hw05/pages/JuniorPlus.tsx +++ b/src/s2-homeworks/hw05/pages/JuniorPlus.tsx @@ -1,12 +1,13 @@ import React from 'react' import HW10 from '../../hw10/HW10' +import HW11 from '../../hw11/HW11' function JuniorPlus() { return (
junior plus page - {/**/} + {/**/} {/**/}
diff --git a/src/s2-homeworks/hw11/HW11.tsx b/src/s2-homeworks/hw11/HW11.tsx new file mode 100644 index 0000000..7102c95 --- /dev/null +++ b/src/s2-homeworks/hw11/HW11.tsx @@ -0,0 +1,48 @@ +import React, {useState} from 'react' +import SuperRange from './common/c7-SuperRange/SuperRange' +import SuperDoubleRange from './common/c8-SuperDoubleRange/SuperDoubleRange' +import s2 from '../../s1-main/App.module.css' +import {restoreState} from '../hw06/localStorage/localStorage' + +function HW11() { + const [value1, setValue1] = useState(restoreState('hw11-value1', 0)) + const [value2, setValue2] = useState(restoreState('hw11-value2', 100)) + + const change = (values: [number, number]) => { + value1 !== values[0] && setValue1(values[0]) + value2 !== values[1] && setValue2(values[1]) + } + + return ( +
+
+ {/*можно убрать этот тег*/} + + {/*should work (должно работать)*/} +
+ {value1} + +
+ +
+ {value1} + + {value2} +
+ +
+ {/*можно убрать этот тег*/} +
+ {/*можно убрать этот тег*/} +
+ ) +} + +export default HW11 diff --git a/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.module.css b/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.module.css new file mode 100644 index 0000000..cbd0093 --- /dev/null +++ b/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.module.css @@ -0,0 +1,21 @@ +.range { + appearance: none; + width: 160px; + + outline: none; + border-radius: 3px; + background: #99ff99; +} + +.range::-webkit-slider-thumb { + appearance: none; + position: relative; + width: 16px; + height: 16px; + top: 3px; + margin-top: -6px; + + background: red; + cursor: pointer; + z-index: 2; +} \ No newline at end of file diff --git a/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.tsx b/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.tsx new file mode 100644 index 0000000..2d361cc --- /dev/null +++ b/src/s2-homeworks/hw11/common/c7-SuperRange/SuperRange.tsx @@ -0,0 +1,45 @@ +import React, {ChangeEvent, DetailedHTMLProps, InputHTMLAttributes} from 'react' +import s from './SuperRange.module.css' + +// тип пропсов обычного инпута +type DefaultInputPropsType = DetailedHTMLProps, HTMLInputElement> + +// здесь мы говорим что у нашего инпута будут такие же пропсы как у обычного инпута +// (чтоб не писать value: string, onChange: ...; они уже все описаны в DefaultInputPropsType) +type SuperRangePropsType = DefaultInputPropsType & { // и + ещё пропсы которых нет в стандартном инпуте + onChangeRange?: (value: number) => void +}; + +const SuperRange: React.FC = ( + { + type, // достаём и игнорируем чтоб нельзя было задать другой тип инпута + onChange, onChangeRange, + className, + + ...restProps// все остальные пропсы попадут в объект restProps + } +) => { + const onChangeCallback = (e: ChangeEvent) => { + onChange && onChange(e) // сохраняем старую функциональность + + onChangeRange && onChangeRange(+e.currentTarget.value) + } + + const finalRangeClassName = `${s.range} ${className ? className : ''}` + + // взять одинарный ползунок из материал-юай и повесить на него ид + return ( + <> + + + ) +} + +export default SuperRange diff --git a/src/s2-homeworks/hw11/common/c8-SuperDoubleRange/SuperDoubleRange.tsx b/src/s2-homeworks/hw11/common/c8-SuperDoubleRange/SuperDoubleRange.tsx new file mode 100644 index 0000000..1483517 --- /dev/null +++ b/src/s2-homeworks/hw11/common/c8-SuperDoubleRange/SuperDoubleRange.tsx @@ -0,0 +1,38 @@ +import React, {DetailedHTMLProps, InputHTMLAttributes} from 'react' +import SuperRange from '../c7-SuperRange/SuperRange' + +type DefaultInputPropsType = DetailedHTMLProps, HTMLInputElement> + +type SuperDoubleRangePropsType = Omit & { + onChangeRange?: (value: [number, number]) => void + value?: [number, number] + // min, max, step, disable, ... +} + +const SuperDoubleRange: React.FC = ( + { + onChangeRange, value, + // min, max, step, disable, ... + ...restProps + } +) => { + // сделать самому, можно подключать библиотеки + const [value1, value2] = value || [-1, -1] + + const change1 = (n: number) => { + if (n <= value2) onChangeRange?.([n, value2]) + } + const change2 = (n: number) => { + if (value1 <= n) onChangeRange?.([value1, n]) + } + + // взять двойной ползунок из материал-юай и повесить на него ид + return ( +
+ + +
+ ) +} + +export default SuperDoubleRange