-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b761cf
commit af84d7a
Showing
7 changed files
with
166 additions
and
7,176 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
src | ||
examples | ||
.babelrc | ||
.gitignore | ||
webpack.config.js |
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,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; |
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,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; |
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,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; |
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,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> |
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,6 @@ | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
|
||
import App from './App'; | ||
|
||
render(<App />, document.getElementById('root')); |
Oops, something went wrong.