5.7.8 • Published 6 months ago

material-ui-pack v5.7.8

Weekly downloads
40
License
MIT
Repository
github
Last release
6 months ago

material-ui-pack

DEMO HERE https://material-ui-pack.xyz

npm version

material-ui-pack.xyz

Why? Because I love the Material UI api but <TextField name="phone" phone> is nicer than <TextField variant="outlined" size="medium" value={state.phone} label="Phone" onChange={handlePhoneFormatAndSetState}/> when you manage a lot of forms and many projects.

Install

# use with mui v5
npm i material-ui-pack@5

# use with mui v4
npm i material-ui-pack@4

Form

Pass your own state and setState into the surrounding Form component.

PropertyDescription
stateReact useState to contain all your form values.
setStateFrom React useState
onSubmitCallback function to fire when form is submitted
busyBoolean used to disable inputs and submit button after your form is submitted
marginDefault for TextField components "normal" | "none" | "dense"
sizeDefault for TextField components "small" | "medium"
debugDisplay state values stringified
import React from "react"
import { Form, SubmitButton, TextField } from "material-ui-pack"

export default function App() {
  const [state, setState] = React.useState({
    email: "",
    password: "",
  })
  const [busy, setBusy] = React.useState(false)
  function handleSubmit() {
    setBusy(true)
    // do something with state here
    ...
    setBusy(false)
  }
  return (
    <Form state={state} setState={setState} onSubmit={handleSubmit} busy={busy}>
      <TextField name="email" type="email" />
      <TextField name="password" fomatter="password" />
      <SubmitButton>Submit</SubmitButton>
    </Form>
  )
}

useForm Hook

You can optionally use this React hook to compose your own custom form components.

import React from "react"
import { useForm } from "./FormProvider"

export function CustomFormInput(props) {
  const { formProps, getValue, setValue } = useForm<FormData>()
  return (
    <input
      type="text"
      disabled={formProps.busy}
      value={getValue(props.name)}
      onChange={e => setValue(props.name, e.currentTarget.value)}
    />
  )
}

SubmitButton

example

No need to set type="submit" or an onClick action.

<SubmitButton>Submit</SubmitButton>

TextField

example

The wrapped TextField will default with variant="outlined" and you can skip properties such as label, value, and onChange. Labels are automtically derived from the name property name="firstName" will be labeled First Name.

PropertyDescription
nameString key name from your state object, can also be an object path like address.street1 or alternateEmails[0].email. It uses lodash to get/set values in your state.
lowercaseAll lowercase formatting
passwordHidden text with toggle button to view
newPasswordDisplay warning if it is too short or does not contain upper, lower, and numeric characters
phoneStandard 10 digit phone formatting
capitalizeAuto capitcalize words
// labeled "First Name"
<TextField name="firstName" />

// supports object paths in the name (uses lodash.get() and lodash.set())
<TextField name="alternateEmails[0]" label="Alternate Email"/>

<TextField name="email" formatter="lowercase" />
<TextField name="password" formatter="password" />
<TextField name="newPassword" formatter="newPassword" />
<TextField name="phone" formatter="phone" />
<TextField name="capitalizedWords" formatter="capitalize" />

CurrencyField

example

<CurrencyField name="price" />

PercentageField

example

<PercentageField name="taxRate" decimals={3} />

NumberField

<NumberField name="myNumber" decimals={1} />

Checkbox

example

// auto labeled "A Checkbox"
<Checkbox name="aCheckbox" />

SelectCombo

example

A drop-down/input combo box. User can enter or select options.

PropertyDescription
optionsArray of objects containing value and label
<SelectCombo
  options={[
    { value: "blue", label: "Blue" },
    { value: "red", label: "Red" },
  ]}
  name="favoriteColor"
/>

SelectCountry

example

This component uses i18n-iso-countries to build a full list of countries for your address forms. Values are set as ISO 3166-1 alpha-3, but support alpha-2 also.

