code
stringlengths 24
2.07M
| docstring
stringlengths 25
85.3k
| func_name
stringlengths 1
92
| language
stringclasses 1
value | repo
stringlengths 5
64
| path
stringlengths 4
172
| url
stringlengths 44
218
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
static resolveUrl(url, baseUrl) {
url = trim(url);
if (!url) return null;
if (startsWith(url, '#')) return null;
const { protocol } = parse(url);
if (includes(['http:', 'https:'], protocol)) {
return url.split('#')[0];
} else if (!protocol) { // eslint-disable-line no-else-return
return resolve(baseUrl, url).split('#')[0];
}
return null;
}
|
@param {!string} url
@param {!string} baseUrl
@return {!string}
|
resolveUrl
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static escapeQuotes(value, separator = ',') {
if (value === null || value === undefined) return '';
const regExp = new RegExp(`["${separator}\\r\\n]`);
if (regExp.test(value)) return `"${value.replace(/"/g, '""')}"`;
return value;
}
|
@param {!string} value
@param {!string=} separator
@return {!string}
|
escapeQuotes
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static getRobotsUrl(url) {
const { protocol, host } = parse(url);
return format({ protocol, host, pathname: '/robots.txt' });
}
|
@param {!string} url
@return {!string}
@private
|
getRobotsUrl
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static lowerBound(array, value, comp) {
let first = 0;
let count = array.length;
while (count > 0) {
const step = (count / 2) | 0;
let it = first + step;
if (comp(array[it], value) <= 0) {
it += 1;
first = it;
count -= step + 1;
} else {
count = step;
}
}
return first;
}
|
@param {!string} url
@return {!string}
@private
|
lowerBound
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static checkDomainMatch(domains, hostname) {
return some(domains, domain => {
if (isRegExp(domain)) return domain.test(hostname);
return domain === hostname;
});
}
|
@param {!Array<!string|RegExp>} domains
@param {!string} hostname
@return {!boolean}
|
checkDomainMatch
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static getSitemapUrls(sitemapXml) {
const urls = [];
sitemapXml.replace(/<loc>([^<]+)<\/loc>/g, (_, url) => {
const unescapedUrl = Helper.unescape(url);
urls.push(unescapedUrl);
return null;
});
return urls;
}
|
@param {!string} sitemapXml
@return {!Array<!string>}
|
getSitemapUrls
|
javascript
|
yujiosaka/headless-chrome-crawler
|
lib/helper.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/lib/helper.js
|
MIT
|
static run(port) {
const server = new Server(port);
return new Promise(resolve => void server._server.once('listening', () => {
resolve(server);
}));
}
|
@param {!number} port
@return {!Promise<!Server>}
|
run
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
setAuth(path, username, password) {
this._auths.set(path, { username, password });
}
|
@param {!string} path
@param {!string} username
@param {!string} password
|
setAuth
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
setCSP(path, csp) {
this._csps.set(path, csp);
}
|
@param {string} path
@param {string} csp
|
setCSP
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
setContent(path, content) {
this._contents.set(path, content);
}
|
@param {!string} path
@param {!string} content
|
setContent
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
setResponseDelay(path, delay) {
this._delays.set(path, delay);
}
|
@param {!string} path
@param {!number} delay
|
setResponseDelay
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
setRedirect(from, to) {
this._routes.set(from, (request, response) => {
response.writeHead(302, { location: to });
response.end();
});
}
|
@param {string} from
@param {string} to
|
setRedirect
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
_onRequest(request, response) {
this._handleError(request, response);
const { path } = parse(request.url);
response.setHeader('Content-Type', mime.getType(path));
const auth = this._auths.get(path);
if (!this._authenticate(auth, request)) {
response.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Secure Area"' });
response.end('HTTP Error 401 Unauthorized: Access is denied');
return;
}
const delay = this._delays.get(path) || 0;
setTimeout(() => {
const route = this._routes.get(path);
if (route) {
route(request, response);
return;
}
const content = this._contents.get(path);
if (content) {
response.end(content);
return;
}
response.end(path);
}, delay);
}
|
@param {!IncomingMessage} request
@param {!ServerResponse} response
@private
|
_onRequest
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
_handleError(request, response) {
request.on('error', error => {
if (error.code === 'ECONNRESET') {
response.end();
return;
}
throw error;
});
}
|
@param {!IncomingMessage} request
@param {!ServerResponse} response
@private
|
_handleError
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
_authenticate(auth, request) {
if (!auth) return true;
const credentials = this._getCredentials(request.headers.authorization);
if (credentials === `${auth.username}:${auth.password}`) return true;
return false;
}
|
@param {!{username:string, password:string}} auth
@param {!IncomingMessage} request
@return {!boolean}
@private
|
_authenticate
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
_getCredentials(authorization = '') {
const credentials = authorization.split(' ')[1] || '';
return Buffer.from(credentials, 'base64').toString();
}
|
@param {!string=} authorization
@return {!string}
@private
|
_getCredentials
|
javascript
|
yujiosaka/headless-chrome-crawler
|
test/server/index.js
|
https://github.com/yujiosaka/headless-chrome-crawler/blob/master/test/server/index.js
|
MIT
|
function getParameterByName(name) {
const parsedName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
const regex = new RegExp(`[\\?&]${parsedName}=([^&#]*)`);
const results = regex.exec(location.hash);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
}
|
Get a URL query parameter from the hash.
|
getParameterByName
|
javascript
|
nikgraf/belle
|
docs/theme/initialize.js
|
https://github.com/nikgraf/belle/blob/master/docs/theme/initialize.js
|
MIT
|
function updateStructure(targetObject, object) {
for (const componentName in object) {
if (object.hasOwnProperty(componentName)) {
for (const styleName in object[componentName]) {
if (object[componentName].hasOwnProperty(styleName)) {
targetObject[componentName][styleName] = object[componentName][styleName]; // eslint-disable-line no-param-reassign
}
}
}
}
}
|
Updates the deepest structure while keeping the original reference of the outer objects.
|
updateStructure
|
javascript
|
nikgraf/belle
|
docs/theme/initialize.js
|
https://github.com/nikgraf/belle/blob/master/docs/theme/initialize.js
|
MIT
|
function initializeTheme() {
const theme = getParameterByName('theme');
if (theme === 'bootstrap') {
updateStructure(belle.style, bootstrap3Theme.style);
updateStructure(belle.config, bootstrap3Theme.config);
} else if (theme === 'belle-with-default-focus') {
updateStructure(belle.style, belleWithClassicFocusTheme.style);
updateStructure(belle.config, belleWithClassicFocusTheme.config);
}
}
|
Updates the deepest structure while keeping the original reference of the outer objects.
|
initializeTheme
|
javascript
|
nikgraf/belle
|
docs/theme/initialize.js
|
https://github.com/nikgraf/belle/blob/master/docs/theme/initialize.js
|
MIT
|
constructor(properties) {
super(properties);
this.childProps = sanitizeChildProps(properties);
}
|
ActionArea
The purpose of this component is to provide a button like behaviour for a
click like interaction within other components. Button can't be used in such
cases as it always will have it's own focus which is not desired in
components like DatePicker e.g. next month button.
Note: Use the ActionArea's onUpdate instead of onClick as otherwise on iOS9
the ActionArea will trigger onFocus for it's parent with a set tabindex.
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/ActionArea.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ActionArea.js
|
MIT
|
componentWillReceiveProps(properties) {
this.childProps = sanitizeChildProps(properties);
}
|
Update the childProps based on the updated properties passed to the card.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/ActionArea.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ActionArea.js
|
MIT
|
render() {
let style = { ...actionAreaStyle.style, ...this.props.style };
if (this.state.isHovered) {
style = {
...style,
...actionAreaStyle.hoverStyle,
...this.props.hoverStyle,
};
}
if (this.state.isActive) {
style = {
...style,
...actionAreaStyle.activeStyle,
...this.props.activeStyle,
};
}
return (
<div
role="button"
{...this.childProps}
onMouseDown={ this._onMouseDown }
onMouseUp={ this._onMouseUp }
onMouseEnter={ this._onMouseEnter }
onMouseLeave={ this._onMouseLeave }
onTouchStart={ this._onTouchStart }
onTouchEnd={ this._onTouchEnd }
onTouchCancel={ this._onTouchCancel }
onClick={ this._onClick }
style={ style }
>
{ this.props.children }
</div>
);
}
|
Updates the button to be release and makes sure the next onMouseEnter is
ignored.
|
render
|
javascript
|
nikgraf/belle
|
src/components/ActionArea.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ActionArea.js
|
MIT
|
function sanitizeChildProps(properties) {
return omit(properties, Object.keys(buttonPropTypes));
}
|
Returns an object with properties that are relevant for the button element.
In case a wrong or no type is defined the type of the child button will be
set to `button`.
|
sanitizeChildProps
|
javascript
|
nikgraf/belle
|
src/components/Button.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Button.js
|
MIT
|
function updatePseudoClassStyle(styleId, properties, preventFocusStyleForTouchAndClick) {
const baseStyle = properties.primary ? buttonStyle.primaryStyle : buttonStyle.style;
const baseDisabledStyle = properties.primary ? buttonStyle.primaryDisabledStyle : buttonStyle.disabledStyle;
const disabledStyle = {
...baseStyle,
...properties.style,
...baseDisabledStyle,
...properties.disabledStyle,
};
const baseActiveStyle = properties.primary ? buttonStyle.primaryActiveStyle : buttonStyle.activeStyle;
const activeStyle = {
...baseActiveStyle,
...properties.activeStyle,
};
let focusStyle;
if (preventFocusStyleForTouchAndClick) {
focusStyle = { outline: 0 };
} else {
const baseFocusStyle = properties.primary ? buttonStyle.primaryFocusStyle : buttonStyle.focusStyle;
focusStyle = {
...baseFocusStyle,
...properties.focusStyle,
};
}
const styles = [
{
id: styleId,
style: activeStyle,
pseudoClass: 'active',
},
{
id: styleId,
style: disabledStyle,
pseudoClass: 'active',
disabled: true,
},
{
id: styleId,
style: focusStyle,
pseudoClass: 'focus',
},
];
injectStyles(styles);
}
|
Update hover, focus & active style for the speficied styleId.
@param styleId {string} - a unique id that exists as class attribute in the DOM
@param properties {object} - the components properties optionally containing custom styles
|
updatePseudoClassStyle
|
javascript
|
nikgraf/belle
|
src/components/Button.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Button.js
|
MIT
|
componentWillMount() {
this.styleId = `style-id${uniqueId()}`;
updatePseudoClassStyle(this.styleId, this.props, this.preventFocusStyleForTouchAndClick);
}
|
Generates the style-id & inject the focus & active style.
|
componentWillMount
|
javascript
|
nikgraf/belle
|
src/components/Button.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Button.js
|
MIT
|
componentWillReceiveProps(properties) {
this.preventFocusStyleForTouchAndClick = has(properties, 'preventFocusStyleForTouchAndClick') ? properties.preventFocusStyleForTouchAndClick : config.preventFocusStyleForTouchAndClick;
this.setState({
childProps: sanitizeChildProps(properties),
});
removeStyle(this.styleId);
updatePseudoClassStyle(this.styleId, properties, this.preventFocusStyleForTouchAndClick);
}
|
Update the childProps based on the updated properties of the button.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/Button.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Button.js
|
MIT
|
componentDidUpdate() {
this.focused = false;
this.mouseDownOnButton = false;
}
|
Deactivate the focused attribute in order to make sure the focus animation
only runs once when the component is focused on & not after re-rendering
e.g when the user clicks the button.
|
componentDidUpdate
|
javascript
|
nikgraf/belle
|
src/components/Button.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Button.js
|
MIT
|
constructor(properties) {
super(properties);
const { style, ...childProps } = properties; // eslint-disable-line no-unused-vars
this.childProps = childProps;
}
|
Card component with a light shadow.
This component will apply any attribute to the div that has been provided as
property & is valid for a div.
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/Card.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Card.js
|
MIT
|
componentWillReceiveProps(properties) {
const { style, ...childProps } = properties; // eslint-disable-line no-unused-vars
this.childProps = childProps;
}
|
Update the childProps based on the updated properties passed to the card.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/Card.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Card.js
|
MIT
|
render() {
const divStyle = { ...cardStyle.style, ...this.props.style };
return (
<div {...this.childProps} style={ divStyle }>
{ this.props.children }
</div>
);
}
|
Update the childProps based on the updated properties passed to the card.
|
render
|
javascript
|
nikgraf/belle
|
src/components/Card.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Card.js
|
MIT
|
function updatePseudoClassStyle(styleId, caretStyleId, properties) {
const hoverStyle = {
...style.hoverStyle,
...properties.hoverStyle,
};
const focusStyle = {
...style.focusStyle,
...properties.focusStyle,
};
const disabledHoverStyle = {
...style.disabledHoverStyle,
...properties.disabledHoverStyle,
};
const caretFocusStyle = {
...style.caretFocusStyle,
};
const styles = [
{
id: styleId,
style: hoverStyle,
pseudoClass: 'hover',
},
{
id: styleId,
style: disabledHoverStyle,
pseudoClass: 'hover',
disabled: true,
},
{
id: styleId,
style: focusStyle,
pseudoClass: 'focus',
},
{
id: caretStyleId,
style: caretFocusStyle,
pseudoClass: 'focus',
},
];
injectStyles(styles);
}
|
Update hover style for the specified styleId.
@param styleId {string} - a unique id that exists as class attribute in the DOM
@param caretStyleId {string} - unique is assigned as class to caret span
@param properties {object} - the components properties optionally containing hoverStyle
|
updatePseudoClassStyle
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
function sanitizeWrapperProps(properties) {
return omit(properties, [
'style',
'aria-label',
'aria-disabled',
]);
}
|
Returns an object with properties that are relevant for the wrapper div.
|
sanitizeWrapperProps
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
function sanitizeInputProps(properties) {
return omit(properties, Object.keys(comboBoxPropTypes));
}
|
Returns an object with properties that are relevant for the input box.
|
sanitizeInputProps
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
function sanitizeCaretProps(properties) {
return omit(properties, [
'style',
'className',
'onClick',
'tabIndex',
]);
}
|
Returns an object with properties that are relevant for the wrapping div of
the selected option.
|
sanitizeCaretProps
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
function sanitizeMenuProps(properties) {
return omit(properties, [
'style',
'ref',
'role',
]);
}
|
Returns an object with properties that are relevant for the combo-box menu.
|
sanitizeMenuProps
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
function filterFunc(inputValue, optionValue) {
if (inputValue && optionValue) {
return optionValue.toLowerCase().indexOf(inputValue.toLowerCase()) > -1;
}
return false;
}
|
Default function used for filtering options.
|
filterFunc
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_getHint() {
if (this.state.isOpen) {
const filteredOptions = this.filteredOptions;
if (filteredOptions && filteredOptions.length > 0) {
let hint;
const focusedOptionIndex = this.state.focusedOptionIndex;
const inputValue = this.state.inputValue;
if (focusedOptionIndex >= 0) {
hint = filteredOptions[focusedOptionIndex].props.value;
} else if (inputValue && inputValue.length > 0) {
hint = filteredOptions[0].props.value;
}
if (hint) {
if (inputValue && inputValue.length > 0) {
const position = hint.toLowerCase().indexOf(inputValue.toLowerCase());
if (position === 0) {
return inputValue + hint.substr(inputValue.length, (hint.length - inputValue.length));
} else if (position === -1) {
return hint;
}
} else {
return hint;
}
}
}
}
return undefined;
}
|
This method will calculate the hint that should be present in comboBox at some point in time. Rules:
1. If menu is not open hint is undefined
2. If menu is open but there are no filteredOptions hint is undefined
3. If if some option is highlighted hint is equal to its value
4. If no option is highlighted but some value is present in input box hint is equal to value of first filteredOptions
If user has typed some text in input box and there is a hint(according to above calculations), the starting of hint
is replaced by the text input by user ( this is to make sure that case of letters in hint is same as that in input box
value and overlap is perfect.)
todo: simplify logic in method below
|
_getHint
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
componentWillMount() {
const id = uniqueId();
this._styleId = `style-id${id}`;
this._caretStyleId = `caretStyle-id${id}`;
updatePseudoClassStyle(this._styleId, this._caretStyleId, this.props);
}
|
Generates the style-id & inject the focus & hover style.
|
componentWillMount
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
componentWillReceiveProps(properties) {
const newState = {
wrapperProps: sanitizeWrapperProps(properties.wrapperProps),
inputProps: sanitizeInputProps(properties),
caretProps: sanitizeCaretProps(properties.caretProps),
menuProps: sanitizeMenuProps(properties.menuProps),
};
if (has(properties, 'valueLink')) {
newState.inputValue = properties.valueLink.value || '';
} else if (has(properties, 'value')) {
newState.inputValue = properties.value || '';
}
if (newState.inputValue) {
newState.filteredOptions = ComboBox.filterOptions(newState.inputValue, properties);
}
this.setState(newState);
removeAllStyles([this._styleId, this._caretStyleId]);
updatePseudoClassStyle(this._styleId, this._caretStyleId, properties);
}
|
Generates the style-id & inject the focus & hover style.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
componentWillUnmount() {
removeAllStyles([this._styleId, this._caretStyleId]);
}
|
Remove a component's associated styles whenever it gets removed from the DOM.
|
componentWillUnmount
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_onArrowUpKeyDown() {
if (this.filteredOptions.length > 0) {
let index = this.filteredOptions.length - 1;
if (this.state.focusedOptionIndex) {
index = this.state.focusedOptionIndex - 1;
}
this.setState({
focusedOptionIndex: index,
});
}
}
|
Highlight previous option when arrowUp key is pressed.
Highlight last option if currently first option is focused.
|
_onArrowUpKeyDown
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_onEnterOrTabKeyDown() {
if (this.state.focusedOptionIndex >= 0) {
this._triggerChange(this.filteredOptions[this.state.focusedOptionIndex].props.value);
}
}
|
Update value of Input box to the value of highlighted option.
|
_onEnterOrTabKeyDown
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_findMatch(value) {
return find(this.filteredOptions, (entry) => entry.props.value === value);
}
|
The function will return options (if any) who's value is same as value of the combo-box input.
|
_findMatch
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_triggerChange(value) {
if (has(this.props, 'valueLink')) {
this.props.valueLink.requestChange(value);
this.setState({
isOpen: false,
focusedOptionIndex: undefined,
});
} else if (has(this.props, 'value')) {
this.setState({
isOpen: false,
focusedOptionIndex: undefined,
});
} else {
this.setState({
inputValue: value,
isOpen: false,
focusedOptionIndex: undefined,
});
this.filteredOptions = ComboBox.filterOptions(value, this.props);
}
const obj = { value, isOptionSelection: true, isMatchingOption: true };
const matchedOption = this._findMatch(value);
obj.identifier = matchedOption ? matchedOption.props.identifier : undefined;
if (this.props.onUpdate) {
this.props.onUpdate(obj);
}
}
|
The function is called when user selects an option. Function will do following:
1. Close the options
2. Change value of input depending on whether its has value, defaultValue or valueLink property
3. Call onUpdate props function
|
_triggerChange
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_getValueForIndex(index) {
return this.filteredOptions[index].props.value;
}
|
Returns the value of the child with a certain index.
|
_getValueForIndex
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
_userUpdateValue(value) {
if (has(this.props, 'valueLink')) {
this.props.valueLink.requestChange(value);
this.setState({
isOpen: true,
focusedOptionIndex: undefined,
});
} else if (has(this.props, 'value')) {
this.setState({
isOpen: true,
focusedOptionIndex: undefined,
});
} else {
this.setState({
inputValue: value,
isOpen: true,
focusedOptionIndex: undefined,
});
this.filteredOptions = ComboBox.filterOptions(value, this.props);
}
const obj = { value, isOptionSelection: false, isMatchingOption: false };
const matchedOption = this._findMatch(value);
if (matchedOption) {
obj.identifier = matchedOption.props.identifier;
obj.isMatchingOption = true;
}
if (this.props.onUpdate) {
this.props.onUpdate(obj);
}
}
|
The function is called when user inputs a value in the input box. This can be done by:
1. typing/ pasting value into input box
2. pressing arrowRight key when there is some hint in the input box
Function will do following:
1. Open the options
2. Change value of input depending on whether its has value, defaultValue or valueLink property
3. Call onUpdate props function
|
_userUpdateValue
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
static filterOptions(inputValue, properties) { /* eslint react/sort-comp:0*/
let filteredOptions = [];
if (!isEmpty(properties.children)) {
if (inputValue) {
filteredOptions = filterReactChildren(properties.children, (entry) => (
properties.filterFunc(inputValue, entry.props.value)
));
} else {
filteredOptions = getArrayForReactChildren(properties.children, (entry) => entry);
}
if (properties.maxOptions) {
filteredOptions = filteredOptions.splice(0, properties.maxOptions);
}
}
return filteredOptions;
}
|
Function to filter options using input value.
|
filterOptions
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
render() {
let inputStyle = {
...style.style,
...this.props.style,
};
const hintStyle = {
...style.hintStyle,
...this.props.hintStyle,
};
const wrapperStyle = {
...style.wrapperStyle,
...this.props.wrapperStyle,
};
const menuStyle = {
...style.menuStyle,
...this.props.menuStyle,
};
const hint = this.props.enableHint ? this._getHint() : undefined;
const placeHolder = !hint ? this.props.placeholder : undefined;
const inputClassName = unionClassNames(this.props.className, this._styleId);
const tabIndex = this.props.tabIndex ? this.props.tabIndex : '0';
if (this.props.disabled) {
inputStyle = {
...inputStyle,
...style.disabledStyle,
...this.props.disabledStyle,
};
}
// todo: Currently there are no different hover styles for caret, like select they are probably not really needed.
let caretStyle;
if (this.props.displayCaret) {
if (this.props.disabled) {
caretStyle = {
...style.caretToOpenStyle,
...this.props.caretToOpenStyle,
...style.disabledCaretToOpenStyle,
...this.props.disabledCaretToOpenStyle,
};
} else if (this.state.isOpen) {
caretStyle = {
...style.caretToCloseStyle,
...this.props.caretToCloseStyle,
};
} else {
caretStyle = {
...style.caretToOpenStyle,
...this.props.caretToOpenStyle,
};
}
}
const computedMenuStyle = (this.state.isOpen && !this.props.disabled && this.filteredOptions && this.filteredOptions.length > 0) ? menuStyle : { display: 'none' };
// using value for input makes it a controlled component and it will be changed in controlled manner if (1) user enters value, (2) user selects some option
// value will be updated depending on whether user has passed value / valueLink / defaultValue as property
return (
<div
style={ wrapperStyle }
aria-label = { this.props['aria-label'] }
aria-disabled = { this.props.disabled }
{...this.state.wrapperProps}
>
<input
style={ hintStyle }
value={ hint }
tabIndex = { -1 }
key="style-hint"
readOnly
/>
<input
disabled = { this.props.disabled }
aria-disabled = { this.props.disabled }
value={ this.state.inputValue }
placeholder={ placeHolder }
style={ inputStyle }
className={ inputClassName }
onChange={ this._onChange }
tabIndex={ tabIndex }
onBlur={ this._onBlur }
onFocus={ this._onFocus }
onKeyDown={ this._onKeyDown }
aria-autocomplete="list"
key="combo-input"
{...this.state.inputProps}
/>
<span
style={ caretStyle }
className = { this._caretStyleId }
onClick = { this._onCaretClick }
tabIndex = { -1 }
{...this.state.caretProps}
/>
<ul
style={ computedMenuStyle }
role="listbox"
aria-expanded={ this.state.isOpen }
{...this.state.menuProps}
>
{
React.Children.map(this.filteredOptions, (entry, index) => ((
<ComboBoxItem
key={ index }
index={ index }
onItemTouchStart={ this._onTouchStartAtOption }
onItemTouchEnd={ this._onTouchEndAtOption }
onItemTouchCancel={ this._onTouchCancelAtOption }
onItemClick={ this._onClickAtOption }
onItemMouseEnter={ this._onMouseEnterAtOption }
onItemMouseLeave={ this._onMouseLeaveAtOption }
>
{ entry }
</ComboBoxItem>
)))
}
</ul>
</div>
);
}
|
Function to filter options using input value.
|
render
|
javascript
|
nikgraf/belle
|
src/components/ComboBox.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBox.js
|
MIT
|
render() {
return (
<li
onClick={ this._onClick }
onMouseEnter={ this._onMouseEnter }
onMouseLeave={ this._onMouseLeave }
onMouseDown={ this._onMouseDown }
onTouchStart={ this._onTouchStart }
onTouchEnd={ this._onTouchEnd }
onTouchCancel={ this._onTouchCancel }
role="option"
>
{ this.props.children }
</li>
);
}
|
Belle internal component to wrap an Option in a ComboBox.
This component exists to avoid binding functions in JSX.
|
render
|
javascript
|
nikgraf/belle
|
src/components/ComboBoxItem.js
|
https://github.com/nikgraf/belle/blob/master/src/components/ComboBoxItem.js
|
MIT
|
function sanitizeWrapperProps(properties) {
return omit(properties, Object.keys(datePickerPropTypes));
}
|
Returns an object with properties that are relevant for the wrapping div of the date picker.
|
sanitizeWrapperProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeEmptyDayProps(properties) {
return omit(properties, [
'key',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeEmptyDayProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeDisabledDayProps(properties) {
return omit(properties, [
'key',
'onMouseEnter',
'onMouseLeave',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeDisabledDayProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeDayProps(properties) {
return omit(properties, [
'key',
'onMouseDown',
'onMouseUp',
'onMouseEnter',
'onMouseLeave',
'onTouchStart',
'onTouchEnd',
'onTouchCancel',
'aria-selected',
'style',
'role',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeDayProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeNavBarProps(properties) {
return omit(properties, [
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeNavBarProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizePrevMonthNavProps(properties) {
return omit(properties, [
'aria-label',
'className',
'onClick',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizePrevMonthNavProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizePrevMonthNavIconProps(properties) {
return omit(properties, [
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizePrevMonthNavIconProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeNextMonthNavProps(properties) {
return omit(properties, [
'aria-label',
'className',
'onClick',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeNextMonthNavProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeNextMonthNavIconProps(properties) {
return omit(properties, [
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeNextMonthNavIconProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeMonthLabelProps(properties) {
return omit(properties, [
'id',
'role',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeMonthLabelProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeDayLabelProps(properties) {
return omit(properties, [
'key',
'role',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeDayLabelProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeWeekHeaderProps(properties) {
return omit(properties, [
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeWeekHeaderProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function sanitizeWeekGridProps(properties) {
return omit(properties, [
'role',
'style',
]);
}
|
Returns an object with properties that are relevant for day span.
|
sanitizeWeekGridProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
function updatePseudoClassStyle(pseudoStyleIds, properties, preventFocusStyleForTouchAndClick) {
const styles = [
{
id: pseudoStyleIds.prevMonthNavStyleId,
style: {
...defaultStyle.hoverPrevMonthNavStyle,
...properties.hoverPrevMonthNavStyle,
},
pseudoClass: 'hover',
}, {
id: pseudoStyleIds.nextMonthNavStyleId,
style: {
...defaultStyle.hoverNextMonthNavStyle,
...properties.hoverNextMonthNavStyle,
},
pseudoClass: 'hover',
},
];
let focusStyle;
if (preventFocusStyleForTouchAndClick) {
focusStyle = { outline: 0 };
} else {
focusStyle = {
...defaultStyle.focusStyle,
...properties.focusStyle,
};
}
styles.push({
id: pseudoStyleIds.styleId,
style: focusStyle,
pseudoClass: 'focus',
});
injectStyles(styles);
}
|
Injects pseudo classes for styles into the DOM.
|
updatePseudoClassStyle
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
constructor(properties) {
super(properties);
let selectedDate;
let month;
let year;
if (has(properties, 'valueLink')) {
selectedDate = properties.valueLink.value;
} else if (has(properties, 'value')) {
selectedDate = properties.value;
} else if (has(properties, 'defaultValue')) {
selectedDate = properties.defaultValue;
}
if (properties.defaultMonth) {
month = properties.defaultMonth - 1;
} else if (selectedDate) {
month = selectedDate.getMonth();
} else {
month = today().getMonth();
}
if (properties.defaultYear) {
year = properties.defaultYear;
} else if (selectedDate) {
year = selectedDate.getFullYear();
} else {
year = today().getFullYear();
}
this.state = {
isFocused: false,
isActive: false,
selectedDate,
month,
year,
};
this.localeData = getLocaleData(properties.locale);
this.wrapperProps = sanitizeWrapperProps(properties);
this.dayProps = sanitizeDayProps(properties.dayProps);
this.disabledDayProps = sanitizeDisabledDayProps(properties.dayProps);
this.emptyDayProps = sanitizeEmptyDayProps(properties.dayProps);
this.navBarProps = sanitizeNavBarProps(properties.navBarProps);
this.prevMonthNavProps = sanitizePrevMonthNavProps(properties.prevMonthNavProps);
this.prevMonthNavIconProps = sanitizePrevMonthNavIconProps(properties.prevMonthNavIconProps);
this.nextMonthNavProps = sanitizeNextMonthNavProps(properties.nextMonthNavProps);
this.nextMonthNavIconProps = sanitizeNextMonthNavIconProps(properties.nextMonthNavIconProps);
this.monthLabelProps = sanitizeMonthLabelProps(properties.monthLabelProps);
this.dayLabelProps = sanitizeDayLabelProps(properties.dayLabelProps);
this.weekHeaderProps = sanitizeWeekHeaderProps(properties.weekHeaderProps);
this.weekGridProps = sanitizeWeekGridProps(properties.weekGridProps);
this.preventFocusStyleForTouchAndClick = has(properties, 'preventFocusStyleForTouchAndClick') ? properties.preventFocusStyleForTouchAndClick : config.preventFocusStyleForTouchAndClick;
}
|
DatePicker React Component.
This implementation follows the recommendations proposed here:
http://www.w3.org/TR/wai-aria-practices/#datepicker
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
componentWillMount() {
const id = uniqueId();
this.pseudoStyleIds = {};
this.pseudoStyleIds.styleId = `wrapper-style-id${id}`;
this.pseudoStyleIds.prevMonthNavStyleId = `prevMonthNav-style-id${id}`;
this.pseudoStyleIds.nextMonthNavStyleId = `nextMonthNav-style-id${id}`;
updatePseudoClassStyle(this.pseudoStyleIds, this.props, this.preventFocusStyleForTouchAndClick);
}
|
Generates the style-id based on React's unique DOM node id.
Calls function to inject the pseudo classes into the dom.
|
componentWillMount
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
componentWillReceiveProps(properties) {
const newState = {};
if (has(properties, 'valueLink')) {
newState.selectedDate = properties.valueLink.value;
} else if (has(properties, 'value')) {
newState.selectedDate = properties.value;
}
this.setState(newState);
this.localeData = getLocaleData(properties.locale);
this.wrapperProps = sanitizeWrapperProps(properties);
this.dayProps = sanitizeDayProps(properties.dayProps);
this.disabledDayProps = sanitizeDisabledDayProps(properties.dayProps);
this.emptyDayProps = sanitizeEmptyDayProps(properties.dayProps);
this.navBarProps = sanitizeNavBarProps(properties.navBarProps);
this.prevMonthNavProps = sanitizePrevMonthNavProps(properties.prevMonthNavProps);
this.prevMonthNavIconProps = sanitizePrevMonthNavIconProps(properties.prevMonthNavIconProps);
this.nextMonthNavProps = sanitizeNextMonthNavProps(properties.nextMonthNavProps);
this.nextMonthNavIconProps = sanitizeNextMonthNavIconProps(properties.nextMonthNavIconProps);
this.monthLabelProps = sanitizeMonthLabelProps(properties.monthLabelProps);
this.dayLabelProps = sanitizeDayLabelProps(properties.dayLabelProps);
this.weekHeaderProps = sanitizeWeekHeaderProps(properties.weekHeaderProps);
this.weekGridProps = sanitizeWeekGridProps(properties.weekGridProps);
this.preventFocusStyleForTouchAndClick = has(properties, 'preventFocusStyleForTouchAndClick') ? properties.preventFocusStyleForTouchAndClick : config.preventFocusStyleForTouchAndClick;
removeAllStyles(Object.keys(this.pseudoStyleIds));
updatePseudoClassStyle(this.pseudoStyleIds, properties, this.preventFocusStyleForTouchAndClick);
}
|
Function will update component state and styles as new props are received.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_triggerSelectDate(day, month, year) {
if (!this.props.disabled && !this.props.readOnly) {
const selectedDate = day ? new Date(year, month, day) : undefined;
if (has(this.props, 'valueLink')) {
this.props.valueLink.requestChange(selectedDate);
} else if (!has(this.props, 'value')) {
this.setState({
selectedDate,
month,
year,
});
}
if (this.props.onUpdate) {
this.props.onUpdate({
value: selectedDate,
});
}
}
}
|
Depending on whether component is controlled or uncontrolled the function will update this.state.selectedDate.
It will also call props.onUpdate.
|
_triggerSelectDate
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_triggerToggleDate(date) {
if (!this.props.disabled && !this.props.readOnly) {
let day;
let month;
let year;
if (this.state.selectedDate && date &&
this.state.selectedDate.getDate() === date.getDate() && this.state.selectedDate.getMonth() === date.getMonth() && this.state.selectedDate.getFullYear() === date.getFullYear()) {
day = undefined;
month = this.state.month;
year = this.state.year;
} else {
day = date.getDate();
month = date.getMonth();
year = date.getFullYear();
}
this._triggerSelectDate(day, month, year);
}
}
|
Function will select / deselect date passed to it, it is used in case of 'Space' keyDown on a day.
|
_triggerToggleDate
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_focusOnTheFistDayOfTheMonth() {
this.setState({
focusedDateKey: `${this.state.year}-${this.state.month + 1}-1`,
});
}
|
Function will select / deselect date passed to it, it is used in case of 'Space' keyDown on a day.
|
_focusOnTheFistDayOfTheMonth
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_focusOnFallbackDay() {
if (this.state.lastHoveredDay) {
this.setState({
focusedDateKey: this.state.lastHoveredDay,
});
} else {
this._focusOnTheFistDayOfTheMonth();
}
}
|
Function will select / deselect date passed to it, it is used in case of 'Space' keyDown on a day.
|
_focusOnFallbackDay
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_focusOtherDay(days) {
const focusedDate = getDateForDateKey(this.state.focusedDateKey);
const currentMonth = focusedDate.getMonth();
const nextFocusedDate = getDateForDateKey(this.state.focusedDateKey);
nextFocusedDate.setDate(nextFocusedDate.getDate() + days);
const nextFocusedDateKey = convertDateToDateKey(nextFocusedDate);
const nextMonth = nextFocusedDate.getMonth();
if (nextMonth !== currentMonth) {
if ((nextMonth < currentMonth || (nextMonth === 11 && currentMonth === 0)) &&
!(nextMonth === 0 && currentMonth === 11)) {
this._decreaseMonthYear();
} else if ((nextMonth > currentMonth || (nextMonth === 0 && currentMonth === 11)) &&
!(nextMonth === 11 && currentMonth === 0)) {
this._increaseMonthYear();
}
}
this.setState({
focusedDateKey: nextFocusedDateKey,
});
}
|
The function is mainly used when some day is focused and Arrow keys are pressed to navigate to some other day.
days is the number of days by which focused should be moved ahead or behind.
|
_focusOtherDay
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_decreaseMonthYear() {
let newMonth;
let newYear;
if (this.state.month === 0) {
newMonth = 11;
newYear = this.state.year - 1;
} else {
newMonth = this.state.month - 1;
newYear = this.state.year;
}
this.setState({
month: newMonth,
year: newYear,
focusedDateKey: undefined,
lastHoveredDay: undefined,
});
if (this.props.onMonthUpdate) {
this.props.onMonthUpdate(newMonth + 1, newYear);
}
}
|
The function will decrease current month in state. It will also call props.onMonthUpdate.
|
_decreaseMonthYear
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_increaseMonthYear() {
let newMonth;
let newYear;
if (this.state.month === 11) {
newMonth = 0;
newYear = this.state.year + 1;
} else {
newMonth = this.state.month + 1;
newYear = this.state.year;
}
this.setState({
month: newMonth,
year: newYear,
focusedDateKey: undefined,
lastHoveredDay: undefined,
});
if (this.props.onMonthUpdate) {
this.props.onMonthUpdate(newMonth + 1, newYear);
}
}
|
The function will increase current month in state. It will also call props.onMonthUpdate.
|
_increaseMonthYear
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_isWithinMinAndMax(date) {
return !(this.props.min && date < this.props.min ||
this.props.max && date > this.props.max);
}
|
The function will increase current month in state. It will also call props.onMonthUpdate.
|
_isWithinMinAndMax
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_renderPrevMonthNav() {
const prevMonthNavStyle = {
...defaultStyle.prevMonthNavStyle,
...this.props.prevMonthNavStyle,
};
const prevMonthNavIconStyle = {
...defaultStyle.prevMonthNavIconStyle,
...this.props.prevMonthNavIconStyle,
};
let className = this.pseudoStyleIds.prevMonthNavStyleId;
if (this.props.prevMonthNavProps) {
className = unionClassNames(this.props.prevMonthNavProps.className, className);
}
return (
<ActionArea
onUpdate={ this._onClickPrevMonth }
style={ prevMonthNavStyle }
className={ className }
aria-label="Go to previous month"
{ ...this.prevMonthNavProps }
>
<div
style={ prevMonthNavIconStyle }
{ ...this.prevMonthNavIconProps }
/>
</ActionArea>
);
}
|
The function will increase current month in state. It will also call props.onMonthUpdate.
|
_renderPrevMonthNav
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_renderNextMonthNav() {
const nextMonthNavStyle = {
...defaultStyle.nextMonthNavStyle,
...this.props.nextMonthNavStyle,
};
const nextMonthNavIconStyle = {
...defaultStyle.nextMonthNavIconStyle,
...this.props.nextMonthNavIconStyle,
};
let className = this.pseudoStyleIds.nextMonthNavStyleId;
if (this.props.nextMonthNavProps) {
className = unionClassNames(this.props.nextMonthNavProps.className, className);
}
return (
<ActionArea
onUpdate={ this._onClickNextMonth }
style= { nextMonthNavStyle }
className={ className }
aria-label="Go to next month"
{ ...this.nextMonthNavProps }
>
<div
style={ nextMonthNavIconStyle }
{ ...this.nextMonthNavIconProps }
/>
</ActionArea>
);
}
|
The function will increase current month in state. It will also call props.onMonthUpdate.
|
_renderNextMonthNav
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_renderNavBar() {
const navBarStyle = {
...defaultStyle.navBarStyle,
...this.props.navBarStyle,
};
const monthLabelStyle = {
...defaultStyle.monthLabelStyle,
...this.props.monthLabelStyle,
};
return (
<div
style={ navBarStyle }
{ ...this.navBarProps }
>
{ this._renderPrevMonthNav() }
<span
style={ monthLabelStyle }
role="heading"
/*
This label has an id as suggested in http://www.w3.org/TR/wai-aria-practices/#datepicker
*/
id={ `${this.state.year}-${this.state.month}` }
{ ...this.monthLabelProps }
>
{ `${this.localeData.monthNames[this.state.month]} ${this.state.year}` }
</span>
{ this._renderNextMonthNav() }
</div>
);
}
|
Function will return jsx for rendering the nav bar for calendar.
Depending on following rules it will apply styles to prevMonthNav and nextMonthNav:
1. If disabled hide navs
2. If active apply activeStyles
|
_renderNavBar
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_renderWeekHeader() {
const weekHeaderStyle = {
...defaultStyle.weekHeaderStyle,
...this.props.weekHeaderStyle,
};
let dayLabelStyle = {
...defaultStyle.dayLabelStyle,
...this.props.dayLabelStyle,
};
if (this.props.disabled) {
dayLabelStyle = {
...dayLabelStyle,
...defaultStyle.disabledDayLabelStyle,
...this.props.disabledDayLabelStyle,
};
}
const weekendLabelStyle = {
...dayLabelStyle,
...defaultStyle.weekendLabelStyle,
...this.props.weekendLabelStyle,
};
let dayNames = shift(this.localeData.dayNamesMin, this.localeData.firstDay);
dayNames = this.localeData.isRTL ? reverse(dayNames) : dayNames;
let weekendIndex = ((7 - this.localeData.firstDay) % 7) + this.localeData.weekEnd;
weekendIndex = this.localeData.isRTL ? 6 - weekendIndex : weekendIndex;
return (
<div
style={ weekHeaderStyle }
{ ...this.weekHeaderProps }
>
{
map(dayNames, (dayAbbr, index) => ((
<span
key={ `dayAbbr-${index}` }
style={ index === weekendIndex ? weekendLabelStyle : dayLabelStyle }
role="columnheader"
{ ...this.dayLabelProps }
>
{ dayAbbr }
</span>
)))
}
</div>
);
}
|
Function will return jsx for rendering the week header for calendar.
Disabled styles will be applied for disabled date-picker.
Day headers will be rendered using locale information.
|
_renderWeekHeader
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
_renderDay(currentDate, index) {
const day = currentDate.getDate();
const month = currentDate.getMonth();
const year = currentDate.getFullYear();
const isOtherMonth = month !== this.state.month;
const dateKey = convertDateToDateKey(currentDate);
const isDisabledDay = !this._isWithinMinAndMax(currentDate);
let ariaSelected = false;
let dayStyle = {
...defaultStyle.dayStyle,
...this.props.dayStyle,
};
if (this.props.readOnly) {
dayStyle = {
...dayStyle,
...defaultStyle.readOnlyDayStyle,
...this.props.readOnlyDayStyle,
};
}
if (isOtherMonth) {
dayStyle = {
...dayStyle,
...defaultStyle.otherMonthDayStyle,
...this.props.otherMonthDayStyle,
};
}
if (this.props.disabled || isDisabledDay) {
dayStyle = {
...dayStyle,
...defaultStyle.disabledDayStyle,
...this.props.disabledDayStyle,
};
}
if (currentDate.getDay() === this.localeData.weekEnd) {
dayStyle = {
...dayStyle,
...defaultStyle.weekendStyle,
...this.props.weekendStyle,
};
}
if (day === today().getDate() && month === today().getMonth() && year === today().getFullYear()) {
dayStyle = {
...dayStyle,
...defaultStyle.todayStyle,
...this.props.todayStyle,
};
}
if (this.state.selectedDate && day === this.state.selectedDate.getDate()
&& currentDate.getMonth() === this.state.selectedDate.getMonth() && currentDate.getFullYear() === this.state.selectedDate.getFullYear()) {
dayStyle = {
...dayStyle,
...defaultStyle.selectedDayStyle,
...this.props.selectedDayStyle,
};
ariaSelected = true;
}
if (this.state.focusedDateKey === dateKey) {
dayStyle = {
...dayStyle,
...defaultStyle.focusDayStyle,
...this.props.focusDayStyle,
};
if (this.props.disabled || isDisabledDay) {
dayStyle = {
...dayStyle,
...defaultStyle.disabledFocusDayStyle,
...this.props.disabledFocusDayStyle,
};
}
}
if (!this.props.disabled && !this.props.readOnly && this.state.activeDay === dateKey) {
dayStyle = {
...dayStyle,
...defaultStyle.activeDayStyle,
...this.props.activeDayStyle,
};
}
const renderedDay = this.props.renderDay ? this.props.renderDay(currentDate) : day;
if (!this.props.showOtherMonthDate && isOtherMonth) {
return (
<span
key={ `day-${index}` }
style={ dayStyle }
{...this.emptyDayProps}
/>
);
}
if (isDisabledDay) {
return (
<DisabledDay
key={ `day-${index}` }
style={ dayStyle }
dateKey={ dateKey }
onDayMouseEnter={ this._onDayMouseEnter }
onDayMouseLeave={ this._onDayMouseLeave }
disabledDayProps={ this.disabledDayProps }
>
{ renderedDay }
</DisabledDay>
);
}
return (
<Day
key={ `day-${index}` }
dateKey={ dateKey }
onDayMouseDown={ this._onDayMouseDown }
onDayMouseUp={ this._onDayMouseUp }
onDayMouseEnter={ this._onDayMouseEnter }
onDayMouseLeave={ this._onDayMouseLeave }
onDayTouchStart={ this._onDayTouchStart }
onDayTouchEnd={ this._onDayTouchEnd }
onDayTouchCancel={ this._onDayTouchCancel }
selected={ ariaSelected }
style={ dayStyle }
dayProps={this.dayProps}
>
{ renderedDay }
</Day>
);
}
|
Function will return jsx for rendering the a day.
It will apply various styles in sequence as below (styles will be additive):
1. If component is readOnly apply readOnly styles
2. If component is disabled apply disabled styles
- If component is disabled and hovered apply disableHover styles
3. If day is weekend apply weekendStyle
4. If its day in current month and component is not disabled or readOnly:
- If its current day apply todayStyle
- If this is selected day apply selectedDayStyle
- If component is hovered apply hover styles
- If component is hovered and active apply hoveredStyles + activeStyles
- If component is hovered and not active but focused and preventFocusStyleForTouchAndClick apply focus styles
5. If current day represents other months day in calendar apply otherMonthDayStyle
|
_renderDay
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
render() {
let style = {
...defaultStyle.style,
...this.props.style,
};
if (this.props.readOnly) {
style = {
...style,
...defaultStyle.readOnlyStyle,
...this.props.readOnlyStyle,
};
}
if (this.props.disabled) {
style = {
...style,
...defaultStyle.disabledStyle,
...this.props.disabledStyle,
};
}
if (this.preventFocusStyleForTouchAndClick && this.state.isFocused) {
style = {
...style,
...defaultStyle.focusStyle,
...this.props.focusStyle,
};
}
if (this.state.isActive) {
style = {
...style,
...defaultStyle.activeStyle,
...this.props.activeStyle,
};
}
const weekArray = getWeekArrayForMonth(this.state.month, this.state.year, this.localeData.firstDay);
const tabIndex = !this.props.disabled ? this.props.tabIndex : false;
return (
<div
tabIndex={ tabIndex }
onFocus={ this._onFocus }
onBlur={ this._onBlur }
onKeyDown={ this._onKeyDown }
onMouseDown={ this._onMouseDown }
onMouseUp={ this._onMouseUp }
onTouchStart={ this._onTouchStart }
onTouchEnd={ this._onTouchEnd }
onTouchCancel={ this._onTouchCancel }
aria-label={ this.props['aria-label'] }
aria-disabled={ this.props.disabled }
aria-readonly={ this.props.readOnly }
style={ style }
className={
unionClassNames(this.props.className, this.pseudoStyleIds.styleId)
}
{...this.wrapperProps}
>
{ this._renderNavBar() }
<div
role="grid"
style={ defaultStyle.weekGridStyle }
{ ...this.weekGridProps }
>
{ this._renderWeekHeader() }
{
map(weekArray, (week) => {
const weekDays = this.localeData.isRTL ? reverse(week) : week;
return map(weekDays, (day, dayIndex) => this._renderDay(day, dayIndex));
})
}
</div>
</div>
);
}
|
Function will render:
- main calendar component
- call methods to render navBar and week header
- get array of weeks in a month and for each day in the week call method to render day
It will apply styles sequentially according to Wrapper according to following rules:
1. If component is readOnly apply readOnlyStyle
2. If component is disabled apply disabledStyle
- If disabled component is hovered apply disabledHoverStyle
3. If component is not disabled:
- If component is hovered apply hover style
- If component is hovered and active apply hover + active styles
- If component is hovered and focused but not active and preventFocusStyleForTouchAndClick is true apply focusStyles
|
render
|
javascript
|
nikgraf/belle
|
src/components/DatePicker.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DatePicker.js
|
MIT
|
render() {
return (
<span
style={ this.props.style }
onMouseEnter={ this._onMouseEnter }
onMouseLeave={ this._onMouseLeave }
onMouseDown={ this._onMouseDown }
onMouseUp={ this._onMouseUp }
onTouchStart={ this._onTouchStart }
onTouchEnd={ this._onTouchEnd }
onTouchCancel={ this._onTouchCancel }
aria-selected={ this.props.selected }
role="gridcell"
{...this.props.dayProps}
>
{ this.props.children }
</span>
);
}
|
Belle internal component to wrap a Day in a DatePicker.
This component exists to avoid binding functions in JSX.
|
render
|
javascript
|
nikgraf/belle
|
src/components/Day.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Day.js
|
MIT
|
render() {
return (
<span
style={ this.props.style }
onMouseEnter={ this._onDayMouseEnter }
onMouseLeave={ this._onDayMouseLeave }
{...this.props.disabledDayProps}
>
{ this.props.children }
</span>
);
}
|
Belle internal component to wrap a DisabledDay in a DatePicker.
This component exists to avoid binding functions in JSX.
|
render
|
javascript
|
nikgraf/belle
|
src/components/DisabledDay.js
|
https://github.com/nikgraf/belle/blob/master/src/components/DisabledDay.js
|
MIT
|
function sanitizeChildProps(properties) {
return omit(properties, Object.keys(optionPropTypes));
}
|
Returns an object with properties that are relevant for the wrapping div.
|
sanitizeChildProps
|
javascript
|
nikgraf/belle
|
src/components/Option.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Option.js
|
MIT
|
constructor(properties) {
super(properties);
this.state = {
childProps: sanitizeChildProps(properties),
};
}
|
Option component.
This component should be used together with Belle's Select.
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/Option.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Option.js
|
MIT
|
componentWillReceiveProps(properties) {
this.setState({ childProps: sanitizeChildProps(properties) });
}
|
Update the childProps based on the updated properties passed to the
Option.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/Option.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Option.js
|
MIT
|
render() {
let optionStyle;
if (this.props._isDisplayedAsSelected) {
optionStyle = {
...style.selectStyle,
...this.props.selectStyle,
};
if (this.context.isDisabled) {
optionStyle = {
...optionStyle,
...style.disabledSelectStyle,
...this.props.disabledSelectStyle,
};
}
} else {
optionStyle = {
...style.style,
...this.props.style,
};
if (this.context.isHoveredValue === this.props.value) {
optionStyle = {
...optionStyle,
...style.hoverStyle,
...this.props.hoverStyle,
};
}
}
return (
<div
style={ optionStyle }
{...this.state.childProps}
>
{ this.props.children }
</div>
);
}
|
Update the childProps based on the updated properties passed to the
Option.
|
render
|
javascript
|
nikgraf/belle
|
src/components/Option.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Option.js
|
MIT
|
function sanitizeChildProps(properties) {
return omit(properties, Object.keys(placeholderPropTypes));
}
|
Returns an object with properties that are relevant for the wrapping div.
|
sanitizeChildProps
|
javascript
|
nikgraf/belle
|
src/components/Placeholder.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Placeholder.js
|
MIT
|
constructor(properties) {
super(properties);
this.state = {
childProps: sanitizeChildProps(properties),
};
}
|
Placeholder component.
This component should be used together with Belle's Select.
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/Placeholder.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Placeholder.js
|
MIT
|
componentWillReceiveProps(properties) {
this.setState({ childProps: sanitizeChildProps(properties) });
}
|
Update the childProps based on the updated properties passed to the
Placeholder.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/Placeholder.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Placeholder.js
|
MIT
|
render() {
let computedStyle = {
...style.style,
...this.props.style,
};
if (this.props._isDisabled) {
computedStyle = {
...computedStyle,
...style.disabledStyle,
...this.props.disabledStyle,
};
}
return (
<div style={ computedStyle } {...this.state.childProps}>
{ this.props.children }
</div>
);
}
|
Update the childProps based on the updated properties passed to the
Placeholder.
|
render
|
javascript
|
nikgraf/belle
|
src/components/Placeholder.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Placeholder.js
|
MIT
|
function sanitizeWrapperProps(properties) {
return omit(properties, Object.keys(ratingPropTypes));
}
|
sanitize properties for the wrapping div.
|
sanitizeWrapperProps
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
function sanitizeCharacterProps(properties) {
return omit(properties, [
'data-belle-value',
'style',
]);
}
|
sanitize properties for the character span.
|
sanitizeCharacterProps
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
function updatePseudoClassStyle(ratingWrapperStyleId, properties, preventFocusStyleForTouchAndClick) {
let ratingFocusStyle;
if (preventFocusStyleForTouchAndClick) {
ratingFocusStyle = { outline: 0 };
} else {
ratingFocusStyle = {
...style.focusStyle,
...properties.focusStyle,
};
}
const styles = [
{
id: ratingWrapperStyleId,
style: ratingFocusStyle,
pseudoClass: 'focus',
},
];
injectStyles(styles);
}
|
Injects pseudo classes for styles into the DOM.
|
updatePseudoClassStyle
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
constructor(properties) {
super(properties);
let value;
if (has(properties, 'valueLink')) {
value = properties.valueLink.value;
} else if (has(properties, 'value')) {
value = properties.value;
} else if (has(properties, 'defaultValue')) {
value = properties.defaultValue;
}
this.state = {
value,
focusedValue: undefined,
generalProps: sanitizeWrapperProps(properties),
characterProps: sanitizeCharacterProps(properties.characterProps),
isFocus: false,
isActive: false,
};
this.preventFocusStyleForTouchAndClick = has(properties, 'preventFocusStyleForTouchAndClick') ? properties.preventFocusStyleForTouchAndClick : config.preventFocusStyleForTouchAndClick;
}
|
Rating component
The component leverages 5 characters (by default stars) to allow the user to
to rate.
|
constructor
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
componentWillMount() {
const id = uniqueId();
this.ratingWrapperStyleId = `rating-wrapper-style-id${id}`;
updatePseudoClassStyle(this.ratingWrapperStyleId, this.props, this.preventFocusStyleForTouchAndClick);
if (canUseDOM) {
this.mouseUpOnDocumentCallback = this._onMouseUpOnDocument;
document.addEventListener('mouseup', this.mouseUpOnDocumentCallback);
}
}
|
Apply pseudo class styling to the wrapper div.
|
componentWillMount
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
componentWillReceiveProps(properties) {
const newState = {
wrapperProps: sanitizeWrapperProps(properties),
characterProps: sanitizeCharacterProps(properties.characterProps),
};
if (properties.valueLink) {
newState.value = properties.valueLink.value;
} else if (properties.value) {
newState.value = properties.value;
}
this.setState(newState);
this.preventFocusStyleForTouchAndClick = has(properties, 'preventFocusStyleForTouchAndClick') ? properties.preventFocusStyleForTouchAndClick : config.preventFocusStyleForTouchAndClick;
removeStyle(this.ratingWrapperStyleId);
updatePseudoClassStyle(this.ratingWrapperStyleId, properties, this.preventFocusStyleForTouchAndClick);
}
|
Apply pseudo class styling to the wrapper div.
|
componentWillReceiveProps
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
componentWillUnmount() {
removeStyle(this.ratingWrapperStyleId);
if (canUseDOM) {
document.removeEventListener('mouseup', this.mouseUpOnDocumentCallback);
}
}
|
Removes pseudo classes from the DOM once component gets removed.
|
componentWillUnmount
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
_getCurrentValue() {
let value;
if (this.state.focusedValue !== undefined) {
value = this.state.focusedValue;
} else {
value = (this.state.value) ? this.state.value : 0;
}
return value;
}
|
Returns current value of rating to be displayed on the component
|
_getCurrentValue
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
_triggerComponentUpdateOnTouchMove(touches) {
const touchedElement = document.elementFromPoint(touches.clientX, touches.clientY);
const value = Number(touchedElement.getAttribute('data-belle-value'));
if (value && this.state.focusedValue !== value) {
this.setState({
focusedValue: value,
});
}
}
|
The function will be passed to requestAnimationFrame for touchMove
|
_triggerComponentUpdateOnTouchMove
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
_triggerComponentUpdate(value) {
if (has(this.props, 'valueLink')) {
this.props.valueLink.requestChange(value);
this.setState({
focusedValue: undefined,
isActive: false,
});
} else if (has(this.props, 'value')) {
this.setState({
focusedValue: undefined,
isActive: false,
});
} else {
this.setState({
focusedValue: undefined,
isActive: false,
value,
});
}
if (this.props.onUpdate) {
this.props.onUpdate({ value });
}
}
|
update component when component is clicked, touch ends, enter or space key are hit
different update logic will apply depending on whether component has property defaultValue, value or valueLink specified
|
_triggerComponentUpdate
|
javascript
|
nikgraf/belle
|
src/components/Rating.js
|
https://github.com/nikgraf/belle/blob/master/src/components/Rating.js
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.