项目作者: inovando

项目描述 :
:atom: Very opinionated reusable components (some binded to final-form exclusively) https://www.npmjs.com/package/@inovando/react-components
高级语言: JavaScript
项目地址: git://github.com/inovando/react-components.git
创建时间: 2020-07-15T19:05:02Z
项目社区:https://github.com/inovando/react-components

开源协议:

下载


@inovando/react-components

:atom: Very opinionated reusable components (some binded to final-form exclusively)

@inovando/react-components"">NPM code style: prettier

Demo

Install

  1. npm install --save @inovando/react-components
  2. # or
  3. yarn add @inovando/react-components

Usage

<Upload ></Upload>

Upload Component Demo

  1. import React, { Component } from "react";
  2. import { Upload } from "@inovando/react-components";
  3. import "@inovando/react-components/dist/index.css";
  4. const App = () => {
  5. const [files, setFiles] = useState([]);
  6. return (
  7. <Upload
  8. onChange={(files) => {
  9. setFiles(files);
  10. }}
  11. value={files}
  12. // This overrides "locale" prop
  13. label="Arraste arquivos ou clique aqui para fazer upload"
  14. // Only "pt" or "en" yet, defaults to "pt"
  15. locale="pt"
  16. // Accept file type range (Optional)
  17. // https://react-dropzone.js.org/#section-accepting-specific-file-types
  18. accept="image/*"
  19. // in mb (Optional)
  20. maxSize={3}
  21. // custom style for container
  22. style={{ margin: '0 auto', maxWidth: 400 }}
  23. />
  24. );
  25. };

Final-Form Adapters

Install final-form

  1. npm install --save final-form react-final-form
  2. # or
  3. yarn add final-form react-final-form

<UploadField ></UploadField> (based on React Dropzone)

Check out “initialValues” example

  1. import React from 'react';
  2. import { Form, Field } from 'react-final-form';
  3. import { UploadField } from '@inovando/react-components';
  4. import '@inovando/react-components/dist/index.css';
  5. function ExampleForm() {
  6. const onSubmit = values => {
  7. console.log('values:', values)
  8. }
  9. return (
  10. <Form
  11. onSubmit={onSubmit}
  12. initialValues={{
  13. files: [],
  14. }}
  15. render={({ handleSubmit, submitting }) => (
  16. <form onSubmit={handleSubmit} noValidate>
  17. <Field
  18. name="files"
  19. component={UploadField}
  20. label="Arraste arquivos ou clique aqui para fazer upload"
  21. validate={(value) =>
  22. value.length ? undefined : 'Campo obrigatório'
  23. }
  24. />
  25. <button disabled={submitting} type="submit">
  26. submit
  27. </button>
  28. </form>
  29. )}
  30. />
  31. );
  32. }

<MoneyField ></MoneyField> (based on React Number Format)

Check out “initialValues” example

  1. import React from 'react';
  2. import { Form, Field } from 'react-final-form';
  3. import { MoneyField } from '@inovando/react-components';
  4. import '@inovando/react-components/dist/index.css';
  5. function ExampleForm() {
  6. const onSubmit = values => {
  7. console.log('values:', values)
  8. }
  9. return (
  10. <Form
  11. onSubmit={onSubmit}
  12. initialValues={{
  13. money: 0.5,
  14. }}
  15. render={({ handleSubmit, submitting }) => (
  16. <form onSubmit={handleSubmit} noValidate>
  17. <Field
  18. name="money"
  19. component={MoneyField}
  20. fullWidth
  21. margin="normal"
  22. variant="outlined"
  23. label="Valor (R$)"
  24. validate={(value) => (value ? undefined : 'Campo obrigatório')}
  25. />
  26. <button disabled={submitting} type="submit">
  27. submit
  28. </button>
  29. </form>
  30. )}
  31. />
  32. );
  33. }

<AutocompleteField ></AutocompleteField> (based on Material-UI Autocomplete)

Check out “initialValues” example

  1. import React from 'react';
  2. import { Form, Field } from 'react-final-form';
  3. import { AutocompleteField } from '@inovando/react-components';
  4. import '@inovando/react-components/dist/index.css';
  5. const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
  6. function ExampleForm() {
  7. const [loading, setLoading] = useState(false);
  8. const onSubmit = values => {
  9. console.log('values:', values)
  10. }
  11. return (
  12. <Form
  13. onSubmit={onSubmit}
  14. render={({ handleSubmit, submitting }) => (
  15. <form onSubmit={handleSubmit} noValidate>
  16. <Field
  17. name="state"
  18. component={AutocompleteField}
  19. fullWidth
  20. margin="normal"
  21. variant="outlined"
  22. label="Estado"
  23. options={[
  24. { label: 'Paraná', value: 'PR'},
  25. // ...
  26. ]}
  27. handleChange={(value) => {
  28. console.log('value:', value);
  29. }}
  30. onSearch={async (text) => {
  31. setLoading(true);
  32. await sleep(1000);
  33. setLoading(false);
  34. }}
  35. kind="value" // expects "initialValue" as anything but an object, defaults to "object"
  36. delay={500} // "onSearch" debounce delay in ms, defaults to 250
  37. loading={loading}
  38. validate={(value) => (value ? undefined : 'Campo obrigatório')}
  39. />
  40. <button disabled={submitting} type="submit">
  41. submit
  42. </button>
  43. </form>
  44. )}
  45. />
  46. );
  47. }

<DateField ></DateField> (based on Material-UI Pickers)

Check out “initialValues” example

  1. import React from 'react';
  2. import { Form, Field } from 'react-final-form';
  3. import { DateField } from '@inovando/react-components';
  4. import '@inovando/react-components/dist/index.css';
  5. // Material-UI Pickers Dependencies
  6. import { MuiPickersUtilsProvider } from '@material-ui/pickers';
  7. import DateFnsUtils from '@date-io/date-fns';
  8. import ptLocale from 'date-fns/locale/pt';
  9. const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
  10. function ExampleForm() {
  11. const [loading, setLoading] = useState(false);
  12. const onSubmit = values => {
  13. console.log('values:', values)
  14. }
  15. return (
  16. // this tag can be at your global scope, such as "App.js"
  17. <MuiPickersUtilsProvider utils={DateFnsUtils} locale={ptLocale}>
  18. <Form
  19. onSubmit={onSubmit}
  20. render={({ handleSubmit, submitting }) => (
  21. <form onSubmit={handleSubmit} noValidate>
  22. <Field
  23. name="dataInicio"
  24. component={DateField}
  25. minDate={new Date()} // final-form validation logic must be at "validate" prop
  26. fullWidth
  27. margin="normal"
  28. variant="outlined"
  29. label="Data de início"
  30. validate={(value) =>
  31. value && isBefore(subDays(new Date(), 1), parseISO(value))
  32. ? undefined
  33. : 'Campo inválido'
  34. }
  35. />
  36. <button disabled={submitting} type="submit">
  37. submit
  38. </button>
  39. </form>
  40. )}
  41. />
  42. </MuiPickersUtilsProvider>
  43. );
  44. }

License

MIT © inovando