-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added interactivity to AssetContainer
- Added FormProvider to RecordForm - Changed InstitutionContainer to useFieldArray - Updated story for AssetContainer - Updated AssetContainer docs
- Loading branch information
Showing
17 changed files
with
395 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,12 @@ | ||
// import AssetContainer from "../components/AssetContainer/AssetContainer"; | ||
import dynamic from "next/dynamic"; | ||
|
||
const AssetContainer = dynamic( | ||
() => import("../components/AssetContainer/AssetContainer"), | ||
{ | ||
ssr: true, | ||
} | ||
); | ||
import { RecordForm } from "~/RecordForm"; | ||
|
||
export default function Home() { | ||
// const response = await fetch( | ||
// "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/eur/jpy.json" | ||
// ); | ||
// const data = await response.json(); | ||
|
||
// async function search(formData) { | ||
// "use server"; | ||
// const query = formData.get("query"); | ||
// console.log(formData); | ||
// } | ||
|
||
const isClient = typeof window !== "undefined"; | ||
// const isClient = typeof window !== "undefined"; | ||
return ( | ||
<main> | ||
<AssetContainer /> | ||
<RecordForm /> | ||
</main> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"use client"; | ||
|
||
import theme from "./ChakraTheme.js"; | ||
import { ChakraProvider } from "@chakra-ui/react"; | ||
|
||
|
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,39 +1,34 @@ | ||
## Formatting numbers like 1,000.0000 | ||
For amount inputField I want numbers with thousands separator. It can be achieved with: | ||
```js | ||
const format = (val) => {formatting logic here} | ||
|
||
<NumberInput value={format(value)} /> | ||
## AssetContainer | ||
### Props | ||
```diff | ||
isCompact (bool). From [InstitutionContainer] | ||
onDeleteAsset (function) From [RecordForm] | ||
+ assetName (string `institutions.ID.assets.ID`) used for registering input | ||
|
||
- Asset (array) From [InstitutionContainer] // used before use-hook-form | ||
- InstitutionId | ||
- AssetId // used for registering fields to useForm, but later joined replaced with single assetName prop | ||
``` | ||
[Chakra docs](https://chakra-ui.com/docs/components/number-input#formatting-and-parsing-the-value) | ||
|
||
It can be achieved with several approaches: | ||
- Library for number formatting | ||
- [react-number-format](https://s-yadav.github.io/react-number-format/docs/props) I dont like nesting approach. To use input with props `<Input name="currency" placeholder="USD />` inside `<NumericFormat customInput={input}>` i need to create separate variable. At first glance it seems not optimal. | ||
- [numbrojs](https://numbrojs.com) | ||
- [numeraljs](http://numeraljs.com) (I used this one before and like it, but library seems very outdated) | ||
|
||
- Vanilla js with `toLocalString` | ||
[stackoverflow](https://stackoverflow.com/a/48062039/15007541) | ||
|
||
For RecordForm I sticked to vanilla JS, because seems easier and this way I reduce the number of dependencies. | ||
|
||
|
||
```jsx | ||
const [amount, setAmount] = useState(0); | ||
const numFormat = (val) => val.toLocaleString(); | ||
const parse = (val) => Number(val.replace(/^\$/, "")); // val is string, so need to use Number() to make it work with .toLocaleString | ||
|
||
<NumberInput // props go in chakra <NumberInput>, not <NumberInputField> | ||
onChange={(val) => setAmount(parse(val))} // val returns string | ||
value={numFormat(amount)} | ||
name="amount" | ||
px={2} | ||
> | ||
<NumberInputField /> | ||
</NumberInput> | ||
### Listeners | ||
- deleteButton onClick | ||
- inputs onChange | ||
|
||
### States | ||
```mermaid | ||
--- | ||
title: AssetContainer | ||
--- | ||
stateDiagram-v2 | ||
direction LR | ||
[*] --> expanded | ||
[*] --> compact | ||
expanded --> compact : prop iscompact changed | ||
compact --> expanded : prop iscompact changed | ||
``` | ||
|
||
`Number("") === 0` so when i clear input i got 0. For now i decided to let this be | ||
|
||
This will be changed later when used with *react-hook-form* | ||
## See also | ||
- [next.js/examples/next-forms at canary · vercel/next.js · GitHub](https://github.com/vercel/next.js/tree/canary/examples/next-forms) | ||
- [Data Fetching: Server Actions and Mutations | Next.js](https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#forms) | ||
- https://react-hook-form.com/docs/usefieldarray |
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 @@ | ||
export { AssetContainer } from "./AssetContainer"; |
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,39 @@ | ||
## Formatting numbers like 1,000.0000 | ||
For amount inputField I want numbers with thousands separator. It can be achieved with: | ||
```js | ||
const format = (val) => {formatting logic here} | ||
|
||
<NumberInput value={format(value)} /> | ||
``` | ||
[Chakra docs](https://chakra-ui.com/docs/components/number-input#formatting-and-parsing-the-value) | ||
|
||
It can be achieved with several approaches: | ||
- Library for number formatting | ||
- [react-number-format](https://s-yadav.github.io/react-number-format/docs/props) I dont like nesting approach. To use input with props `<Input name="currency" placeholder="USD />` inside `<NumericFormat customInput={input}>` i need to create separate variable. At first glance it seems not optimal. | ||
- [numbrojs](https://numbrojs.com) | ||
- [numeraljs](http://numeraljs.com) (I used this one before and like it, but library seems very outdated) | ||
|
||
- Vanilla js with `toLocalString` | ||
[stackoverflow](https://stackoverflow.com/a/48062039/15007541) | ||
|
||
For RecordForm I sticked to vanilla JS, because seems easier and this way I reduce the number of dependencies. | ||
|
||
|
||
```jsx | ||
const [amount, setAmount] = useState(0); | ||
const numFormat = (val) => val.toLocaleString(); | ||
const parse = (val) => Number(val.replace(/^\$/, "")); // val is string, so need to use Number() to make it work with .toLocaleString | ||
|
||
<NumberInput // props go in chakra <NumberInput>, not <NumberInputField> | ||
onChange={(val) => setAmount(parse(val))} // val returns string | ||
value={numFormat(amount)} | ||
name="amount" | ||
px={2} | ||
> | ||
<NumberInputField /> | ||
</NumberInput> | ||
``` | ||
|
||
`Number("") === 0` so when i clear input i got 0. For now i decided to let this be | ||
|
||
This will be changed later when used with *react-hook-form* |
29 changes: 29 additions & 0 deletions
29
components/AssetContainer/storybook/AssetContainer.stories.jsx
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,29 @@ | ||
import { AssetContainer } from "~/AssetContainer"; | ||
import { AssetContainerDecorator } from "./AssetContainerDecorator"; | ||
|
||
const meta = { | ||
title: "RecordForm/AssetContainer", | ||
component: AssetContainer, | ||
decorators: [ | ||
(Story, { args }) => ( | ||
<AssetContainerDecorator> | ||
<Story /> | ||
</AssetContainerDecorator> | ||
), | ||
], | ||
}; | ||
export default meta; | ||
|
||
export const Expanded = { | ||
args: { | ||
isCompact: false, | ||
assetName: "institutions.0.assets.0", | ||
}, | ||
}; | ||
|
||
export const Compact = { | ||
args: { | ||
...Expanded.args, | ||
isCompact: true, | ||
}, | ||
}; |
Oops, something went wrong.