팝업 파일은 사용자가 툴바 아이콘을 눌렀을 때 나오는 창에 표시되는 HTML 파일입니다. 스타일 시트를 링크하는 태그와 스크립트 태그를 포함하는 면에서 웹 페이지와 비슷하게 동작합니다. 하지만 웹 페이지와 달리 인라인 자바스크립트는 허용하지 않습니다.
input
박스에 팔로우 하고싶은 사용자의 이름을 적어서 추가하면 됩니다.class Content extends React.Component<ContentProps, ContentState> {
constructor(props: ContentProps) {
super(props);
this.state = {
isToggleOn: true,
id: this.props.storage.getId(),
userName: '',
followers: this.props.storage.list(),
};
}
handleSubmit(e: React.FormEvent<HTMLFormElement>): void {
const {id, userName, followers} = this.state;
e.preventDefault();
if (userName === '') {
return;
}
for (const follower of followers) {
if (follower.userName === userName) {
this.setState({
userName: '',
});
return;
}
}
this.props.storage.add({
id,
userName,
});
this.props.storage.plusId();
this.setState({
id: this.props.storage.getId(),
userName: '',
followers: this.props.storage.list(),
});
}
handleChange(e: React.ChangeEvent<HTMLInputElement>): void {
this.setState({
userName: e.target.value,
});
}
render(): React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
> {
const {isToggleOn, userName, followers} = this.state;
// CSS 코드
return (
<div className="wrapper">
<div className="tab">
// tab 화면 코드
</div>
<div className="content">
<form
className="add-user"
style={isToggleOn === true ? appear : disappear}
onSubmit={(e): void => this.handleSubmit(e)}
>
<input
type="text"
placeholder="팔로잉할 사용자를 추가해주세요"
className="add-user__input"
value={userName}
onChange={(e): void => this.handleChange(e)}
/>
<button className="add-user__button" type="submit">
사용자 추가
</button>
</form>
// 삭제하는 화면 코드
<div className="count-users">
팔로우한 사용자 수:<span>{followers?.length}</span>
</div>
</div>
);
}
}
handleChange
함수는 input
박스에 있는 텍스트 내용을 감지합니다.handleSubmit
함수가 실행됩니다.class Content extends React.Component<ContentProps, ContentState> {
constructor(props: ContentProps) {
super(props);
this.state = {
isToggleOn: true,
id: this.props.storage.getId(),
userName: '',
followers: this.props.storage.list(),
};
}
handleRemove(name: string): void {
this.props.storage.remove(name);
this.setState((state) => ({
followers: state.followers.filter((candidate) => {
return candidate.userName !== name;
}),
}));
}
render(): React.DetailedHTMLProps<
React.HTMLAttributes<HTMLDivElement>,
HTMLDivElement
> {
const {isToggleOn, userName, followers} = this.state;
// CSS 코드
return (
<div className="wrapper">
<div className="tab">
// tab 화면 코드
</div>
<div className="content">
// 추가하는 화면 코드
<div
className="delete-user"
style={isToggleOn === false ? appear : disappear}
>
<ul
style={
followers.length === 0
? {overflowY: 'visible'}
: {overflowY: 'scroll'}
}
>
{followers.length === 0 ? (
<li className="delete-user__no-user">
<span>🥺</span>추가한 사용자가 없습니다.
</li>
) : (
followers.map((follower) => (
<li key={follower.id.toString()}>
{follower.userName}
<button
type="button"
onClick={(): void => this.handleRemove(follower.userName)}
>
삭제
</button>
</li>
))
)}
</ul>
</div>
</div>
<div className="count-users">
팔로우한 사용자 수:<span>{followers?.length}</span>
</div>
</div>
);
}
}
handleRemove
함수가 실행됩니다.handleRemove
실행 과정