
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
@mobx-json/mui-form
Advanced tools
The simplest way to manage your form in React.
State management is charged by mobx, but you can use the materia-ui wrapped one without any knowledge of it.
You can check some examples here: https://mobx-json.thundermiracle.com
npm i -S @mobx-json/mui-form
or
yarn @mobx-json/mui-form
Define your form in JSON or javascript.
{
"fields": [
{
"settings": {
"widget": "TextField",
"rule": "required|min:3"
},
"attrs": {
"name": "id",
"label": "Use Id",
"placeholder": "input your user id"
}
},
{
"settings": {
"widget": "Password",
"rule": "required|min:8"
},
"attrs": {
"name": "password",
"label": "Password",
"placeholder": "at least 8 chars"
}
}
]
}
Create the form
import { useMuiJsonForm } from '@mobx-json/form';
import LoginFormJson from './blueprints/login-form.json';
const IndexPage = props => {
const { form, submitWithCheck } = useMuiJsonForm({
blueprint: LoginFormJson,
});
const handleSubmit = React.useCallback(async () => {
const data = submitWithCheck();
if (data) {
await tryLogin(data);
}
}, [submitWithCheck]);
// the following one is material-ui, you can use native ones as well
return (
<Container maxWidth="xs">
<Card>
<CardContent>{form}</CardContent>
<CardActions>
<Button color="primary" onClick={handleSubmit}>
LOGIN
</Button>
</CardActions>
</Card>
</Container>
);
}
Display
The blueprint MUST be defined as follows.
{
"fields": [
...
]
}
Each field should contains 3 parts including settings, attrs and fields (which is only necessary for settings.valueType = container).
normal component
{
"fields": [
{
"settings": {
"widget": "TextField",
"valueType": "string"
...
},
"attrs": {
"name": "firstName"
...
}
}
]
}
container
{
"fields": [
{
"settings": {
"widget": "GridItemContainer",
"valueType": "container"
...
},
"attrs": {
"name": "baseInfoGrid"
...
},
"fields": [
{
"settings": {
"widget": "TextField",
"valueType": "string"
...
},
"attrs": {
"name": "firstName"
...
}
}
]
}
]
}
To define which component to use and validation, etc.
exp:
{
"settings": {
"widget": "TextField", // required
"valueType": "string", // default: string
"rule": "required"
}
}
generate: TextField
component and display error if input is empty.
Property | Required | Type | Default | Description |
---|---|---|---|---|
widget | 〇 | string | 1. components contained in the package(TextField, Select...) 2. component you passed by widgetMap 3. native component in html(div, span...) | |
valueType | string | string | one of number, string, array, boolean, container | |
rule | string | null | using validatorjs as default validation tool, you can check the default validation rules here | |
propRule | string | null | rule to apply prop dynamically by input values. | |
computeRule | string | null | rule to compute value dynamically by target fields' value. |
Folloing basic components are included in this package for quicker start: Checkbox, Checkboxes, Display, Password, PasswordOutlined, Radio, Select, SelectOutlined, Switch, TextField, TextFieldOutlined.
As every value from e.target.value is convertted into string, we have to define a correct valueType to format.
※ container: a special valueType which means sub fields are contained
A rule to validate the input.
exp: "rule": "required|max:30"
will throws error if input is empty or over 30.
Apply defined prop to attrs if meets theYou can define multiple propRule and split them by |
Format:
propName,propValueWhenMeetCondition,propValueType:targetFieldName,targetFieldValue
* Which means if (targetFieldName.value == targetFieldValue) set propName->propValue
exp: "propRule": "hidden,true,boolean:firstName,Daniel"
will hide the component when the firstName equals 'Daniel'.
Compute by other fields value. You can define multiple computeRule and split them by |, but the last rule will win.
Format:
computeMethod:targetFieldName1,targetFieldName2,targetFieldName3...:extraInfo
Now supports 2 computeMethods:
concat
concat all targetFields' value into one string
exp: "computeRule": "concat:firstName,lastName:, "
When firstName is 'Daniel', lastName is Wood, component's value will automatically changed to 'Daniel, Woo'.
sum
sum all targetFields' value include string numbers. return 0 if character is contained.
exp: "computeRule": "sum:profit1,profit2"
When profit1 is 100, profit2 is 200, component's value will automatically changed to 300.
All props defined in attrs will be passed to each component. And attrs.name MUST
be unique in each form.
exp:
{
"settings": {
"widget": "TextField",
"valueType": "string"
},
"attrs": {
"name": "firstName", // required & MUST be unique
"label": "First Name",
"placeholder": "input your first name"
}
}
generates:
<TextField name="firstName" label="First Name" placeholder="input your first name" />
Extended props in every component:
Property | Required | Type | Default | Description |
---|---|---|---|---|
grid | object | { "xs": 12 } | responsive grid layout. Automatically add xs=12 to every component unless noGrid is true . See here for details. | |
noGrid | boolean | false | if true , grid will be ignored | |
defaultValue | any | null | applied to value if it's undefined | |
value | any | null | value for display | |
keepLabelSpace | boolean | false | if true , margin-top: 16px | |
icon | string | 1. iconsMap MUST be passed in initialization. see iconsMap example and how to initialize. 2. icon is one of the keys defined in iconsMap.js. 3. IconComponent will be passed to each component. | ||
itemsSource | string | 1. itemsSource MUST be passed in initialization. see itemsSource example and how to initialize. 2. itemsSource is one of the keys defined in itemsSource.js. 3. itemsSource will be converted to items and passed to each component. 4. Available in Radio, Checkboxes component |
Check the example page:
rest props are the same with material-ui's Grid component.
{
"settings": {
"widget": "GridItemContainer", // required
"valueType": "container" // required
... // others like propRule
},
"attrs": {
"name": "nameGrid", // required
"divider": false, // show divider in the bottom of the Grid component if true
"primary": '', // primary text size h4 in Grid
"primaryProps": {}, // props for primary text's typography
"secondary": '', // secondary text size body2 under the primary text
"secondaryProps": {}, // props for secondaryProps text's typography
"subPrimary": '', // subPrimary text size h6 right of the primary text
"subPrimaryProps": {}, // props for subPrimaryProps text's typography
"hidden": false, // hide all sub fields if true (propagate hidden prop to all children)
"disabled": false, // disable all sub fields if true (propagate disable prop to all children)
...rest props
}
}
rest props are the same with material-ui's Grid component.
{
"settings": {
"widget": "Checkbox",
"valueType": "boolean",
"rule": "required" // invalid if not checked
},
"attrs": {
"name": "subscription",
"label": "Subscribe the news letter!"
...rest props
}
}
{
"settings": {
"widget": "Checkboxes",
"valueType": "array",
"rule": "required"
},
"attrs": {
"name": "interest",
"label": "Your Interests",
"selectAll": false, // if true, show SelectAll checkbox
"selectAllLabel": "ALL", // default label of SelectAll checkbox is ALL
"icon": "", // get IconComponent from iconsMap
"itemsSource": "", // get items definition from itemsSource
"items": [ // items MUST be an array with label&value
{
"label": "Movie",
"value": 1
},
{
"label": "Gaming",
"value": 2
},
{
"label": "Coding",
"value": 3
}
]
}
}
For keeping data and will not be rendered.
exp:
{
"settings": {
"widget": "Data"
},
"attrs": {
"name": "id"
}
}
For display and format value. See examples.
formatters:
{
"settings": {
"widget": "Display"
},
"attrs": {
"name": "birthday",
"label": "Birthday",
"icon": "Today",
"format": {
"type": "date",
"template": "yyyy年MM月dd日"
}
}
}
Package is using hook to make the form and return submit functions. React > 16.8 is needed.
Need if icon, itemsSource
is used, use customized components or display self-defined error messages.
Parameter | Required | Type | Default | Description |
---|---|---|---|---|
locale | string | en | validatorjs' locale string | |
messages | object | {} | key-value object for extended error messages | |
itemsSource | △ | object | {} | key-value object for items definition. ※ required if itemsSource is used in blueprint |
iconsMap | △ | object | {} | key-value object for IconComponent definition. ※ required if icon is used in blueprint |
extraWidgetMap | object | {} | key-value object for extra Widget(component) |
initialize example:
import { initialize } from '@mobx-json/mui-form';
initialize({
locale: 'zh',
messages,
itemsSource,
iconsMap,
extraWidgetMap
});
itemsSource example:
export default {
City: [
{
label: '東京都',
value: 'tokyo',
},
{
label: '神奈川県',
value: 'kanagawa',
},
{
label: '千葉県',
value: 'chiba',
},
],
Sex: [
{
label: 'Male',
value: 0,
},
{
label: 'Female',
value: 1,
},
{
label: 'Other',
value: -1,
},
],
}
iconsMap example:
import AccountCircle from '@material-ui/icons/AccountCircle';
import ContactMail from '@material-ui/icons/ContactMail';
export default {
AccountCircle,
ContactMail,
};
extraWidgetMap example:
import MyTextField from './MyTextField';
export default {
AdvancedTextField: MyTextField,
};
Use hook useMuiJsonForm to generate form.
example:
import { useMuiJsonForm } from '@mobx-json/mui-form';
const { form, submitWithCheck, setData, setBlueprint } = useMuiJsonForm({
blueprint,
formUniqName,
data,
});
// load data after mounted
React.useEffect(async () => {
const dataFromServer = await readDataFromServer();
setData(dataFromServer);
}, []);
// save to server if validation pass
const handleSubmit = React.useCallback(async () => {
const submitData = submitWithCheck();
if (submitData) {
await saveDataToServer(submitData);
}
}, [submitWithCheck]);
Parameter | Required | Type | Default | Description |
---|---|---|---|---|
blueprint | 〇 | object | blueprint for rendering form. See details. | |
formUniqName | △ | string | '' | necessary if define multiple forms in one page, or auto focus when errors occur will fail. |
data | object | {} | data for form in initialization | |
options | object | { smoothScroll: true, gridContainerProps: {} } | 1. smoothScroll: if true, append style scroll-behavior:smooth; to the lastest overflow: auto; component. 2. gridContainerProps: props appled to root grid container |
useMuiJsonForm returns an object contains following parameters:
key | Type | Description |
---|---|---|
form | React.Element | React form generated by blueprint |
submitWithCheck | Function | validate all inputs, if ok, returns inputed data; if not, returns false and automatically focus the first error field. |
setData | Function | apply data object to form |
setBlueprint | Function | apply a new blueprint. ※ re-render all fields! |
This project is licensed under the terms of the MIT license.
FAQs
create material-ui forms by json & mobx
The npm package @mobx-json/mui-form receives a total of 2 weekly downloads. As such, @mobx-json/mui-form popularity was classified as not popular.
We found that @mobx-json/mui-form demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.