Skip to content

Commit d2860cc

Browse files
committed
fixing controlled states for custom made fields 4h
1 parent 2b99c16 commit d2860cc

26 files changed

Lines changed: 450 additions & 166 deletions

packages/frui-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frui-core",
3-
"version": "0.0.24",
3+
"version": "0.0.25",
44
"license": "MIT",
55
"description": "Core library of FRUI",
66
"author": "Chris <chris@incept.asia>",

packages/frui-core/src/hooks/useCountry.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CountryOption, CountryConfig } from '../types/fields';
44
import countries from '../data/countries.json';
55

66
export default function useSelectCountry(config: CountryConfig) {
7-
const { value, map } = config;
7+
const { value, defaultValue, map } = config;
88
//generate options
99
const options = countries
1010
.filter(country => country.currencyType === 'fiat')
@@ -15,7 +15,12 @@ export default function useSelectCountry(config: CountryConfig) {
1515
option => option.value?.countryCode === value
1616
)[0] as CountryOption
1717
: undefined;
18-
19-
return { selected, options };
2018

19+
const selectedDefault = typeof defaultValue === 'string'
20+
? options.filter(
21+
option => option.value?.countryCode === defaultValue
22+
)[0] as CountryOption
23+
: undefined;
24+
25+
return { selected, selectedDefault, options };
2126
};

packages/frui-core/src/hooks/useCurrency.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CurrencyOption, CurrencyConfig } from '../types/fields';
44
import countries from '../data/countries.json';
55

66
export default function useSelectCurrency(config: CurrencyConfig) {
7-
const { value, map } = config;
7+
const { value, defaultValue, map } = config;
88
//generate options
99
const options = countries
1010
.filter(country => country.currencyType === 'fiat')
@@ -17,5 +17,11 @@ export default function useSelectCurrency(config: CurrencyConfig) {
1717
)[0] as CurrencyOption
1818
: undefined;
1919

20-
return { selected, options };
20+
const selectedDefault = typeof defaultValue === 'string'
21+
? options.filter(
22+
option => option.value?.currencyCode === defaultValue
23+
)[0] as CurrencyOption
24+
: undefined;
25+
26+
return { selected, selectedDefault, options };
2127
};

packages/frui-core/src/hooks/useFieldset.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import type { FieldsetConfig } from '../types/components';
22
//hooks
3-
import { useState } from 'react';
3+
import { useState, useEffect } from 'react';
44

55
export default function useFieldset<ValueType = any>(
66
config: FieldsetConfig<ValueType>
77
) {
88
//extract props
99
const {
1010
value,
11+
defaultValue,
1112
emptyValue,
1213
onChange,
1314
onUpdate
1415
} = config;
1516

1617
//make sure we have an array
17-
const safeValues: (ValueType|undefined)[] = Array.isArray(value) ? value : [];
18+
const safeValues: (ValueType|undefined)[] = Array.isArray(defaultValue) ? [ ...defaultValue ] : [];
1819
//hooks
1920
const [ values, setValues ] = useState(safeValues);
2021
//handlers
@@ -35,7 +36,13 @@ export default function useFieldset<ValueType = any>(
3536
}
3637
},
3738
add: () => handlers.set(values.concat([emptyValue]))
38-
}
39-
39+
};
40+
//for controlled states we should update
41+
//the values when the value prop changes
42+
useEffect(() => {
43+
if (!Array.isArray(value)) return;
44+
handlers.set([ ...value ]);
45+
}, [ value ]);
46+
4047
return { values, handlers };
4148
}

packages/frui-core/src/hooks/useNumber.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ function getFormatsFromInput(
148148
export default function useNumber(config: NumberProps) {
149149
//expand props
150150
const {
151+
value,
151152
defaultValue,
152153
min,
153154
max,
@@ -219,6 +220,14 @@ export default function useNumber(config: NumberProps) {
219220
input.selectionEnd = cursor;
220221
}
221222
}, [ cursor ]);
223+
//for controlled states we should update
224+
//the values when the value prop changes
225+
useEffect(() => {
226+
if (value === undefined) return;
227+
const newValue = getFormats(String(value || ''), options);
228+
setHiddenValue(newValue.value);
229+
setDisplayValue(newValue.display);
230+
}, [ value ]);
222231

223232
return { displayValue, handlers };
224233
}

packages/frui-core/src/hooks/useSelect.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import type { KeyboardEvent } from 'react';
33
import type { SelectOption, SelectConfig } from '../types/fields';
44
//hooks
5-
import { useState } from 'react';
5+
import { useState, useEffect } from 'react';
66

