-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): migrate
BarcodeInput
to Sanity UI/part-less setup (#…
- Loading branch information
Showing
9 changed files
with
226 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
examples/ecommerce-studio/plugins/barcode-input/BarcodeInput.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 4 additions & 0 deletions
4
examples/ecommerce-studio/plugins/barcode-input/BarcodeType.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"rules": { | ||
"import/no-unresolved": "off", | ||
} | ||
} | ||
|
28 changes: 0 additions & 28 deletions
28
packages/@sanity/cli/templates/ecommerce/plugins/barcode-input/BarcodeInput.css
This file was deleted.
Oops, something went wrong.
153 changes: 98 additions & 55 deletions
153
packages/@sanity/cli/templates/ecommerce/plugins/barcode-input/BarcodeInput.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
fe11f54
There was a problem hiding this comment.
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
fe11f54
There was a problem hiding this comment.
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
fe11f54
There was a problem hiding this comment.
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