UNPKG

@sanity/form-builder

Version:
55 lines (54 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireDefault(require("react")); var _convertPath = require("../utils/convertPath"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } /** * An example of how to sync focus path through document.location.hash * */ function getHash() { return decodeURIComponent(document.location.hash.substring(1)); } function getPathFromHash() { var hash = getHash(); return hash ? (0, _convertPath.toFormBuilder)(hash) : []; } class HashFocusManager extends _react.default.Component { constructor() { super(...arguments); _defineProperty(this, "state", { focusPath: getPathFromHash() }); _defineProperty(this, "handleHashChange", () => { this.setState({ focusPath: getPathFromHash() }); }); _defineProperty(this, "handleFocus", focusPath => { document.location.hash = (0, _convertPath.toGradient)(focusPath); }); _defineProperty(this, "handleBlur", () => { // this.setState({focusPath: []}) }); } componentDidMount() { window.addEventListener('hashchange', this.handleHashChange, false); } componentWillUnmount() { window.removeEventListener('hashchange', this.handleHashChange, false); } render() { return this.props.children({ onBlur: this.handleBlur, onFocus: this.handleFocus, focusPath: this.state.focusPath }); } } exports.default = HashFocusManager;