77
export default function useSelect(config: SelectConfig) {
88
const {
99
value,
10+
defaultValue,
1011
onDropdown,
1112
onSelected,
1213
onUpdate
@@ -15,7 +16,7 @@ export default function useSelect(config: SelectConfig) {
1516
//search query string
1617
const [ query, setQuery ] = useState('');
1718
//selected option
18-
const [ selected, setSelected ] = useState(value);
19+
const [ selected, setSelected ] = useState(defaultValue);
1920
//whether to show dropdown
2021
const [ showing, show ] = useState(false);
2122
//handlers
@@ -65,6 +66,13 @@ export default function useSelect(config: SelectConfig) {
6566
}
6667
};
6768

69+
//for controlled states we should update
70+
//the values when the value prop changes
71+
useEffect(() => {
72+
if (!value) return;
73+
setSelected(value)
74+
}, [ value ]);
75+
6876
return { selected, showing, handlers };
6977

7078
};

packages/frui-core/src/types/components.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export type ControlProps = {
8080
// Fieldset component
8181
export type FieldsetConfig<ValueType = any> = {
8282
value?: ValueType[],
83+
defaultValue?: ValueType[],
8384
emptyValue?: ValueType,
8485
onChange?: (values: ValueType[]) => void,
8586
onUpdate?: (values: ValueType[]) => void
@@ -90,6 +91,7 @@ export type FieldsetProps<ValueType = any> = ExtendsType<ButtonProps, {
9091
add?: string,
9192
data?: Record<string, any>,
9293
value?: ValueType[],
94+
defaultValue?: ValueType[],
9395
emptyValue?: ValueType,
9496
error?: boolean,
9597
errorColor?: string,

packages/frui-core/src/types/fields.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export type CountryData = {
7272
};
7373
export type CountryOption = SelectOption<CountryData>;
7474
export type CountryConfig = {
75-
value: string | CountryOption | undefined,
75+
value?: string | CountryOption | undefined,
76+
defaultValue?: string | CountryOption | undefined,
7677
map: (country: CountryData) => CountryOption
7778
};
7879
export type CountryProps = ExtendsType<SelectProps, {
@@ -85,7 +86,8 @@ export type CurrencyOption = SelectOption<CountryData>;
8586
export type CurrencyConfig = CountryConfig;
8687
export type CurrencyProps = ExtendsType<SelectProps, {
8788
options?: undefined,
88-
value?: CurrencyOption|string
89+
value?: CurrencyOption|string,
90+
defaultValue?: CurrencyOption|string
8991
}>;
9092

9193
// date component
@@ -240,6 +242,7 @@ export type SelectOption<T = any> = {
240242
};
241243
export type SelectConfig = {
242244
value?: SelectOption,
245+
defaultValue?: SelectOption,
243246
onDropdown?: (show: boolean) => void,
244247
onSelected?: (value: SelectOption) => void,
245248
onUpdate?: (value: string|number) => void
@@ -257,6 +260,7 @@ export type SelectDropdownProps = {
257260
};
258261
export type SelectProps = {
259262
value?: SelectOption,
263+
defaultValue?: SelectOption,
260264
options: SelectOption[]|Record<string, string>,
261265
searchable?: boolean,
262266
placeholder?: string,

packages/frui-react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frui-react",
3-
"version": "0.0.24",
3+
"version": "0.0.25",
44
"license": "MIT",
55
"description": "A collection of vanilla react components written in typescript.",
66
"author": "Chris <chris@incept.asia>",
@@ -19,7 +19,7 @@
1919
"react-dom": "^18.0.0"
2020
},
2121
"dependencies": {
22-
"frui-core": "0.0.24",
22+
"frui-core": "0.0.25",
2323
"inputmask": "5.0.8",
2424
"markdown-to-jsx": "7.2.0",
2525
"moment": "2.29.4"

packages/frui-react/src/Fieldset.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export default function make<ValueType = any>(
2121
add,
2222
data,
2323
value,
24+
defaultValue,
2425
emptyValue,
2526
error,
2627
errorColor = '#DC3545',
@@ -33,6 +34,7 @@ export default function make<ValueType = any>(
3334

3435
const { values, handlers } = useFieldset({
3536
value,
37+
defaultValue,
3638
emptyValue,
3739
onChange,
3840
onUpdate

0 commit comments

Comments
 (0)