Skip to content

Commit

Permalink
Fixed #1
Browse files Browse the repository at this point in the history
  • Loading branch information
wildseansy committed Jun 15, 2021
1 parent 9757425 commit 00d3381
Show file tree
Hide file tree
Showing 4 changed files with 1,680 additions and 1,470 deletions.
4 changes: 2 additions & 2 deletions examples/courseBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Course = {
},
{
title: 'Is Active',
name: "courseActive",
name: 'courseActive',
description: 'Whether this course has scheduled items remaining this quarter.',
type: 'boolean',
inputComponent: ComputedField,
Expand All @@ -36,7 +36,7 @@ const Course = {
},
{
title: 'Description',
name: "description",
name: 'description',
description: 'Generated by what instructors are teaching this class on the schedule.',
type: 'text',
inputComponent: ComputedField,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanity-plugin-computed-field",
"version": "1.0.1",
"version": "1.0.2",
"description": "A Field based on other fields. Memoization in your sanity model",
"main": "lib/ComputedField.js",
"scripts": {
Expand Down
18 changes: 11 additions & 7 deletions src/ComputedField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,23 @@ const validateConfiguration = (options: SanityType['options']) => {
}

const ComputedField: React.FC<SanityProps> = React.forwardRef(
(props, forwardedRef: React.ForwardedRef<HTMLInputElement>) => {
(props: SanityProps, forwardedRef: React.ForwardedRef<HTMLInputElement>) => {
const {type, level, onFocus, value, markers} = props
const document = props.document
const errors = React.useMemo(() => markers.filter(isValidationErrorMarker), [markers])
const [loading, setLoading] = React.useState(false)
const {_id, _type}: SanityDocument = document
const options = props.type.options
validateConfiguration(options)
const reducer = React.useCallback((queryResult) => options.reduceQueryResult(queryResult), [
options.reduceQueryResult,
])
const reducer = React.useCallback(
(queryResult: {[s: string]: unknown}) => options.reduceQueryResult(queryResult),
[options.reduceQueryResult]
)
const handleChange = React.useCallback(
(val) => {
(val: any) => {
let validated = val
if (type.name === 'number') {
validated = parseInt(val, 10)
validated = parseFloat(val)
if (validated === NaN) {
validated = undefined
}
Expand All @@ -87,7 +88,10 @@ const ComputedField: React.FC<SanityProps> = React.forwardRef(
},
[props.onChange, type.name]
)
const onChange = React.useCallback((e) => handleChange(e.target.value), [handleChange])
const onChange = React.useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => handleChange(e.target.value),
[handleChange]
)
const generate = React.useCallback(() => {
const query = `*[_type == '${_type}' && _id == '${_id}' || _id == '${_id.replace(
'drafts.',
Expand Down
Loading

0 comments on commit 00d3381

Please sign in to comment.