Skip to content

Commit

Permalink
Merge pull request #1093 from cozy/feat/dualfield
Browse files Browse the repository at this point in the history
feat: Add InputGroup component
  • Loading branch information
GoOz authored Jul 9, 2019
2 parents de067e4 + 650de1e commit 1d4deb6
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = {
'../react/Checkbox/index.jsx',
'../react/Field/index.jsx',
'../react/Input/index.jsx',
'../react/InputGroup/index.jsx',
'../react/Label/index.jsx',
'../react/Radio/index.jsx',
'../react/SelectBox/SelectBox.jsx',
Expand Down
89 changes: 89 additions & 0 deletions react/InputGroup/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
### InputGroup with an appended text

```
const { Bold } = require('../Text');
const Input = require('../Input').default;
<form>
<div>
<InputGroup append={<Bold className="u-pr-1">text</Bold>}>
<Input placeholder="Placeholder" />
</InputGroup>
</div>
</form>
```

### InputGroup with a prepended text

```
const { Bold } = require('../Text');
const Input = require('../Input').default;
<form>
<div>
<InputGroup prepend={<Bold className="u-pl-1">text</Bold>}>
<Input placeholder="Placeholder" />
</InputGroup>
</div>
</form>
```

### InputGroup with an appended input

You will need to set a width to the side component, with a utility class for example.

```
const Input = require('../Input').default;
<form>
<div>
<InputGroup append={<Input placeholder="@domain.tld" className="u-w-4"/>}>
<Input placeholder="Email" />
</InputGroup>
</div>
</form>
```

### InputGroup with an appended select

You will need to set a width to the side component, with a utility class for example.

```
const SelectBox = require('../SelectBox').default;
const options = [
{ value: 'cozy.io', label: '.cozy.io' },
{ value: 'cozycloud.cc', label: '.cozycloud.cc' }
];
<form>
<div>
<InputGroup append={<SelectBox inset options={options} className="u-w-4" />}>
<Input placeholder="URL" />
</InputGroup>
</div>
</form>
```

### Full width InputGroup with an appended text

```
const { Bold } = require('../Text');
const Input = require('../Input').default;
<form>
<div>
<InputGroup fullwidth append={<Bold className="u-pr-1">text</Bold>}>
<Input placeholder="Placeholder" />
</InputGroup>
</div>
</form>
```

### Errored InputGroup with an appended text

```
const { Bold } = require('../Text');
const Input = require('../Input').default;
<form>
<div>
<InputGroup error append={<Bold className="u-pr-1">text</Bold>}>
<Input placeholder="Placeholder" />
</InputGroup>
</div>
</form>
```
57 changes: 57 additions & 0 deletions react/InputGroup/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { Component } from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import styles from './styles.styl'

class InputGroup extends Component {
constructor(props) {
super(props)
this.handleFocus = this.handleFocus.bind(this)
this.handleBlur = this.handleBlur.bind(this)
this.state = {
focused: false
}
}

handleFocus() {
this.setState({ focused: true })
}

handleBlur() {
this.setState({ focused: false })
}

render() {
const { children, prepend, append, error, fullwidth } = this.props
const { focused } = this.state
return (
<div
className={cx(styles['c-inputgroup'], {
[styles['c-inputgroup--error']]: error,
[styles['c-inputgroup--fullwidth']]: fullwidth,
[styles['c-inputgroup--focus']]: focused
})}
>
{prepend && (
<div className={styles['c-inputgroup-side']}>{prepend}</div>
)}
<div className={styles['c-inputgroup-main']}>{children}</div>
{append && <div className={styles['c-inputgroup-side']}>{append}</div>}
</div>
)
}
}

InputGroup.propTypes = {
prepend: PropTypes.object,
append: PropTypes.object,
error: PropTypes.bool,
fullwidth: PropTypes.bool
}

InputGroup.defaultProps = {
error: false,
fullwidth: false
}

export default InputGroup
22 changes: 22 additions & 0 deletions react/InputGroup/styles.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@require '../../stylus/components/forms'

.c-inputgroup
@extend $inputgroup

input
@extend $inputgroup-input

.c-inputgroup--focus
@extend $inputgroup--focus

.c-inputgroup--error
@extend $inputgroup--error

.c-inputgroup--fullwidth
@extend $inputgroup--fullwidth

.c-inputgroup-main
@extend $inputgroup-main

.c-inputgroup-side
@extend $inputgroup-side
19 changes: 14 additions & 5 deletions react/SelectBox/SelectBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isIOSApp } from 'cozy-device-helper'

import styles from './styles.styl'
import Icon from '../Icon'
import { dodgerBlue, silver, coolGrey } from '../palette'
import { dodgerBlue, silver, coolGrey, paleGrey } from '../palette'
import withBreakpoints from '../helpers/withBreakpoints'

