badgen.net/components/builder-bar.tsx

84 wiersze
2.0 KiB
TypeScript

2018-09-07 08:06:57 +00:00
import React from 'react'
2018-10-19 05:06:41 +00:00
const DEFAULT_SIZE = 42
2019-10-04 12:45:52 +00:00
interface BuilderBarProps {
placeholder: string;
badgeURL: string;
2019-10-04 13:44:20 +00:00
host?: string;
2019-10-04 12:45:52 +00:00
onChange: (value: string) => void;
onFocus: () => void;
onBlur: () => void;
}
export default class BuilderBar extends React.Component<BuilderBarProps> {
shouldComponentUpdate ({ badgeURL }) {
const url = badgeURL ? `#${badgeURL}` : window.location.pathname
window.history.replaceState({}, document.title, url)
return true
}
2018-09-13 03:25:34 +00:00
calcInputSize = url => {
2018-10-19 05:06:41 +00:00
return url.length < DEFAULT_SIZE ? DEFAULT_SIZE : url.length
2018-09-07 08:06:57 +00:00
}
2019-08-24 08:24:46 +00:00
handleChange = ev => this.props.onChange(ev.target.value)
2018-09-07 08:06:57 +00:00
render () {
2018-09-13 03:25:34 +00:00
const { host, placeholder, badgeURL } = this.props
const inputSize = this.calcInputSize(badgeURL)
2018-09-07 08:06:57 +00:00
return (
<label>
<span className='left-host'>
<span>{host}</span>
</span>
2018-09-07 08:06:57 +00:00
<input
2018-09-07 08:29:40 +00:00
tabIndex={1}
2019-10-04 12:45:52 +00:00
spellCheck={false}
2018-09-08 14:40:15 +00:00
size={(inputSize || placeholder.length) + 1}
2018-09-07 08:06:57 +00:00
placeholder={placeholder}
2019-08-24 08:24:46 +00:00
onChange={this.handleChange}
2018-09-20 06:38:02 +00:00
onFocus={this.props.onFocus}
2018-10-19 05:06:41 +00:00
onBlur={this.props.onBlur}
2018-09-13 03:25:34 +00:00
value={badgeURL}
2018-09-07 08:06:57 +00:00
/>
<style jsx>{`
2018-09-07 08:06:57 +00:00
label {
height: 100px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: #F3F3F3;
overflow-x: auto;
2018-10-19 05:06:41 +00:00
cursor: text;
2018-09-07 08:06:57 +00:00
}
span, input {
font: 16px/24px monospace;
}
.left-host {
2018-09-07 08:06:57 +00:00
display: inline-block;
text-align: right;
2018-10-19 05:06:41 +00:00
width: 234px;
2018-09-07 08:06:57 +00:00
}
.left-host > span {
float: right;
white-space: nowrap;
}
2018-09-07 08:06:57 +00:00
input {
height: 24px;
padding: 0;
border: none;
outline: none;
background: transparent;
color: black;
min-width: 526px;
2018-09-07 08:06:57 +00:00
}
2019-08-24 08:24:46 +00:00
`}
</style>
2018-09-07 08:06:57 +00:00
</label>
)
}
}