Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
components-demo
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nikolay Tarassenko
components-demo
Commits
55b14891
Commit
55b14891
authored
May 26, 2023
by
Nikolay Tarassenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
classwork_54
parent
c07e076b
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
171 additions
and
45 deletions
+171
-45
Counter.css
src/components/Counter/Counter.css
+12
-0
Counter.tsx
src/components/Counter/Counter.tsx
+23
-0
People.tsx
src/components/People/People.tsx
+46
-0
Person.tsx
src/components/People/Person/Person.tsx
+1
-1
ToggleButton.tsx
src/components/ToggleButton/ToggleButton.tsx
+29
-0
App.css
src/containers/App.css
+28
-0
App.tsx
src/containers/App.tsx
+25
-37
main.tsx
src/main.tsx
+7
-7
No files found.
src/components/Counter/Counter.css
0 → 100644
View file @
55b14891
.Counter-text
{
font-size
:
2rem
;
font-weight
:
900
;
}
.text-green
{
color
:
green
;
}
.text-red
{
color
:
red
;
}
src/components/Counter/Counter.tsx
0 → 100644
View file @
55b14891
import
"./Counter.css"
;
interface
Props
{
value
:
number
;
}
const
Counter
=
({
value
}:
Props
):
JSX
.
Element
=>
{
const
counterClasses
=
[
"Counter-text"
];
if
(
value
<
2
)
{
counterClasses
.
push
(
"text-red"
);
}
else
{
counterClasses
.
push
(
"text-green"
);
}
counterClasses
;
return
(
<
div
>
<
p
className=
{
counterClasses
.
join
(
" "
)
}
>
people count:
{
value
}
</
p
>
</
div
>
);
};
export
default
Counter
;
src/components/People/People.tsx
0 → 100644
View file @
55b14891
import
type
{
ChangeEvent
}
from
"react"
;
import
Person
from
"./Person/Person"
;
import
IPerson
from
"../../interfaces/IPerson"
;
type
People
=
(
IPerson
&
{
hobby
:
string
})[];
interface
Props
{
people
:
People
;
onClickHeader
(
id
:
string
):
void
;
onChangeName
(
e
:
ChangeEvent
<
HTMLInputElement
>
,
id
:
string
):
void
;
onRemove
(
id
:
string
):
void
;
}
const
People
=
({
people
,
onClickHeader
,
onChangeName
,
onRemove
,
}:
Props
):
JSX
.
Element
=>
{
return
(
<
div
>
{
people
.
map
((
person
)
=>
{
return
(
<
Person
key=
{
person
.
id
}
name=
{
person
.
name
}
age=
{
person
.
age
}
onClickHeader=
{
()
=>
{
onClickHeader
(
person
.
id
);
}
}
onChangeHandler=
{
(
e
)
=>
{
onChangeName
(
e
,
person
.
id
);
}
}
onRemoveHandler=
{
()
=>
{
onRemove
(
person
.
id
);
}
}
>
hobby:
{
person
.
hobby
}
</
Person
>
);
})
}
</
div
>
);
};
export
default
People
;
src/Person/Person.tsx
→
src/
components/People/
Person/Person.tsx
View file @
55b14891
import
type
{
ReactNode
,
MouseEventHandler
,
ChangeEventHandler
}
from
"react"
;
import
type
{
ReactNode
,
MouseEventHandler
,
ChangeEventHandler
}
from
"react"
;
import
type
IPerson
from
"../interfaces/IPerson"
;
import
type
IPerson
from
"../
../../
interfaces/IPerson"
;
type
Props
=
Omit
<
IPerson
,
"id"
>
&
{
type
Props
=
Omit
<
IPerson
,
"id"
>
&
{
children
?:
ReactNode
;
children
?:
ReactNode
;
...
...
src/components/ToggleButton/ToggleButton.tsx
0 → 100644
View file @
55b14891
import
{
MouseEventHandler
,
ReactNode
}
from
"react"
;
const
ToggleButton
=
({
isToggle
,
onClick
,
hideText
=
"Hide"
,
showText
=
"Show"
,
}:
{
isToggle
:
boolean
;
onClick
:
MouseEventHandler
<
HTMLButtonElement
>
;
hideText
?:
ReactNode
;
showText
?:
ReactNode
;
})
=>
{
const
buttonClasses
=
[
"button "
];
if
(
isToggle
)
{
buttonClasses
.
push
(
"red"
);
}
return
(
<
button
style=
{
{
marginTop
:
"10px"
}
}
className=
{
buttonClasses
.
join
(
" "
)
}
onClick=
{
onClick
}
>
{
isToggle
?
hideText
:
showText
}
</
button
>
);
};
export
default
ToggleButton
;
src/App.css
→
src/
containers/
App.css
View file @
55b14891
.App
{
display
:
flex
;
flex-direction
:
column
;
align-items
:
center
;
}
.person
{
.person
{
display
:
inline-block
;
display
:
inline-block
;
position
:
relative
;
position
:
relative
;
...
@@ -33,3 +39,25 @@
...
@@ -33,3 +39,25 @@
.removeBtn
:hover
{
.removeBtn
:hover
{
color
:
red
;
color
:
red
;
}
}
.App
.button
{
color
:
white
;
background
:
green
;
border-radius
:
3px
;
border-color
:
green
;
padding
:
1rem
;
}
.App
.button
:hover
{
background
:
#01da01
;
border-color
:
#01da01
;
}
.App
.red
{
background-color
:
red
;
border-color
:
red
;
}
.App
.button.red
:hover
{
background
:
#fa6262
;
border-color
:
#fa6262
;
}
src/App.tsx
→
src/
containers/
App.tsx
View file @
55b14891
import
type
{
ChangeEvent
}
from
"react"
;
import
type
{
ChangeEvent
}
from
"react"
;
import
{
useState
}
from
"react"
;
import
{
useState
}
from
"react"
;
import
"./App.css"
;
import
"./App.css"
;
import
Person
from
"./Person/Person"
;
import
IPerson
from
"../interfaces/IPerson"
;
import
IPerson
from
"./interfaces/IPerson"
;
import
People
from
"../components/People/People"
;
import
Counter
from
"../components/Counter/Counter"
;
import
ToggleButton
from
"../components/ToggleButton/ToggleButton"
;
type
State
=
(
IPerson
&
{
type
State
=
(
IPerson
&
{
hobby
:
string
;
hobby
:
string
;
...
@@ -48,10 +50,8 @@ const App = () => {
...
@@ -48,10 +50,8 @@ const App = () => {
setPeople
(
copyPeople
);
setPeople
(
copyPeople
);
};
};
const
changeNameHandler
=
(
const
changeNameHandler
=
(
e
:
ChangeEvent
<
HTMLInputElement
>
,
id
:
string
)
=>
{
e
:
ChangeEvent
<
HTMLInputElement
>
,
const
index
=
people
.
findIndex
((
person
)
=>
person
.
id
===
id
);
index
:
number
)
=>
{
const
copyPeople
=
structuredClone
(
people
);
const
copyPeople
=
structuredClone
(
people
);
copyPeople
[
index
].
name
=
e
.
target
.
value
;
copyPeople
[
index
].
name
=
e
.
target
.
value
;
...
@@ -62,44 +62,32 @@ const App = () => {
...
@@ -62,44 +62,32 @@ const App = () => {
setIsShowPeople
(
!
isShowPeople
);
setIsShowPeople
(
!
isShowPeople
);
};
};
const
removePerson
=
(
index
:
number
):
void
=>
{
const
removePerson
=
(
id
:
string
):
void
=>
{
const
index
=
people
.
findIndex
((
person
)
=>
person
.
id
===
id
);
const
copyPeople
=
structuredClone
(
people
);
const
copyPeople
=
structuredClone
(
people
);
copyPeople
.
splice
(
index
,
1
);
copyPeople
.
splice
(
index
,
1
);
setPeople
(
copyPeople
);
setPeople
(
copyPeople
);
// setPeople((currentState) => {
// console.log(currentState);
// return copyPeople;
// });
};
};
console
.
log
(
"in App"
,
people
);
const
peopleList
=
isShowPeople
?
(
<
People
onChangeName=
{
changeNameHandler
}
onClickHeader=
{
increaseAge
}
onRemove=
{
removePerson
}
people=
{
people
}
/>
)
:
null
;
const
showText
=
<
span
>
"Show people"
</
span
>;
return
(
return
(
<>
<>
<
div
className=
"App"
>
<
div
className=
"App"
>
<
button
onClick=
{
toggleIsShowPeople
}
>
<
ToggleButton
{
isShowPeople
?
"hide"
:
"show"
}
showText=
{
showText
}
</
button
>
onClick=
{
toggleIsShowPeople
}
{
isShowPeople
&&
isToggle=
{
isShowPeople
}
people
.
map
((
person
,
index
)
=>
{
/>
return
(
<
Counter
value=
{
people
.
length
}
/>
<
Person
{
peopleList
}
key=
{
person
.
id
}
name=
{
person
.
name
}
age=
{
person
.
age
}
onClickHeader=
{
()
=>
{
increaseAge
(
person
.
id
);
}
}
onChangeHandler=
{
(
e
)
=>
{
changeNameHandler
(
e
,
index
);
}
}
onRemoveHandler=
{
()
=>
{
removePerson
(
index
);
}
}
>
hobby:
{
person
.
hobby
}
</
Person
>
);
})
}
</
div
>
</
div
>
</>
</>
);
);
...
...
src/main.tsx
View file @
55b14891
import
React
from
'react'
import
React
from
"react"
;
import
ReactDOM
from
'react-dom/client'
import
ReactDOM
from
"react-dom/client"
;
import
App
from
'./App.tsx'
import
App
from
"./containers/App"
;
import
'./index.css'
import
"./index.css"
;
ReactDOM
.
createRoot
(
document
.
getElementById
(
'root'
)
as
HTMLElement
).
render
(
ReactDOM
.
createRoot
(
document
.
getElementById
(
"root"
)
as
HTMLElement
).
render
(
<
React
.
StrictMode
>
<
React
.
StrictMode
>
<
App
/>
<
App
/>
</
React
.
StrictMode
>
,
</
React
.
StrictMode
>
)
)
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment