@@ -10,168 +10,12 @@ import PropTypes from 'prop-types';
1010import React from 'react' ;
1111import ReactDOM from 'react-dom' ;
1212
13- import ButtonWidget from './button_widget.jsx' ;
1413import HelloWidget from './hello_widget.jsx' ;
14+ import ListWidget from './list_widget.jsx' ;
1515import Styles from './styles.es' ;
1616import TextWidget from './text_widget.jsx' ;
1717
1818
19- class ListItem extends React . Component {
20- render ( ) {
21- return ( < li > < input type = 'checkbox'
22- checked = { this . props . isChecked }
23- onChange = { this . props . onChangeChecked } /> { this . props . caption } </ li > ) ;
24- }
25- }
26-
27- ListItem . propTypes = {
28- caption : PropTypes . string . isRequired ,
29- isChecked : PropTypes . bool . isRequired ,
30- onChangeChecked : PropTypes . func . isRequired
31- } ;
32-
33- class ListWidget extends React . Component {
34- constructor ( props ) {
35- super ( props ) ;
36-
37- this . _mutateStateToAddItem = ( state , strItemCaption ) => ( {
38- ...state ,
39- items : [
40- ...state . items ,
41- {
42- id : state . total_added ,
43- caption : strItemCaption ,
44- is_checked : false
45- }
46- ] ,
47- total_added : state . total_added + 1
48- } ) ;
49-
50- this . _mutateStateToUpdateItemCaption = ( state , id , strCaptionNew ) => ( {
51- ...state ,
52- items : state . items . map ( objItem => (
53- objItem . id === id ? {
54- ...objItem ,
55- caption : strCaptionNew
56- }
57- : objItem
58- ) )
59- } ) ;
60-
61- this . _mutateStateToUpdateItemChecked = ( state , id , isChecked ) => ( {
62- ...state ,
63- items : state . items . map ( objItem => (
64- objItem . id === id ? {
65- ...objItem ,
66- is_checked : isChecked
67- }
68- : objItem
69- ) )
70- } ) ;
71-
72- this . _mutateStateToRemoveItemsChecked = state => ( {
73- ...state ,
74- items : state . items . filter ( objItem => ! objItem . is_checked )
75- } ) ;
76-
77- const objStateEmpty = {
78- items : [ ] ,
79- total_added : 0
80- } ;
81-
82- this . state = this . _mutateStateToAddItem (
83- this . _mutateStateToAddItem (
84- this . _mutateStateToAddItem (
85- objStateEmpty ,
86- "Item 1" ) ,
87- "Item 2" ) ,
88- "Item 3" ) ;
89- }
90-
91- render ( ) {
92- const arrItemsSelected = this . state . items . filter ( objItem => objItem . is_checked ) ;
93-
94- const formatListOfNames = ( arrNames ) => arrNames . reduce (
95- ( strOutput , strName , index ) => (
96- `${ strOutput } ${ index > 0 // Will render either ' and' or ',' before 2nd item.
97- // Will render ', and' before the 3rd+ last item.
98- ? ( `${ arrNames . length > 2 ? ',' : ""
99- } ${ index === arrNames . length - 1 ? ' and' : "" } `
100- )
101- : "" } "${ strName } "`
102- ) ,
103- "" ) ;
104-
105- return (
106- < div style = { Styles . common } >
107- < div style = { Styles . content } >
108- < ul style = { { display : 'inline-block' , textAlign : 'left' , listStyleType : 'none' } } >
109- { this . state . items . map ( ( objItem , index ) => (
110- < ListItem key = { objItem . id }
111- caption = { objItem . caption }
112- isChecked = { objItem . is_checked }
113- onChangeChecked = { ( ) => {
114- this . setState (
115- this . _mutateStateToUpdateItemChecked (
116- this . state ,
117- objItem . id ,
118- ! objItem . is_checked ) ) ;
119- } }
120- />
121- ) ) }
122- </ ul >
123- </ div >
124- < div >
125- < ButtonWidget caption = "Add new item..."
126- onClick = { ( ) => {
127- const strItemNew = prompt ( "Please enter new item to add:" ,
128- "Item "
129- + ( this . state . items . length + 1 ) ) ;
130- if ( strItemNew === null ) {
131- return ;
132- }
133-
134- this . setState ( this . _mutateStateToAddItem ( this . state ,
135- strItemNew ) ) ;
136- } } />
137- < ButtonWidget caption = "Edit item..."
138- isDisabled = { arrItemsSelected . length !== 1 }
139- onClick = { ( ) => {
140- const objItemEdit = arrItemsSelected [ 0 ] ;
141- const strItemEdited = prompt ( "Please edit item:" ,
142- objItemEdit . caption ) ;
143- if ( strItemEdited === null ) {
144- return ;
145- }
146-
147- this . setState ( this . _mutateStateToUpdateItemCaption (
148- this . state ,
149- objItemEdit . id ,
150- strItemEdited ) ) ;
151- } } />
152- < ButtonWidget caption = { arrItemsSelected . length > 1 ? "Remove items..."
153- : "Remove item..." }
154- isDisabled = { arrItemsSelected . length === 0 }
155- onClick = { ( ) => {
156- const arrCaptions = arrItemsSelected
157- . map ( objItem => objItem . caption ) ;
158- const isConfirmed = confirm (
159- `Are you sure you want to remove item${
160- arrCaptions . length > 1 ? "s" : "" } ${
161- formatListOfNames ( arrCaptions )
162- } ?`) ;
163- if ( isConfirmed === false ) {
164- return ;
165- }
166-
167- this . setState ( this . _mutateStateToRemoveItemsChecked ( this . state ) ) ;
168- } } />
169- </ div >
170- </ div >
171- ) ;
172- }
173- }
174-
17519class ButtonForCounter extends React . Component {
17620 constructor ( props ) {
17721 super ( props ) ;
0 commit comments