Skip to content

Commit

Permalink
refactor(Field): Extract FieldContainer to use it in CollectionField
Browse files Browse the repository at this point in the history
By extracting this component, we can share it between Field and
CollectionField, so the later has the variant feature for free
  • Loading branch information
drazik committed Nov 29, 2019
1 parent b3b5e7f commit 09e1862
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
26 changes: 18 additions & 8 deletions react/Field/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ InputPassword.defaultProps = {
...Input.defaultProps
}

const FieldContainer = props => {
const { className, variant, ...rest } = props

return (
<div
className={cx(
styles['o-field'],
{ [styles['o-field--inline']]: variant === 'inline' },
className
)}
{...rest}
/>
)
}

const Field = props => {
const {
autoComplete,
Expand Down Expand Up @@ -205,13 +220,7 @@ const Field = props => {
}

return (
<div
className={cx(
styles['o-field'],
{ [styles['o-field--inline']]: variant === 'inline' },
className
)}
>
<FieldContainer className={className} variant={variant}>
<Label htmlFor={id} {...labelProps}>
{label}
</Label>
Expand All @@ -225,7 +234,7 @@ const Field = props => {
</div>
)}
{inputType(type)}
</div>
</FieldContainer>
)
}

Expand Down Expand Up @@ -297,3 +306,4 @@ Field.defaultProps = {
}

export default Field
export { FieldContainer }
3 changes: 3 additions & 0 deletions react/Field/styles.styl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
+small-screen()
flex-basis auto

> *
flex-grow 1

.o-field-input
position relative
max-width rem(512)
Expand Down
21 changes: 21 additions & 0 deletions react/Labs/CollectionField/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ initialState = { contacts: [contacts[0]] };
/>
</DemoProvider>
```

```jsx
import CollectionField from 'cozy-ui/transpiled/react/Labs/CollectionField';
import ContactPicker from 'cozy-ui/transpiled/react/ContactPicker';
import DemoProvider from '../../ContactsListModal/DemoProvider';
import contacts from '../../ContactsList/data.json';
initialState = { contacts: [contacts[0]] };

<DemoProvider>
<CollectionField
values={state.contacts}
component={ContactPicker}
onChange={contacts => setState({ contacts })}
label="Contacts"
addButtonLabel="Add a contact"
removeButtonLabel="Remove this contact"
placeholder="Select a contact"
variant="inline"
/>
</DemoProvider>
```
8 changes: 3 additions & 5 deletions react/Labs/CollectionField/index.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react'
import Button from '../../Button'
import cx from 'classnames'
import fieldStyles from '../../Field/styles.styl'
import Label from '../../Label'
import styles from './styles.styl'
import Stack from '../../Stack'
import Icon from '../../Icon'
import { FieldContainer } from '../../Field'

/**
* Handles a collection of form fields.
Expand All @@ -21,7 +20,6 @@ const CollectionField = props => {
addButtonLabel,
removeButtonLabel,
onChange,
className,
...rest
} = props

Expand All @@ -43,7 +41,7 @@ const CollectionField = props => {
}

return (
<div className={cx(fieldStyles['o-field'], className)} {...rest}>
<FieldContainer {...rest}>
<Label>{label}</Label>
<Stack>
<Stack spacing="s">
Expand Down Expand Up @@ -79,7 +77,7 @@ const CollectionField = props => {
className={styles.CollectionField__addBtn}
/>
</Stack>
</div>
</FieldContainer>
)
}

Expand Down
12 changes: 12 additions & 0 deletions react/__snapshots__/examples.spec.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5086,6 +5086,18 @@ exports[`Labs/CollectionField should render examples: Labs/CollectionField 1`] =
</div>"
`;

exports[`Labs/CollectionField should render examples: Labs/CollectionField 2`] = `
"<div>
<div class=\\"styles__o-field___3n5HM styles__o-field--inline___7JWZ8\\"><label class=\\"styles__c-label___o4ozG styles__c-label--block___2ZV_7\\">Contacts</label>
<div class=\\"styles__Stack--m___1tSpV\\">
<div class=\\"styles__Stack--s___22WMg\\">
<div class=\\"styles__CollectionField__row___Z7bbf\\"><button type=\\"button\\" class=\\"styles__SelectControl___2OxoO\\">Baramax</button><button type=\\"button\\" class=\\"styles__c-btn___-2Vnj styles__c-btn--secondary___3Br_N styles__c-btn--center___16_Xh styles__c-btn--round___1Lkyl\\" title=\\"Remove this contact\\"><span><svg class=\\"styles__icon___23x3R\\" style=\\"fill: var(--slateGrey);\\" width=\\"16\\" height=\\"16\\"><use xlink:href=\\"#cross-small\\"></use></svg><span class=\\"u-visuallyhidden\\">Remove this contact</span></span></button></div>
</div><button type=\\"button\\" class=\\"styles__c-btn___-2Vnj styles__c-btn--text___2Vp-2 styles__c-btn--center___16_Xh styles__CollectionField__addBtn___Z0FO-\\"><span><svg class=\\"styles__CollectionField__addBtnIcon___1hA5b styles__icon___23x3R\\" width=\\"16\\" height=\\"16\\"><use xlink:href=\\"#plus\\"></use></svg><span>Add a contact</span></span></button>
</div>
</div>
</div>"
`;

exports[`ListItemText should render examples: ListItemText 1`] = `
"<div>
<div class=\\"styles__c-list-text___3kc3E\\">
Expand Down

0 comments on commit 09e1862

Please sign in to comment.