-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: user reducer / input component
- Loading branch information
Showing
16 changed files
with
268 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { CounterSchema } from 'entities/Counter'; | ||
import { UserSchema } from 'entities/User'; | ||
|
||
export interface StateSchema { | ||
counter: CounterSchema | ||
counter: CounterSchema; | ||
user: UserSchema; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { userReducer, userActions } from './model/slice/userSlice'; | ||
|
||
export { UserSchema, User } from './model/types/user'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
import { UserSchema } from '../types/user'; | ||
|
||
const initialState: UserSchema = { | ||
}; | ||
|
||
export const userSlice = createSlice({ | ||
name: 'user', | ||
initialState, | ||
reducers: { | ||
|
||
}, | ||
}); | ||
|
||
export const { actions: userActions } = userSlice; | ||
export const { reducer: userReducer } = userSlice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface User { | ||
id: string; | ||
username: string; | ||
} | ||
|
||
export interface UserSchema { | ||
authData?: User; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { LoginModal } from './ui/LoginModal/LoginModal'; |
14 changes: 14 additions & 0 deletions
14
src/features/AuthByUsername/ui/LoginForm/LoginForm.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.LoginForm { | ||
width: 400px; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.input { | ||
margin-top: 10px; | ||
} | ||
|
||
.loginBtn { | ||
margin-top: 15px; | ||
margin-left: auto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Button, ButtonTheme } from 'shared/ui/Button/Button'; | ||
import { Input } from 'shared/ui/Input/Input'; | ||
import cls from './LoginForm.module.scss'; | ||
|
||
interface LoginFormProps { | ||
className?: string; | ||
} | ||
|
||
export const LoginForm = ({ className }: LoginFormProps) => { | ||
const { t } = useTranslation(); | ||
return ( | ||
<div className={classNames(cls.LoginForm, {}, [className])}> | ||
<Input | ||
autofocus | ||
type="text" | ||
className={cls.input} | ||
placeholder={t('Введите username')} | ||
/> | ||
<Input | ||
type="text" | ||
className={cls.input} | ||
placeholder={t('Введите пароль')} | ||
/> | ||
|
||
<Button theme={ButtonTheme.OUTLINE} className={cls.loginBtn}>{t('login')}</Button> | ||
</div> | ||
); | ||
}; |
2 changes: 2 additions & 0 deletions
2
src/features/AuthByUsername/ui/LoginModal/LoginModal.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.LoginModal { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { Modal } from 'shared/ui/Modal/Modal'; | ||
import { LoginForm } from 'features/AuthByUsername/ui/LoginForm/LoginForm'; | ||
import cls from './LoginModal.module.scss'; | ||
|
||
interface LoginModalProps { | ||
className?: string; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
} | ||
|
||
export const LoginModal = ({ className, isOpen, onClose }: LoginModalProps) => ( | ||
<Modal lazy onClose={onClose} isOpen={isOpen} className={classNames(cls.LoginModal, {}, [className])}> | ||
<LoginForm /> | ||
</Modal> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
.InputWrapper { | ||
display: flex; | ||
} | ||
|
||
.placeholder { | ||
margin-right: 5px; | ||
} | ||
|
||
.input { | ||
background: transparent; | ||
border: none; | ||
outline: none; | ||
width: 100%; | ||
color: transparent; | ||
text-shadow: 0 0 0 var(--primary-color); | ||
|
||
&:focus { | ||
outline: none; | ||
} | ||
} | ||
|
||
.caretWrapper { | ||
flex-grow: 1; | ||
position: relative; | ||
} | ||
|
||
.caret { | ||
height: 3px; | ||
width: 9px; | ||
background: var(--primary-color); | ||
bottom: 0; | ||
left: 0; | ||
position: absolute; | ||
animation: blink 0.7s forwards infinite; | ||
} | ||
|
||
@keyframes blink { | ||
0% { | ||
opacity: 0; | ||
} | ||
|
||
50% { | ||
opacity: 0.01; | ||
} | ||
|
||
100% { | ||
opacity: 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { classNames } from 'shared/lib/classNames/classNames'; | ||
import { | ||
ChangeEvent, | ||
InputHTMLAttributes, | ||
memo, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import cls from './Input.module.scss'; | ||
|
||
type HTMLInputProps = Omit< | ||
InputHTMLAttributes<HTMLInputElement>, | ||
'value' | 'onChange' | ||
>; | ||
|
||
interface InputProps extends HTMLInputProps { | ||
className?: string; | ||
value?: string; | ||
onChange?: (value: string) => void; | ||
autofocus?: boolean; | ||
|
||
} | ||
|
||
export const Input = memo((props: InputProps) => { | ||
const { | ||
className, | ||
value, | ||
onChange, | ||
type = 'text', | ||
placeholder, | ||
autofocus, | ||
|
||
...otherProps | ||
} = props; | ||
const ref = useRef<HTMLInputElement>(null); | ||
const [isFocused, setIsFocused] = useState(false); | ||
const [caretPosition, setCaretPosition] = useState(0); | ||
|
||
const onChangeHandler = (e: ChangeEvent<HTMLInputElement>) => { | ||
onChange?.(e.target.value); | ||
}; | ||
|
||
const onBlur = () => { | ||
setIsFocused(false); | ||
}; | ||
|
||
const onFocus = () => { | ||
setIsFocused(true); | ||
}; | ||
|
||
const onSelect = (e: any) => { | ||
setCaretPosition(e?.target?.selectionStart || 0); | ||
}; | ||
|
||
useEffect(() => { | ||
if (autofocus) { | ||
setIsFocused(true); | ||
ref.current?.focus(); | ||
} | ||
}, [autofocus]); | ||
|
||
return ( | ||
<div className={classNames(cls.InputWrapper, {}, [className])}> | ||
{placeholder && ( | ||
<div className={cls.placeholder}> | ||
{`${placeholder}>`} | ||
</div> | ||
)} | ||
|
||
<div className={cls.caretWrapper}> | ||
<input | ||
ref={ref} | ||
type={type} | ||
value={value} | ||
onChange={onChangeHandler} | ||
className={cls.input} | ||
onBlur={onBlur} | ||
onSelect={onSelect} | ||
onFocus={onFocus} | ||
{...otherProps} | ||
/> | ||
{isFocused && ( | ||
<span | ||
className={cls.caret} | ||
style={{ left: `${caretPosition * 9}px` }} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters