Skip to content

Commit

Permalink
refactor(cli): migrate BarcodeInput to Sanity UI/part-less setup (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelfull committed Sep 16, 2021
1 parent b7c51c3 commit fe11f54
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .module-aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ module.exports = {
'@sanity/diff': './packages/@sanity/diff/src',
'@sanity/field': './packages/@sanity/field/src',
'@sanity/form-builder/PatchEvent': './packages/@sanity/form-builder/src/PatchEvent',
// Required by BarcodeInput in the Ecommerce template
'@sanity/form-builder/lib/FormBuilderInput':
'./packages/@sanity/form-builder/src/FormBuilderInput',
'@sanity/form-builder': './packages/@sanity/form-builder/src',
'@sanity/imagetool': './packages/@sanity/imagetool/src',
'@sanity/initial-value-templates': './packages/@sanity/initial-value-templates/src',
Expand Down
3 changes: 2 additions & 1 deletion examples/ecommerce-studio/config/.checksums
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"#": "Used by Sanity to keep track of configuration file checksums, do not delete or modify!",
"@sanity/default-layout": "bb034f391ba508a6ca8cd971967cbedeb131c4d19b17b28a0895f32db5d568ea",
"@sanity/default-login": "6fb6d3800aa71346e1b84d95bbcaa287879456f2922372bb0294e30b968cd37f",
"@sanity/data-aspects": "d199e2c199b3e26cd28b68dc84d7fc01c9186bf5089580f2e2446994d36b3cb6"
"@sanity/data-aspects": "d199e2c199b3e26cd28b68dc84d7fc01c9186bf5089580f2e2446994d36b3cb6",
"@sanity/form-builder": "b38478227ba5e22c91981da4b53436df22e48ff25238a55a973ed620be5068aa"
}
3 changes: 3 additions & 0 deletions examples/ecommerce-studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"@sanity/default-layout": "2.19.0",
"@sanity/default-login": "2.19.0",
"@sanity/desk-tool": "2.19.0",
"@sanity/form-builder": "2.19.0",
"@sanity/ui": "^0.36.8",
"prop-types": "^15.6.0",
"react": "17.0.1",
"react-barcode": "^1.3.2",
"react-dom": "17.0.1",
Expand Down
109 changes: 109 additions & 0 deletions examples/ecommerce-studio/plugins/barcode-input/BarcodeInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copied from packages/@sanity/cli/templates/ecommerce/plugins/barcode-input/BarcodeInput.js

import React, {useState} from 'react'
import PropTypes from 'prop-types'
import Barcode from 'react-barcode'
import {FormFieldSet} from '@sanity/base/components'
import {Box} from '@sanity/ui'
import {setIfMissing} from '@sanity/form-builder/PatchEvent'
import {FormBuilderInput} from '@sanity/form-builder/lib/FormBuilderInput'
import styled from 'styled-components'

const BarcodeRoot = styled(Box)`
svg {
display: block;
margin: 1em auto;
max-width: 100%;
}
`

const FieldWrapper = styled.div`
display: grid;
grid-template-columns: 2fr 1fr;
grid-gap: 1em;
`

const ErrorMessage = styled.div`
color: #e66666;
text-align: center;
padding: 1em;
`

const BarcodeInput = React.forwardRef(function BarcodeInput(props, ref) {
const {
level,
type,
value,
readOnly,
markers,
presence,
compareValue,
focusPath,
onFocus,
onBlur,
onChange,
} = props
const [valid, setValid] = useState(true)
const handleFieldChange = React.useCallback(
(field, patchEvent) => {
onChange(patchEvent.prefixAll(field.name).prepend(setIfMissing({_type: type.name})))
},
[onChange, type.name]
)

const handleValid = (validState) => {
setValid(validState)
}

return (
<FormFieldSet
level={level}
title={type.title}
description={type.description}
legend={type.title}
>
<BarcodeRoot isValid={valid}>
{value && value.barcode && (
<Barcode
textAlign="center"
value={value.barcode}
format={value.format || ''}
valid={handleValid} // eslint-disable-line react/jsx-handler-names
/>
)}
{!valid && <ErrorMessage>Not valid {value.format}</ErrorMessage>}
</BarcodeRoot>
<FieldWrapper>
{type.fields.map((field) => (
<FormBuilderInput
key={field.name}
description={field.type.description}
title={field.type.title}
type={field.type}
value={value && value[field.name]}
compareValue={compareValue}
path={[field.name]}
focusPath={focusPath}
readOnly={readOnly || field.type.readOnly}
presence={presence}
markers={markers}
onFocus={onFocus}
onBlur={onBlur}
onChange={(patchEvent) => handleFieldChange(field, patchEvent)}
/>
))}
</FieldWrapper>
</FormFieldSet>
)
})

BarcodeInput.propTypes = {
level: PropTypes.number,
value: PropTypes.object,
onChange: PropTypes.func,
type: PropTypes.object,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
}

export default BarcodeInput
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Copied from packages/@sanity/cli/templates/ecommerce/plugins/barcode-input/BarcodeType.js

import BarcodeInput from './BarcodeInput'

export default {
name: 'barcode',
title: 'Barcode',
type: 'object',
inputComponent: BarcodeInput,

preview: {
select: {
title: 'barcode',
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/cli/.depcheckignore.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"//": "these are used by template projects and doesn't need to be project dependencies",
"ignore": ["react-icons", "react", "prop-types", "react-barcode"]
"ignore": ["react-icons", "react", "prop-types", "react-barcode", "@sanity/base", "@sanity/ui", "@sanity/form-builder", "styled-components"]
}
6 changes: 6 additions & 0 deletions packages/@sanity/cli/templates/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"import/no-unresolved": "off",
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,64 +1,107 @@
import React, {PureComponent} from 'react' // eslint-disable-line import/no-unresolved
import React, {useState} from 'react'
import PropTypes from 'prop-types'
import {FormBuilderInput, patches} from 'part:@sanity/form-builder'
import Fieldset from 'part:@sanity/components/fieldsets/default'
import Barcode from 'react-barcode' // eslint-disable-line import/no-unresolved
import styles from './BarcodeInput.css'
import Barcode from 'react-barcode'
import {FormFieldSet} from '@sanity/base/components'
import {Box} from '@sanity/ui'
import {setIfMissing} from '@sanity/form-builder/PatchEvent'
import {FormBuilderInput} from '@sanity/form-builder/lib/FormBuilderInput'
import styled from 'styled-components'

const {setIfMissing} = patches

export default class BarcodeInput extends PureComponent {
state = {
valid: true,
const BarcodeRoot = styled(Box)`
svg {
display: block;
margin: 1em auto;
max-width: 100%;
}
`

static propTypes = {
level: PropTypes.number,
value: PropTypes.object,
onChange: PropTypes.func,
type: PropTypes.string,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
}
const FieldWrapper = styled.div`
display: grid;
grid-template-columns: 2fr 1fr;
grid-gap: 1em;
`

handleFieldChange = (field, fieldPatchEvent) => {
const {onChange, type} = this.props
onChange(fieldPatchEvent.prefixAll(field.name).prepend(setIfMissing({_type: type.name})))
}
const ErrorMessage = styled.div`
color: #e66666;
text-align: center;
padding: 1em;
`

handleValid = (valid) => {
this.setState({valid})
}
const BarcodeInput = React.forwardRef(function BarcodeInput(props, ref) {
const {
level,
type,
value,
readOnly,
markers,
presence,
compareValue,
focusPath,
onFocus,
onBlur,
onChange,
} = props
const [valid, setValid] = useState(true)
const handleFieldChange = React.useCallback(
(field, patchEvent) => {
onChange(patchEvent.prefixAll(field.name).prepend(setIfMissing({_type: type.name})))
},
[onChange, type.name]
)

render() {
const {level, type, value} = this.props
const {valid} = this.state
return (
<Fieldset level={level} legend={type.title} description={type.description}>
<div className={valid ? styles.barcodeValid : styles.barcodeInvalid}>
{value && value.barcode && (
<Barcode
textAlign="center"
value={value.barcode}
format={value.format || ''}
valid={this.handleValid} // eslint-disable-line react/jsx-handler-names
/>
)}
</div>
{!valid && <p className={styles.errorMessage}>Not valid {value.format}</p>}
<div className={styles.fieldWrapper}>
{type.fields.map((field) => (
<FormBuilderInput
key={field.name}
type={field.type}
value={value && value[field.name]}
onChange={(patchEvent) => this.handleFieldChange(field, patchEvent)}
onBlur={this.props.onBlur}
onFocus={this.props.onFocus}
/>
))}
</div>
</Fieldset>
)
const handleValid = (validState) => {
setValid(validState)
}

return (
<FormFieldSet
level={level}
title={type.title}
description={type.description}
legend={type.title}
>
<BarcodeRoot isValid={valid}>
{value && value.barcode && (
<Barcode
textAlign="center"
value={value.barcode}
format={value.format || ''}
valid={handleValid} // eslint-disable-line react/jsx-handler-names
/>
)}
{!valid && <ErrorMessage>Not valid {value.format}</ErrorMessage>}
</BarcodeRoot>
<FieldWrapper>
{type.fields.map((field) => (
<FormBuilderInput
key={field.name}
description={field.type.description}
title={field.type.title}
type={field.type}
value={value && value[field.name]}
compareValue={compareValue}
path={[field.name]}
focusPath={focusPath}
readOnly={readOnly || field.type.readOnly}
presence={presence}
markers={markers}
onFocus={onFocus}
onBlur={onBlur}
onChange={(patchEvent) => handleFieldChange(field, patchEvent)}
/>
))}
</FieldWrapper>
</FormFieldSet>
)
})

BarcodeInput.propTypes = {
level: PropTypes.number,
value: PropTypes.object,
onChange: PropTypes.func,
type: PropTypes.object,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
}

export default BarcodeInput

3 comments on commit fe11f54

@vercel
Copy link

@vercel vercel bot commented on fe11f54 Sep 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

studio-workshop – ./dev/workshop

studio-workshop-git-next.sanity.build
studio-workshop.sanity.build

@vercel
Copy link

@vercel vercel bot commented on fe11f54 Sep 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

perf-studio – ./

perf-studio.sanity.build
perf-studio-git-next.sanity.build

@vercel
Copy link

@vercel vercel bot commented on fe11f54 Sep 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.