forked from MetaMask/metamask-docs
-
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.
feat(docs): ACT-1360 - Components For Complex Types (MetaMask#1370)
* feat(docs): added rpc-parser component for reference pages * feat(docs): added mm install msg * feat(docs): change mm condition * feat(docs): refactored styles * feat(docs): added request/response section * feat(docs): added error section * feat(docs): added eth_call * added default logic for InteractiveBox * Made enhancements for components * returned condition for disabled button * removed not needed styles in Drawer * started implementing of handling complex types * made some progress * added UI and logic for complex array * added label clear on cancel and back * fixed styles for chevron icon * feat(docs): added cancel func for complex types * implemented complex types except object * redesigned drawer * fixed some issues * improved some styles * fixed a bug * fixed icon color for theme change * fixed code style * fixed code style MetaMask#2 --------- Co-authored-by: ae_atrofimov <[email protected]>
- Loading branch information
1 parent
94ad9a2
commit c4c3e73
Showing
19 changed files
with
630 additions
and
185 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 |
---|---|---|
|
@@ -105,4 +105,4 @@ | |
"$root$": false | ||
} | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/components/ParserOpenRPC/InteractiveBox/fields/ConditionalField.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,96 @@ | ||
import React, { useContext, useEffect, useState } from "react"; | ||
import { FieldTemplateProps } from "@rjsf/utils"; | ||
import { BaseInputTemplate } from "@site/src/components/ParserOpenRPC/InteractiveBox/templates/BaseInputTemplate"; | ||
import { SelectWidget } from "@site/src/components/ParserOpenRPC/InteractiveBox/widgets/SelectWidget"; | ||
import styles from "@site/src/components/ParserOpenRPC/InteractiveBox/styles.module.css"; | ||
import { ParserOpenRPCContext } from "@site/src/components/ParserOpenRPC"; | ||
import clsx from "clsx"; | ||
|
||
export const ConditionalField = (props: FieldTemplateProps) => { | ||
const [isOpened, setIsOpened] = useState(false); | ||
const [selectedTypeSchema, setSelectedTypeSchema] = useState(null); | ||
const [isEditView, setIsEditView] = useState(false); | ||
const { setIsDrawerContentFixed, setDrawerLabel, isComplexTypeView, setIsComplexTypeView } = useContext(ParserOpenRPCContext); | ||
const { formData, schema, name, onChange } = props; | ||
const listItems = schema?.anyOf ? schema?.anyOf : schema?.oneOf; | ||
const checkForNullTypeSchema = (type) => type === "null"; | ||
const showComplexTypeView = () => { | ||
setDrawerLabel(name); | ||
setIsDrawerContentFixed(true); | ||
setIsEditView(true); | ||
setIsComplexTypeView(true); | ||
} | ||
const onDropdownOptionClick = (e) => { | ||
const selectedSchema = listItems.find(({ title }) => title === e.target.dataset.value); | ||
const isNullTypeSchema = checkForNullTypeSchema(selectedSchema?.type); | ||
if (isNullTypeSchema) { | ||
onChange(null); | ||
} else { | ||
setSelectedTypeSchema(listItems.find(({ title }) => title === e.target.dataset.value)); | ||
showComplexTypeView(); | ||
} | ||
setIsOpened(false); | ||
} | ||
const selectWidgetProps = { | ||
...props, | ||
schema: selectedTypeSchema, | ||
label: name, | ||
value: formData, | ||
...(selectedTypeSchema?.enum && { | ||
options:{ | ||
enumOptions: selectedTypeSchema?.enum.map(item => ({ label: item, value: item })) | ||
} | ||
}) | ||
} | ||
const baseInputProps = { | ||
...props, | ||
schema: selectedTypeSchema | ||
} | ||
|
||
useEffect(() => { | ||
if(!isComplexTypeView) { | ||
setIsEditView(false); | ||
setSelectedTypeSchema(null); | ||
} | ||
}, [isComplexTypeView]) | ||
|
||
return listItems?.length > 0 ? ( | ||
<> | ||
<div className={styles.tableRow}> | ||
<div className={styles.tableColumn}> | ||
<label className={styles.tableColumnParam}>{name}</label> | ||
</div> | ||
<div className={styles.tableColumn}> | ||
<div className={clsx(styles.tableValueRow, styles.tableValueRowPadding)}> | ||
{formData === undefined ? "" : String(formData)} | ||
<span className={styles.tableColumnType}> | ||
<span className={styles.dropdown} onClick={() => { setIsOpened(!isOpened); }}> | ||
{schema?.anyOf ? "anyOf" : "oneOf"} | ||
<span className={clsx(styles.tableColumnIcon, styles.chevronIcon, styles.dropdownChevronIcon, !isOpened && styles.chevronIconDown)}/> | ||
<span className={clsx(styles.chevronIcon, styles.dropdownChevronIcon, !isOpened && styles.chevronIconDown)}/> | ||
</span> | ||
<ul className={clsx(styles.dropdownList, !isOpened && styles.dropdownListClosed)}> | ||
{listItems?.map((listItem, index) => ( | ||
<li | ||
className={styles.dropdownItem} | ||
key={index} | ||
onClick={onDropdownOptionClick} | ||
data-value={listItem.title} | ||
> | ||
{`${listItem.title}: ${listItem?.enum ? "enum" : listItem.type}`} | ||
</li> | ||
))} | ||
</ul> | ||
</span> | ||
</div> | ||
</div> | ||
</div> | ||
{isComplexTypeView && isEditView && selectedTypeSchema && selectedTypeSchema.type !== "null" ? | ||
<div className={styles.tableComplexType}> | ||
{selectedTypeSchema?.enum ? <SelectWidget {...selectWidgetProps} /> : <BaseInputTemplate {...baseInputProps} />} | ||
</div> | ||
: null | ||
} | ||
</> | ||
) : null; | ||
} |
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
Oops, something went wrong.