Skip to content

Commit

Permalink
add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
devarthurribeiro committed Sep 3, 2018
1 parent 1b761cf commit af84d7a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 7,176 deletions.
5 changes: 5 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src
examples
.babelrc
.gitignore
webpack.config.js
80 changes: 80 additions & 0 deletions dist/components/ViaCep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _propTypes = require("prop-types");

var _propTypes2 = _interopRequireDefault(_propTypes);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ViaCep = function (_React$Component) {
_inherits(ViaCep, _React$Component);

function ViaCep(props) {
_classCallCheck(this, ViaCep);

var _this = _possibleConstructorReturn(this, (ViaCep.__proto__ || Object.getPrototypeOf(ViaCep)).call(this, props));

_this.state = {
data: null,
loading: false,
error: false
};
_this.getCep = _this.getCep.bind(_this);
return _this;
}

_createClass(ViaCep, [{
key: "componentDidMount",
value: function componentDidMount() {
if (this.props.lazy) {
return;
}
this.getCep();
}
}, {
key: "getCep",
value: function getCep() {
var _this2 = this;

this.setState({ loading: true });
fetch("https://viacep.com.br/ws/" + this.props.cep + "/json/").then(function (response) {
return response.json();
}).then(function (data) {
_this2.setState({ data: data, loading: false });
_this2.props.onSuccess(data);
}).catch(function (err) {
_this2.setState({ error: true, loading: false });
});
}
}, {
key: "render",
value: function render() {
return this.props.children({
loading: this.state.loading,
data: this.state.data,
error: this.state.error,
fetch: this.getCep
}) || null;
}
}]);

return ViaCep;
}(_react2.default.Component);

exports.default = ViaCep;
13 changes: 13 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

var _ViaCep = require('./components/ViaCep');

var _ViaCep2 = _interopRequireDefault(_ViaCep);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

exports.default = _ViaCep2.default;
49 changes: 49 additions & 0 deletions examples/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import ViaCep from '../../dist';

class App extends React.Component {
constructor(props) {
super(props);
this.state = { cep: '' };

this.handleChangeCep = this.handleChangeCep.bind(this);
this.handleSuccess = this.handleSuccess.bind(this);
}
handleChangeCep(evt) {
this.setState({ cep: evt.target.value })
}
handleSuccess(cepData) {
console.log(cepData);
}
render() {
return (
<div className="App">
<ViaCep cep={this.state.cep} onSuccess={this.handleSuccess} lazy>
{ ({ data, loading, error, fetch }) => {
if (loading) {
return <p>loading...</p>
}
if (error) {
return <p>error</p>
}
if (data) {
return <div>
<p>
CEP: {data.cep} <br/>
CIDADE: {data.localidade} <br/>
UF: {data.uf} <br/>
</p>
</div>
}
return <div>
<input onChange={this.handleChangeCep} value={this.state.cep} placeholder="CEP" type="text"/>
<button onClick={fetch}>Pesquisar</button>
</div>
}}
</ViaCep>
</div>
);
}
}

export default App;
13 changes: 13 additions & 0 deletions examples/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<title>Via cep Demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
</body>
</html>
6 changes: 6 additions & 0 deletions examples/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';
import { render } from 'react-dom';

import App from './App';

render(<App />, document.getElementById('root'));
Loading

0 comments on commit af84d7a

Please sign in to comment.