Files
shields/frontend/components/dynamic-badge-maker.js
T
DanialandGitHub d8cf836264 Add support for rgb, rgba, hsl, hsla, css named colors (#1635)
* add support for rgb, rgb, css named colors

* add support for hsl, hsla & add color-validate

* update makeBadge test, better coverage

* re-add comment

* add comment for supported colors

* dynamic badge gen, remove 'hex'

* add support for 1.0 opacity & fix 101-109

* fix colorscheme tests

* remove extra tests

* add test for negative values

* add test for uppercase & mixed case colors

* fix mixed case/uppercase test

* allow whitespace around color

* update test error messages

* add comments

* add more uppercase test

* update error message

* default to grey/red if invalid color chosen

default colorscheme:
colorA: grey
colorB: red

* Revert "default to grey/red if invalid color chosen"

This reverts commit 10db0c6d74.
Reverted as this affects the CLI version/when no color specified.

* validColor -> isCSSColor

* assignColor function

* update tests to use sazerac
2018-07-12 10:26:17 +12:00

83 lines
2.5 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import { dynamicBadgeUrl } from '../lib/badge-url';
export default class DynamicBadgeMaker extends React.Component {
static propTypes = {
baseUri: PropTypes.string,
};
state = {
datatype: '',
label: '',
url: '',
query: '',
color: '',
prefix: '',
suffix: '',
};
makeBadgeUri () {
const { datatype, label, url, query, color, prefix, suffix } = this.state;
const { baseUri: baseUrl = document.location.href } = this.props;
return dynamicBadgeUrl(baseUrl, datatype, label, url, query, { color, prefix, suffix });
}
handleSubmit(e) {
e.preventDefault();
document.location = this.makeBadgeUri();
}
get isValid() {
const { datatype, label, url, query } = this.state;
return datatype && label && url && query;
}
render() {
return (
<form onSubmit={e => this.handleSubmit(e)}>
<select
className="short"
value={this.state.datatype}
onChange={event => this.setState({ datatype: event.target.value })}>
<option value="" disabled>data type</option>
<option value="json">json</option>
<option value="xml">xml</option>
<option value="yaml">yaml</option>
</select> {}
<input
className="short"
value={this.state.label}
onChange={event => this.setState({ label: event.target.value })}
placeholder="label" /> {}
<input
className="short"
value={this.state.url}
onChange={event => this.setState({ url: event.target.value })}
placeholder="url" /> {}
<input
className="short"
value={this.state.query}
onChange={event => this.setState({ query: event.target.value })}
placeholder="query" /> {}
<input
className="short"
value={this.state.color}
onChange={event => this.setState({ color: event.target.value })}
placeholder="color" /> {}
<input
className="short"
value={this.state.prefix}
onChange={event => this.setState({ prefix: event.target.value })}
placeholder="prefix" /> {}
<input
className="short"
value={this.state.suffix}
onChange={event => this.setState({ suffix: event.target.value })}
placeholder="suffix" /> {}
<button disabled={! this.isValid}>Make Badge</button>
</form>
);
}
}