PropertyDescription
isoType"isoAlpha2" | "isoAlpha3"
<SelectCountry name="country" />

SelectRegion

example

This component uses country-region-data to build a full list of states/provinces with the props value country.

PropertyDescription
countryTo control which regions are loaded.
countryIsoType"isoAlpha3" | "isoAlpha2"
<SelectRegion country="USA" name="stateOrProvince" />

SelectTimeZone

example

PropertyDescription
countryTo optionall filter the zones.
countryIsoType"isoAlpha3" | "isoAlpha2"
<SelectTimeZone name="timeZone" />

Select

example

This is a simple native drop-down select component.

PropertyDescription
optionsArray of objects containing (string | numnber) valueand(string) label
<Select
  name="color"
  options={[
    { value: "blue", label: "Blue" },
    { value: "red", label: "Red" },
  ]}
/>

DateTimePicker

example

This component uses @material-ui/pickers and sets values in ISO 8601 format.

<DateTimePicker name="dateAndTime" />

DatePicker

This component uses @material-ui/pickers and sets values in "yyyy-mm-dd" format.

<DatePicker name="date" />

TimePicker

This component uses @material-ui/pickers and sets values in "hh:mm:ss" format.

<TimePicker name="time" />

ResponsiveTable

import React from "react"
import { ResponsiveTable } from "material-ui-pack"

export function ResponsiveTableExample() {
  return (
    <ResponsiveTable
      onSelectChange={film => {
        console.log(film)
      }}
      onEdit={film => alert(`Edit ${film.title}`)}
      onDelete={film => alert(`Delete ${film.title}`)}
      rowData={[
        { id: 1, title: "Star Wars", category: "Sci-Fi" },
        { id: 2, title: "The Shining", category: "Horror" },
      ]}
      schema={[
        {
          label: "Title",
          render: function(item) {
            return item.title
          },
        },
        {
          xsDownHidden: true,
          label: "Category",
          render: function(item) {
            return item.category
          },
        },
      ]}
    />
  )
}

Dark Mode

example

Allow useDarkMode and DarkModeProvider to manage dark mode state. User can select dark mode in their operating system OR using the DarkModeToggle switch component. It sets a storage property to remember the user's preference.

import React from "react"
import { ThemeProvider, CssBaseline } from "@material-ui/core"
import { useDarkMode, DarkModeProvider } from "material-ui-pack/dist/DarkModeProvider"
import DarkModeToggle from "material-ui-pack/dist/DarkModeToggle"

function ThemedContent() {
  const { createMuiThemeWithDarkMode } = useDarkMode()
  const theme = createMuiThemeWithDarkMode({
    palette: {
      primary: {
        main: "#db544c",
      },
    },
  })
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <DarkModeToggle />
    </ThemeProvider>
  )
}

