Skip to content

Commit fe092ee

Browse files
committed
07 color picker completed
1 parent 547c2c4 commit fe092ee

4 files changed

Lines changed: 203 additions & 2 deletions

File tree

hooks/07_ColorPicker/Readme.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
```

hooks/07_ColorPicker/src/app.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { HelloComponent, NameEditComponent, ColorBrowser } from "./components";
2+
import { HelloComponent, NameEditComponent, ColorBrowser, ColorPicker } from "./components";
33
import { Color } from "./model/color";
44

55
export const App = () => {
@@ -29,6 +29,7 @@ export const App = () => {
2929
return (
3030
<>
3131
<ColorBrowser color={color} />
32+
<ColorPicker color={color} onColorUpdated={setColor}/>
3233
<HelloComponent userName={name} />
3334
<NameEditComponent
3435
initialUserName={name}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from "react";
2+
import { Color } from "../model/color";
3+
4+
interface Props {
5+
color: Color;
6+
onColorUpdated: (color: Color) => void;
7+
}
8+
9+
export const ColorPicker = (props: Props) => (
10+
<div>
11+
<input
12+
type="range"
13+
min="0"
14+
max="255"
15+
value={props.color.red}
16+
onChange={event =>
17+
props.onColorUpdated({
18+
red: +event.target.value,
19+
green: props.color.green,
20+
blue: props.color.blue
21+
})
22+
}
23+
/>
24+
{props.color.red}
25+
<br />
26+
<input
27+
type="range"
28+
min="0"
29+
max="255"
30+
value={props.color.green}
31+
onChange={(event: any) =>
32+
props.onColorUpdated({
33+
red: props.color.red,
34+
green: event.target.value,
35+
blue: props.color.blue
36+
})
37+
}
38+
/>
39+
{props.color.green}
40+
<br />
41+
<input
42+
type="range"
43+
min="0"
44+
max="255"
45+
value={props.color.blue}
46+
onChange={(event: any) =>
47+
props.onColorUpdated({
48+
red: props.color.red,
49+
green: props.color.green,
50+
blue: event.target.value
51+
})
52+
}
53+
/>
54+
{props.color.blue}
55+
<br />
56+
</div>
57+
);
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./hello";
22
export * from "./nameEdit";
3-
export * from './colorBrowser';
3+
export * from "./colorBrowser";
4+
export * from "./colorPicker";

0 commit comments

Comments
 (0)