2020-08-10 18:29:17 +00:00
|
|
|
// License: LGPL-3.0-or-later
|
|
|
|
import * as React from 'react';
|
|
|
|
|
|
|
|
import { Money } from '../../common/money';
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { Formik, Field, useFormikContext } from 'formik';
|
|
|
|
import { MoneyTextField } from './index';
|
|
|
|
import { action } from '@storybook/addon-actions';
|
|
|
|
|
|
|
|
function FormikInner(props: { onChange:(args:{value:Money})=> void}) {
|
|
|
|
const context = useFormikContext<{value:Money}>();
|
|
|
|
const {value} = context.values;
|
|
|
|
const {onChange} = props;
|
|
|
|
useEffect(() => {
|
|
|
|
onChange({value});
|
|
|
|
}, [value, onChange]);
|
|
|
|
|
2021-02-26 20:23:29 +00:00
|
|
|
return <><div><span aria-label="cents">{value.cents}</span><span aria-label="currency">{value.currency}</span></div>
|
2020-08-10 18:29:17 +00:00
|
|
|
<Field component={MoneyTextField} name="value" aria-label="field" /></>;
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:24:14 +00:00
|
|
|
function FormikHandler(props: { onChange:(args:{value:Money})=> void, value: Money}) {
|
2020-08-10 18:29:17 +00:00
|
|
|
|
|
|
|
const {value, ...innerFormikProps} = props;
|
|
|
|
return (<Formik initialValues={{ value }} onSubmit={() => { console.log("submitted");}} enableReinitialize={true}>
|
|
|
|
<FormikInner {...innerFormikProps} />
|
|
|
|
</Formik>);
|
|
|
|
}
|
|
|
|
|
|
|
|
FormikHandler.defaultProps = {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
onChange: () => {},
|
|
|
|
locale: 'en',
|
|
|
|
};
|
|
|
|
export default { title: 'MoneyTextField' };
|
|
|
|
|
|
|
|
export function usd100() {
|
|
|
|
const usd100 = Money.fromCents(100, 'usd');
|
|
|
|
return <FormikHandler onChange={action('on-change')} value={usd100}/>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function jpy100() {
|
|
|
|
const jpy100 = Money.fromCents(100, 'jpy');
|
|
|
|
return <FormikHandler onChange={action('on-change')} value={jpy100}/>;
|
|
|
|
}
|