-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbible-input.js
47 lines (39 loc) · 1.32 KB
/
bible-input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React, {useState} from 'react';
import PatchEvent, {set, unset} from 'part:@sanity/form-builder/patch-event';
import {extractAndValidate} from '@newfrontdoor/bible';
const createPatchFrom = value =>
PatchEvent.from(value === '' ? unset() : set(value));
function validator({isValid, arrays}) {
if (isValid[0] === null) return null;
if (isValid.every(element => element === true)) {
return arrays;
}
return false;
}
export default function BibleInput(props) {
const [input, setInput] = useState(null);
const [valid, setValid] = useState(null);
const {type, value} = props;
const isValid = valid.map(array => {
if (Array.isArray(array) === false) return null;
if (array.some(e => e === 'incomplete')) return 'incomplete';
if (array.some(e => e === 'non-sequential')) return 'non-sequential';
return array.every(e => e === true);
});
function handleInputChange(e) {
const string = e.currentTarget.value;
setInput(string);
const [arrays, validated] = extractAndValidate(e.currentTarget.value);
setValid(validated);
createPatchFrom(validator(isValid, arrays));
}
return (
<div>
<h2>Passage</h2>
<input value={input} onChange={handleInputChange} />
<div>
<ul>{value.map(array => array.map(item => <li>{item}</li>))}</ul>
</div>
</div>
);
}