Skip to content

Commit c81b991

Browse files
committed
fix: Normalise string boolean values in decoder to support data encoding
1 parent 88945ed commit c81b991

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

src/modules/components/proposal/proposalActions/proposalActionsDecoder/proposalActionsDecoderBooleanField/proposalActionsDecoderBooleanField.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ describe('<ProposalActionsDecoderBooleanField /> component', () => {
6161
expect(useControllerSpy).toHaveBeenCalledWith(expect.objectContaining({ name: `${formPrefix}.${fieldName}` }));
6262
});
6363

64+
it('normalises string field values to booleans to support transaction data encoding', () => {
65+
const onChange = jest.fn();
66+
useControllerSpy.mockReturnValue({
67+
fieldState: { error: undefined },
68+
field: { value: 'true', onBlur: jest.fn(), onChange, ref: jest.fn() },
69+
} as unknown as ReactHookForm.UseControllerReturn);
70+
render(createTestComponent());
71+
expect(onChange).toHaveBeenCalledWith(true);
72+
});
73+
74+
it('does not update the field value when it is already a boolean', () => {
75+
const onChange = jest.fn();
76+
useControllerSpy.mockReturnValue({
77+
fieldState: { error: undefined },
78+
field: { value: false, onBlur: jest.fn(), onChange, ref: jest.fn() },
79+
} as unknown as ReactHookForm.UseControllerReturn);
80+
render(createTestComponent());
81+
expect(onChange).not.toHaveBeenCalled();
82+
});
83+
6484
it('renders the parameter name, type and notice as field labels', () => {
6585
const parameter = { name: 'tryExecute', notice: 'description', type: 'bool', value: undefined };
6686
render(createTestComponent({ parameter }));

src/modules/components/proposal/proposalActions/proposalActionsDecoder/proposalActionsDecoderBooleanField/proposalActionsDecoderBooleanField.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from 'react';
12
import { useController } from 'react-hook-form';
23
import { Radio, RadioGroup } from '../../../../../../core';
34
import { useGukModulesContext } from '../../../../gukModulesProvider';
@@ -39,13 +40,23 @@ export const ProposalActionsDecoderBooleanField: React.FC<IProposalActionsDecode
3940
const { error } = fieldState;
4041
const alert = error?.message == null ? undefined : { message: error.message, variant: 'critical' as const };
4142

43+
const { value, onChange } = field;
44+
45+
useEffect(() => {
46+
// Normalise string values ("true" / "false") seeded by the application to booleans as the transaction data
47+
// encoding only accepts boolean values for bool types.
48+
if (typeof value === 'string' && proposalActionsDecoderUtils.validateBoolean(value)) {
49+
onChange(value === 'true');
50+
}
51+
}, [value, onChange]);
52+
4253
const label = hideLabels ? undefined : (
4354
<>
4455
{name} <span className="text-neutral-500">({type})</span>
4556
</>
4657
);
4758

48-
const handleValueChange = (value: string) => field.onChange(value === 'true');
59+
const handleValueChange = (newValue: string) => onChange(newValue === 'true');
4960

5061
return (
5162
<RadioGroup
@@ -56,7 +67,7 @@ export const ProposalActionsDecoderBooleanField: React.FC<IProposalActionsDecode
5667
onBlur={field.onBlur}
5768
onValueChange={handleValueChange}
5869
ref={field.ref}
59-
value={field.value?.toString() ?? ''}
70+
value={value?.toString() ?? ''}
6071
>
6172
<Radio label={copy.proposalActionsDecoder.booleanTrue} value="true" />
6273
<Radio label={copy.proposalActionsDecoder.booleanFalse} value="false" />

0 commit comments

Comments
 (0)