-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from saseungmin/apply-change-input
[Feature] Auth input fields onChange action
- Loading branch information
Showing
5 changed files
with
181 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
|
||
import { useRecoilState } from 'recoil'; | ||
|
||
import authFieldsAtom from '../../recoil/auth'; | ||
|
||
const authFieldsProperty = { | ||
userId: { | ||
inputType: 'text', | ||
placeholder: '아이디', | ||
autoComplete: 'username', | ||
}, | ||
password: { | ||
inputType: 'password', | ||
placeholder: '비밀번호', | ||
autoComplete: 'new-password', | ||
}, | ||
passwordConfirm: { | ||
inputType: 'password', | ||
placeholder: '비밀번호 확인', | ||
autoComplete: 'new-password', | ||
}, | ||
}; | ||
|
||
const AuthInput = ({ formType, inputName }) => { | ||
const [authFieldsState, setAuthFieldsState] = useRecoilState(authFieldsAtom); | ||
|
||
const { inputType, placeholder, autoComplete } = authFieldsProperty[inputName]; | ||
|
||
const onChange = (e, type) => { | ||
const { name, value } = e.target; | ||
|
||
setAuthFieldsState({ | ||
...authFieldsState, | ||
[type]: { | ||
...authFieldsState[type], | ||
[name]: value, | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<input | ||
type={inputType} | ||
name={inputName} | ||
placeholder={placeholder} | ||
autoComplete={autoComplete} | ||
value={authFieldsState[formType][inputName]} | ||
onChange={(e) => onChange(e, formType)} | ||
/> | ||
); | ||
}; | ||
|
||
export default AuthInput; |
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,69 @@ | ||
import React from 'react'; | ||
|
||
import { RecoilRoot } from 'recoil'; | ||
|
||
import { render, fireEvent } from '@testing-library/react'; | ||
|
||
import InjectTestingRecoilState from '../common/InjectTestingRecoilState'; | ||
|
||
import AuthInput from './AuthInput'; | ||
|
||
describe('AuthInput', () => { | ||
const renderAuthInput = ({ type, name, fields }) => render(( | ||
<RecoilRoot> | ||
<InjectTestingRecoilState | ||
authFields={fields} | ||
/> | ||
<AuthInput | ||
formType={type} | ||
inputName={name} | ||
/> | ||
</RecoilRoot> | ||
|
||
)); | ||
|
||
it('renders auth input', () => { | ||
const props = { | ||
type: 'login', | ||
name: 'userId', | ||
fields: { | ||
login: { | ||
userId: 'seungmin', | ||
}, | ||
}, | ||
}; | ||
|
||
const { getByPlaceholderText } = renderAuthInput(props); | ||
|
||
const input = getByPlaceholderText('아이디'); | ||
|
||
expect(input).not.toBeNull(); | ||
expect(input).toHaveValue('seungmin'); | ||
}); | ||
|
||
it('listens event change input value', () => { | ||
const props = { | ||
type: 'login', | ||
name: 'userId', | ||
fields: { | ||
login: { | ||
userId: '', | ||
}, | ||
}, | ||
}; | ||
|
||
const { getByPlaceholderText } = renderAuthInput(props); | ||
|
||
const input = getByPlaceholderText('아이디'); | ||
|
||
fireEvent.change(input, { | ||
target: { | ||
value: 'seungmin', | ||
name: 'userId', | ||
}, | ||
}); | ||
|
||
expect(input).not.toBeNull(); | ||
expect(input).toHaveValue('seungmin'); | ||
}); | ||
}); |
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