Files
mocoapp-browser-extension/src/js/components/Select.js
Manuel Bouza 72626a6c42 qw/timer (#23)
* Rename logo and add 32x32 version

* Set timer icon if a timer is running

* Do not query activities on initialization

* Show timer in bubble if timed activity exists

* Pass timed activity to App

* Code cleanup

* Show timer view and stop timer

* Make hours optional

* Use booked seconds instead of hours

* Add type submit to form button

* Define colors as sass variables⎄

* Style timer view

* Show start timer submit label

* Update view layouts and content

* Update version and changelog

* Dyanically set iframe height

* Reduce h1 font size

* Add svg webpack loader

* Parse empty string (TimeInputParser)

* Forward ref in Popup component

* Start time on current day only, format buttons

* Improve styling

* Set standard height as iframe default height, validate form

* Upgrade packages to supress react warning

* Show activity form in popup after timer was stoped

* Use stop-watch icon in timer view

* Fix empty description

* Close TimerView if timer stopped for current service

* Style timerview

* Improve timer view styling

* qw/setting-time-tracking-hh-mm (#24)

* Format duration depending on settingTimeTrackingHHMM

* Fix formatDuation without second argument

* Fix time format after updating bubble

* Add tests for formatDuration
2019-10-10 14:57:01 +02:00

143 lines
3.3 KiB
JavaScript

import React, { Component } from "react"
import PropTypes from "prop-types"
import ReactSelect, { createFilter, components } from "react-select"
import {
values,
isString,
isNumber,
join,
filter,
compose,
property,
flatMap,
pathEq,
isNil,
} from "lodash/fp"
const hasOptionGroups = options => options.some(option => Boolean(option.options))
const customTheme = theme => ({
...theme,
borderRadius: 0,
spacing: {
...theme.spacing,
baseUnit: 3,
controlHeight: 32,
},
colors: {
...theme.colors,
primary: "#38b5eb",
primary75: "rgba(56, 181, 235, 0.25)",
primary50: "#38b5eb",
primary25: "#38b5eb",
},
})
const customStyles = props => ({
control: (base, _state) => ({
...base,
borderColor: props.hasError ? "#fb3a2f" : base.borderColor,
}),
valueContainer: base => ({
...base,
padding: "4px 12px",
}),
input: base => ({
...base,
border: "0 !important",
boxShadow: "0 !important",
}),
groupHeading: (base, _state) => ({
...base,
color: "black",
textTransform: "none",
fontWeight: "bold",
fontSize: "100%",
padding: "2px 7px 4px",
}),
option: (base, state) => ({
...base,
padding: hasOptionGroups(state.options) ? "4px 7px 4px 20px" : "4px 7px 4px",
backgroundColor: state.isFocused ? "#38b5eb" : "none",
color: state.isFocused ? "white" : "hsl(0, 0%, 20%)",
}),
})
const filterOption = createFilter({
stringify: compose(
join(" "),
filter(value => isString(value) || isNumber(value)),
values,
property("data"),
),
})
SingleValue.propTypes = {
children: PropTypes.string.isRequired,
data: PropTypes.shape({
customerName: PropTypes.string,
}).isRequired,
}
function SingleValue({ children, ...props }) {
const label = isNil(props.data.customerName)
? children
: `${props.data.customerName}: ${children}`
return <components.SingleValue {...props}>{label}</components.SingleValue>
}
export default class Select extends Component {
static propTypes = {
name: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
options: PropTypes.array,
hasError: PropTypes.bool,
onChange: PropTypes.func.isRequired,
}
static findOptionByValue = (selectOptions, value) => {
const options = flatMap(option => (option.options ? option.options : option), selectOptions)
return options.find(pathEq("value", value)) || null
}
constructor(props) {
super(props)
this.select = React.createRef()
}
handleChange = option => {
const { name, onChange } = this.props
const { value } = option
onChange({ target: { name, value } })
}
handleKeyDown = event => {
if (!this.select.current) {
return
}
if (!this.select.current.state.menuIsOpen && event.key === "Enter") {
this.select.current.setState({ menuIsOpen: true })
}
}
render() {
const { value, ...passThroughProps } = this.props
return (
<ReactSelect
{...passThroughProps}
ref={this.select}
value={Select.findOptionByValue(this.props.options, value)}
filterOption={filterOption}
theme={customTheme}
styles={customStyles(this.props)}
components={{ SingleValue }}
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
/>
)
}
}