const heights = {
Expand All @@ -16,17 +16,24 @@ const heights = {
large: '3rem'
}

const borderStyle = (props, state) => {
if (props.inset) {
return '.125rem solid white'
}
return `.0625rem solid ${state.isFocused ? dodgerBlue : silver}`
}

const customStyles = props => ({
control: (base, state) => ({
...base,
// The gray background color is managed via the select--disabled
// class applied below
backgroundColor: props.disabled ? 'transparent' : 'white',
border: state.isFocused
? `.0625rem solid ${dodgerBlue}`
: `.0625rem solid ${silver}`,
border: borderStyle(props, state),
':hover': {
borderColor: coolGrey
borderColor: props.inset ? 'white' : coolGrey,
backgroundColor: props.inset ? paleGrey : 'white',
cursor: 'pointer'
},
borderRadius: '.1875rem',
boxShadow: 'unset',
Expand Down Expand Up @@ -335,6 +342,7 @@ SelectBox.propTypes = {
components: PropTypes.object,
disabled: PropTypes.bool,
fullwidth: PropTypes.bool,
inset: PropTypes.bool,
name: PropTypes.string,
inputRef: PropTypes.func,
size: PropTypes.oneOf(['tiny', 'medium', 'large']),
Expand All @@ -344,6 +352,7 @@ SelectBox.propTypes = {
SelectBox.defaultProps = {
components: {},
fullwidth: false,
inset: false,
size: 'large',
styles: {}
}
Expand Down
32 changes: 16 additions & 16 deletions react/__snapshots__/examples.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ exports[`Field should render examples: Field 5`] = `
<form>
<div class=\\"styles__o-field___3n5HM\\"><label for=\\"\\" class=\\"styles__c-label___o4ozG styles__c-label--block___2ZV_7\\">I'am a SelectBox</label>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\" id=\\"\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select ...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -434,7 +434,7 @@ exports[`Field should render examples: Field 5`] = `
</div>
<div class=\\"styles__o-field___3n5HM\\"><label for=\\"\\" class=\\"styles__c-label___o4ozG styles__c-label--block___2ZV_7\\">I'am a SelectBox with a value</label>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\" id=\\"\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-xp4uvy\\">Choice 2</div>
<div class=\\"css-1g6gooi\\">
Expand Down Expand Up @@ -3158,7 +3158,7 @@ exports[`SelectBox should render examples: SelectBox 1`] = `
"<div>
<div style=\\"background: whitesmoke; padding: .5em;\\">
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3181,7 +3181,7 @@ exports[`SelectBox should render examples: SelectBox 1`] = `
exports[`SelectBox should render examples: SelectBox 2`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand Down Expand Up @@ -3209,7 +3209,7 @@ exports[`SelectBox should render examples: SelectBox 3`] = `
"<div>
<div style=\\"height: 12rem; padding: .5rem; overflow: auto;\\">Container height: 12rem. <button>Change container height</button>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3232,7 +3232,7 @@ exports[`SelectBox should render examples: SelectBox 3`] = `
exports[`SelectBox should render examples: SelectBox 4`] = `
"<div>
<div class=\\"css-1sontr1 styles__select--autowidth___16AEp styles__select--disabled___1W3en\\">
<div class=\\"css-adwy96\\">
<div class=\\"css-htdvti\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3254,7 +3254,7 @@ exports[`SelectBox should render examples: SelectBox 4`] = `
exports[`SelectBox should render examples: SelectBox 5`] = `
"<div>
<div class=\\"css-10nd86i styles__select--fullwidth___2l_xM\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3277,7 +3277,7 @@ exports[`SelectBox should render examples: SelectBox 6`] = `
"<div>
<div>
<div class=\\"css-1sontr1 styles__select--autowidth___16AEp styles__select--disabled___1W3en\\">
<div class=\\"css-adwy96\\">
<div class=\\"css-htdvti\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3302,7 +3302,7 @@ exports[`SelectBox should render examples: SelectBox 7`] = `
<div>
<div class=\\"u-mb-1\\">
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-1rg8gyn\\">
<div class=\\"css-1enqz0q\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-xp4uvy\\">I am a tiny SelectBox</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3321,7 +3321,7 @@ exports[`SelectBox should render examples: SelectBox 7`] = `
</div>
<div class=\\"u-mb-1\\">
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-15w89vh\\">
<div class=\\"css-s52rxs\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-xp4uvy\\">I am a medium SelectBox</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3340,7 +3340,7 @@ exports[`SelectBox should render examples: SelectBox 7`] = `
</div>
<div class=\\"u-mb-1\\">
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-xp4uvy\\">I am a large SelectBox</div>
<div class=\\"css-1g6gooi\\">
Expand Down Expand Up @@ -3388,7 +3388,7 @@ exports[`SelectBox should render examples: SelectBox 8`] = `
exports[`SelectBox should render examples: SelectBox 9`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3410,7 +3410,7 @@ exports[`SelectBox should render examples: SelectBox 9`] = `
exports[`SelectBox should render examples: SelectBox 10`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3432,7 +3432,7 @@ exports[`SelectBox should render examples: SelectBox 10`] = `
exports[`SelectBox should render examples: SelectBox 11`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand Down Expand Up @@ -3492,7 +3492,7 @@ exports[`SelectBox should render examples: SelectBox 11`] = `
exports[`SelectBox should render examples: SelectBox 12`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw\\">
<div class=\\"css-zdlybx\\">
<div class=\\"css-1phseng needsclick\\">
<div class=\\"css-1492t68\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand All @@ -3514,7 +3514,7 @@ exports[`SelectBox should render examples: SelectBox 12`] = `
exports[`SelectBox should render examples: SelectBox 13`] = `
"<div>
<div class=\\"css-10nd86i styles__select--autowidth___16AEp\\">
<div class=\\"css-2zgoaw needsclick cz__control\\">
<div class=\\"css-zdlybx needsclick cz__control\\">
<div class=\\"css-1phseng needsclick cz__value-container\\">
<div class=\\"css-1492t68 needsclick cz__placeholder\\">Select...</div>
<div class=\\"css-1g6gooi\\">
Expand Down
1 change: 1 addition & 0 deletions react/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ export { default as FileInput } from './FileInput'
export { default as Card } from './Card'
export { default as InlineCard } from './InlineCard'
export { default as PercentageLine } from './PercentageLine'
export { default as InputGroup } from './InputGroup'
Loading

0 comments on commit 1d4deb6

Please sign in to comment.