@@ -107,6 +107,9 @@ export const App = () => {
107107+ const [color, setColor] = React.useState<Color>({red: 20, green: 40, blue: 180})
108108```
109109
110+ > We should consider here whether makes sense to group states, or even in more complex
111+ > cases use reducer / action approach (hooks).
112+
110113- Let's use the color browser to display that info.
111114
112115_ ./src/app.tsx_
@@ -120,3 +123,142 @@ _./src/app.tsx_
120123```
121124
122125- Let's give a try and see our _ ColorBrowser_ in action.
126+
127+ ``` bash
128+ npm start
129+ ```
130+
131+ - We want add color picker editing capabilities, let's create a color picker component
132+ and add a single slider for one of the color values.
133+
134+ _ ./src/components/colorpicker.tsx_
135+
136+ ``` typescript
137+ import * as React from " react" ;
138+ import { Color } from " ../model/color" ;
139+
140+ interface Props {
141+ color: Color ;
142+ onColorUpdated: (color : Color ) => void ;
143+ }
144+
145+ export const ColorPicker = (props : Props ) => (
146+ <div >
147+ < input
148+ type = " range"
149+ min = " 0"
150+ max = " 255"
151+ value = {props.color.red }
152+ onChange = {event =>
153+ props .onColorUpdated ({
154+ red: + event .target .value ,
155+ green: props .color .green ,
156+ blue: props .color .blue
157+ })
158+ }
159+ / >
160+ {props .color .red }
161+ < / div >
162+ );
163+ ```
164+
165+ - Let's register this component in our barrel.
166+
167+ _ ./src/components/index.ts_
168+
169+ ``` diff
170+ export * from "./hello";
171+ export * from "./nameEdit";
172+ export * from './colorBrowser';
173+ + export * from './colorPicker';
174+ ```
175+
176+ - Let's add this component to our import statement in the _ app_ file.
177+
178+ _ ./src/app.tsx_
179+
180+ ``` diff
181+ import * as React from "react";
182+ - import { HelloComponent, NameEditComponent, ColorBrowser } from "./components";
183+ + import { HelloComponent, NameEditComponent, ColorBrowser, ColorPicker } from "./components";
184+ import { Color } from "./model/color";
185+ ```
186+
187+ - Let's use the _ ColorPicker_ in our _ app_ component.
188+
189+ ``` diff
190+ return (
191+ <>
192+ <ColorBrowser color={color} />
193+ + <ColorPicker color={color} onColorUpdated={setColor}/>
194+ ```
195+
196+ - Let's give a try:
197+
198+ ``` bash
199+ npm start
200+ ```
201+
202+ - Now we can apply the same approach for the green and blue component, we will start
203+ with a dirty implementation (copying & pasting the code from the red slider and updating
204+ it for the green and blue sliders), this approach will create a poor ColorPicker component
205+ (but a working one), in the next sample (08) we will refactor this to obtain a more
206+ maintanable component.
207+
208+ _ ./src/components/colorpicker.ts_
209+
210+ ``` diff
211+ export const ColorPicker = (props: Props) => (
212+ <div>
213+ <input
214+ type="range"
215+ min="0"
216+ max="255"
217+ value={props.color.red}
218+ onChange={event =>
219+ props.onColorUpdated({
220+ red: +event.target.value,
221+ green: props.color.green,
222+ blue: props.color.blue
223+ })
224+ }
225+ />
226+ {props.color.red}
227+ + <br />
228+ + <input type="range"
229+ + min="0"
230+ + max="255"
231+ + value={props.color.green}
232+ + onChange={(event : any) => props.onColorUpdated(
233+ + {
234+ + red: props.color.red,
235+ + green: +event.target.value,
236+ + blue: props.color.blue
237+ + }
238+ + )}
239+ + />
240+ + {props.color.green}
241+ + <br />
242+ + <input type="range"
243+ + min="0"
244+ + max="255"
245+ + value={props.color.blue}
246+ + onChange={(event : any) => props.onColorUpdated(
247+ + {
248+ + red: props.color.red,
249+ + green: props.color.green,
250+ + blue: +event.target.value
251+ + }
252+ + )}
253+ + />
254+ + {props.color.blue}
255+ + <br />
256+ </div>
257+ );
258+ ```
259+
260+ - Let's give a try and check the results:
261+
262+ ``` bash
263+ npm start
264+ ```
0 commit comments