-
-
Notifications
You must be signed in to change notification settings - Fork 118
/
Example4.tsx
141 lines (125 loc) · 3.44 KB
/
Example4.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { FC, useReducer } from 'react';
import CurrencyInput, { formatValue } from '../index';
type Field = {
value: number | undefined;
validationClass: string;
errorMessage: string;
};
type ExampleState = {
field1: Field;
field2: Field;
};
type Action = {
fieldName: string;
value: Field;
};
function reducer(state: ExampleState, { fieldName, value }: Action): ExampleState {
return {
...state,
[fieldName]: value,
};
}
const initialState: ExampleState = {
field1: {
value: 100,
validationClass: '',
errorMessage: '',
},
field2: {
value: 200,
validationClass: '',
errorMessage: '',
},
};
export const Example4: FC = () => {
const prefix = '£';
const [state, dispatch] = useReducer(reducer, initialState);
const handleOnValueChange = (_value: string | undefined, fieldName: string | undefined): void => {
if (!fieldName) {
return;
}
if (!_value) {
return dispatch({
fieldName,
value: {
value: undefined,
validationClass: '',
errorMessage: '',
},
});
}
const value = Number(_value);
if (!Number.isNaN(value)) {
dispatch({
fieldName,
value: {
value,
validationClass: 'is-valid',
errorMessage: '',
},
});
} else {
dispatch({
fieldName,
value: {
value,
validationClass: 'is-invalid',
errorMessage: 'Please enter a valid number',
},
});
}
};
const total = (state.field1.value || 0) + (state.field2.value || 0);
return (
<div className="row">
<div className="col-12 mb-4">
<a
href="https://github.com/cchanxzy/react-currency-input-field/blob/main/src/examples/Example4.tsx"
target="_blank"
rel="noreferrer"
>
<h2>Example 4</h2>
</a>
<ul>
<li>Add two values together</li>
<li>Format the total value</li>
</ul>
<form className="needs-validation">
<div className="row">
<div className="col">
<label htmlFor="validation-example-3-field1">Value 1</label>
<CurrencyInput
id="validation-example-3-field1"
name="field1"
className={`form-control ${state.field1.validationClass}`}
value={state.field1.value}
onValueChange={handleOnValueChange}
prefix={prefix}
/>
<div className="invalid-feedback">{state.field1.errorMessage}</div>
</div>
<div className="col">
<label htmlFor="validation-example-3-field2">Value 2</label>
<CurrencyInput
id="validation-example-3-field2"
name="field2"
className={`form-control ${state.field2.validationClass}`}
value={state.field2.value}
onValueChange={handleOnValueChange}
prefix={prefix}
/>
<div className="invalid-feedback">{state.field1.errorMessage}</div>
</div>
<div className="col">
<div className="">
<label>Total:</label>
<div className="h3">{formatValue({ prefix, value: String(total) })}</div>
</div>
</div>
</div>
</form>
</div>
</div>
);
};
export default Example4;