export default function App() {
  return (
    <DarkModeProvider>
      <ThemedContent />
    </DarkModeProvider>
  )

Date & Time Display

DisplayDateTime

<DisplayDateTime
  iso8601="2020-02-21T06:15:06.149Z"
  timeZone="America/New_York"
  fromNow
/>

DisplayDate

<DisplayDate ymd="1978-09-22" fromNow />

formatDateTime

formatDateTime("2020-02-21T06:15:06.149Z", "America/Chicago")

formatDate

formatDate("1978-09-22")

Hooks

useCounter

import IconButton from "@material-ui/core/IconButton"
import Add from "@material-ui/icons/Add"
import Remove from "@material-ui/icons/Remove"
import React from "react"
import useCounter from "../hooks/useCounter"

export function UseCounterExample() {
  const [count, increase, decrease] = useCounter(100)
  return (
    <>
      {count}
      <IconButton onClick={increase}>
        <Add />
      </IconButton>
      <IconButton onClick={decrease}>
        <Remove />
      </IconButton>
    </>
  )
}

useEscapeKey

import { Collapse } from "@material-ui/core"
import React from "react"
import useEscapeKey from "../hooks/useEscapeKey"

export function UseEscapeKeyExample() {
  const [isOpen, setIsOpen] = React.useState(true)

  useEscapeKey(
    React.useCallback(() => {
      setIsOpen(false)
    }, [])
  )

  return (
    <Collapse in={isOpen}>
      <h1>ESC to hide this</h1>
    </Collapse>
  )
}

useHandleState

import { Button, Collapse } from "@material-ui/core"
import React from "react"
import useHandleState from "../hooks/useHandleState"

export function UseHandleStateExample() {
  const [isOpen, handleIsOpen, setIsOpen] = useHandleState(true)

  return (
    <Collapse in={isOpen}>
      <Button onClick={() => setIsOpen(false)}>Hide with Setter</Button>
      <Button onClick={handleIsOpen(false)}>Hide with Handler</Button>
    </Collapse>
  )
}
5.7.8

6 months ago

5.7.7

6 months ago

5.6.8

7 months ago

5.6.7

7 months ago

5.6.6

7 months ago

5.6.5

7 months ago

5.6.4

7 months ago

5.6.3

7 months ago

5.6.2

7 months ago

5.6.0

7 months ago

5.7.6

7 months ago

5.7.5

7 months ago

5.7.4

7 months ago

5.7.3

7 months ago

5.7.2

7 months ago

5.7.1

7 months ago

5.7.0

7 months ago

5.5.8

8 months ago

5.5.7

8 months ago

5.5.6

8 months ago

5.5.5

8 months ago

5.5.4

8 months ago

5.5.3

8 months ago

5.5.2

8 months ago

5.5.1

8 months ago

5.5.0

8 months ago

5.4.3

9 months ago

5.4.2

9 months ago

5.4.1

9 months ago

5.4.0

9 months ago

5.3.1

2 years ago

5.3.0

2 years ago

4.0.7

2 years ago

5.2.5

2 years ago

5.2.4

2 years ago

5.2.3

2 years ago

4.0.5

2 years ago

4.0.4

2 years ago

4.0.6

2 years ago

4.0.3

2 years ago

5.1.6

2 years ago

5.2.2

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

4.0.2

2 years ago

5.1.5

2 years ago

5.1.4

3 years ago

5.1.3

3 years ago

5.1.2

3 years ago

4.0.1

3 years ago

5.0.5

3 years ago

5.0.4

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

4.0.0

3 years ago

5.0.0

3 years ago

2.14.12

3 years ago

2.14.13

3 years ago

2.14.11

3 years ago

2.14.9

3 years ago

2.14.7

3 years ago

2.14.8

3 years ago

2.14.5

3 years ago

2.14.6

3 years ago

2.14.3

3 years ago

2.14.4

3 years ago

2.14.10

3 years ago

2.14.1

3 years ago

2.14.2

3 years ago

2.14.0

3 years ago

2.13.0

3 years ago

2.11.4

3 years ago

2.11.5

3 years ago

2.12.0

3 years ago

2.11.0

3 years ago

2.11.1

3 years ago

2.9.2

3 years ago

2.11.2

3 years ago

2.11.3

3 years ago

2.10.1

3 years ago

2.10.0

3 years ago

2.9.1

3 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.7.3

3 years ago

2.4.0

3 years ago

2.3.0

3 years ago

2.7.0

3 years ago

2.7.2

3 years ago

2.7.1

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.2

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

0.2.41

3 years ago

0.2.40

3 years ago

0.2.42

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.2.39

3 years ago

1.0.2

4 years ago

0.2.38

4 years ago

0.2.37

4 years ago

0.2.36

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.35

4 years ago

0.2.34

4 years ago

0.2.33

4 years ago

0.2.32

4 years ago

0.2.31

4 years ago

0.2.29

4 years ago

0.2.28

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.1.27

4 years ago

0.1.24

4 years ago

0.1.25

4 years ago

0.1.26

4 years ago

0.2.0

4 years ago

0.1.21

4 years ago

0.1.22

4 years ago

0.1.23

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.17

4 years ago

0.1.16

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.13

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago