-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...t/src/modules/object-record/record-field/form-types/components/FormCurrencyFieldInput.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useMemo } from 'react'; | ||
import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode'; | ||
import { FieldCurrencyValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { VariablePickerComponent } from '@/object-record/record-field/form-types/types/VariablePickerComponent'; | ||
import { FormFieldInputContainer } from '@/object-record/record-field/form-types/components/FormFieldInputContainer'; | ||
import { InputLabel } from '@/ui/input/components/InputLabel'; | ||
import { FormNestedFieldInputContainer } from '@/object-record/record-field/form-types/components/FormNestedFieldInputContainer'; | ||
import { FormNumberFieldInput } from '@/object-record/record-field/form-types/components/FormNumberFieldInput'; | ||
import { FormSelectFieldInput } from '@/object-record/record-field/form-types/components/FormSelectFieldInput'; | ||
import { SETTINGS_FIELD_CURRENCY_CODES } from '@/settings/data-model/constants/SettingsFieldCurrencyCodes'; | ||
|
||
type FormCurrencyFieldInputProps = { | ||
label?: string; | ||
defaultValue?: FieldCurrencyValue | null; | ||
onPersist: (value: { | ||
currencyCode: CurrencyCode; | ||
amountMicros: number | string | null; | ||
}) => void; | ||
VariablePicker?: VariablePickerComponent; | ||
}; | ||
|
||
export const FormCurrencyFieldInput = ({ | ||
label, | ||
defaultValue, | ||
onPersist, | ||
VariablePicker, | ||
}: FormCurrencyFieldInputProps) => { | ||
const currencies = useMemo(() => { | ||
return Object.entries(SETTINGS_FIELD_CURRENCY_CODES).map( | ||
([key, { Icon, label }]) => ({ | ||
value: key, | ||
icon: Icon, | ||
label: `${label} (${key})`, | ||
}), | ||
); | ||
}, []); | ||
|
||
const handleAmountMicrosChange = ( | ||
newAmountMicros: string | number | null, | ||
) => { | ||
onPersist({ | ||
currencyCode: (defaultValue?.currencyCode ?? '') as CurrencyCode, | ||
amountMicros: newAmountMicros ?? '', | ||
}); | ||
}; | ||
|
||
const handleCurrencyCodeChange = (newCurrencyCode: string | null) => { | ||
onPersist({ | ||
currencyCode: (newCurrencyCode ?? '') as CurrencyCode, | ||
amountMicros: defaultValue?.amountMicros ?? null, | ||
}); | ||
}; | ||
|
||
return ( | ||
<FormFieldInputContainer> | ||
{label ? <InputLabel>{label}</InputLabel> : null} | ||
<FormNestedFieldInputContainer> | ||
<FormSelectFieldInput | ||
label="Currency Code" | ||
defaultValue={defaultValue?.currencyCode ?? ''} | ||
onPersist={handleCurrencyCodeChange} | ||
options={currencies} | ||
clearLabel={'Currency Code'} | ||
VariablePicker={VariablePicker} | ||
/> | ||
<FormNumberFieldInput | ||
label="Amount Micros" | ||
defaultValue={defaultValue?.amountMicros ?? ''} | ||
onPersist={handleAmountMicrosChange} | ||
VariablePicker={VariablePicker} | ||
placeholder="Set 3210000 for 3.21$" | ||
/> | ||
</FormNestedFieldInputContainer> | ||
</FormFieldInputContainer> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
...-record/record-field/form-types/components/__stories__/FormCurrencyFieldInput.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { FormCurrencyFieldInput } from '../FormCurrencyFieldInput'; | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { FieldCurrencyValue } from '@/object-record/record-field/types/FieldMetadata'; | ||
import { CurrencyCode } from '@/object-record/record-field/types/CurrencyCode'; | ||
import { within } from '@storybook/test'; | ||
|
||
const meta: Meta<typeof FormCurrencyFieldInput> = { | ||
title: 'UI/Data/Field/Form/Input/FormCurrencyFieldInput', | ||
component: FormCurrencyFieldInput, | ||
args: {}, | ||
argTypes: {}, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof FormCurrencyFieldInput>; | ||
|
||
const defaultSalaryValue: FieldCurrencyValue = { | ||
currencyCode: CurrencyCode.USD, | ||
amountMicros: 44000000, | ||
}; | ||
|
||
export const Default: Story = { | ||
args: { | ||
label: 'Salary', | ||
defaultValue: defaultSalaryValue, | ||
}, | ||
play: async ({ canvasElement }) => { | ||
const canvas = within(canvasElement); | ||
await canvas.findByText('Currency Code'); | ||
await canvas.findByText('Amount Micros'); | ||
}, | ||
}; |