Skip to content

#50 Option to hide/show string quotes #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The only *required* value is `data`.
| `searchDebounceTime` | `number` | `350` | Debounce time when `searchText` changes |
| `keySort` | `boolean\|CompareFunction` | `false` | If `true`, object keys will be ordered (using default JS `.sort()`). A [compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) can also be provided to define sorting behaviour. |
| `showArrayIndices` | `boolean` | `true` | Whether or not to display the index (as a property key) for array elements. |
| `showStringQuotes` | `boolean` | `true` | Whether or not to display string values in "quotes". |
| `showCollectionCount` | `boolean\|"when-closed"` | `true` | Whether or not to display the number of items in each collection (object or array). |
| `defaultValue` | `any\|DefaultValueFilterFunction` | `null` | When a new property is added, it is initialised with this value. A function can be provided with the same input as the `FilterFunction`s, but should output a value. This allows a different default value to be used depending on the data state (e.g. default for top level is an object, but a string elsewhere.) |
| `stringTruncate` | `number` | `250` | String values longer than this many characters will be displayed truncated (with `...`). The full string will always be visible when editing. |
Expand Down
20 changes: 16 additions & 4 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function App() {
const [allowCopy, setAllowCopy] = useState(true)
const [sortKeys, setSortKeys] = useState(false)
const [showIndices, setShowIndices] = useState(true)
const [showStringQuotes, setShowStringQuotes] = useState(true)
const [defaultNewValue, setDefaultNewValue] = useState('New data!')
const [isSaving, setIsSaving] = useState(false)
const [searchText, setSearchText] = useState('')
Expand Down Expand Up @@ -284,6 +285,7 @@ function App() {
keySort={sortKeys}
defaultValue={demoData[selectedData]?.defaultValue ?? defaultNewValue}
showArrayIndices={showIndices}
showStringQuotes={showStringQuotes}
minWidth={'min(500px, 95vw)'}
maxWidth="min(650px, 90vw)"
className="block-shadow"
Expand Down Expand Up @@ -483,12 +485,12 @@ function App() {
</Flex>
<Flex w="100%" justify="flex-start">
<Checkbox
id="sortKeysCheckbox"
isChecked={sortKeys}
onChange={() => setSortKeys(!sortKeys)}
id="showStringQuotesCheckbox"
isChecked={showStringQuotes}
onChange={() => setShowStringQuotes(!showStringQuotes)}
w="50%"
>
Sort Object keys
Show String quotes
</Checkbox>
<Checkbox
id="showIndicesCheckbox"
Expand All @@ -499,6 +501,16 @@ function App() {
Show Array indices
</Checkbox>
</Flex>
<Flex w="100%" justify="flex-start">
<Checkbox
id="sortKeysCheckbox"
isChecked={sortKeys}
onChange={() => setSortKeys(!sortKeys)}
w="50%"
>
Sort Object keys
</Checkbox>
</Flex>
<HStack className="inputRow" pt={2}>
<FormLabel className="labelWidth" textAlign="right">
Default new value
Expand Down
2 changes: 2 additions & 0 deletions src/JsonEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const Editor: React.FC<JsonEditorProps> = ({
searchDebounceTime = 350,
keySort = false,
showArrayIndices = true,
showStringQuotes = true,
defaultValue = null,
minWidth = 250,
maxWidth = 'min(600px, 90vw)',
Expand Down Expand Up @@ -179,6 +180,7 @@ const Editor: React.FC<JsonEditorProps> = ({
enableClipboard,
keySort,
showArrayIndices,
showStringQuotes,
indent,
defaultValue,
stringTruncate,
Expand Down
2 changes: 2 additions & 0 deletions src/ValueNodeWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ValueNodeWrapper: React.FC<ValueNodeProps> = (props) => {
searchText,
showLabel,
stringTruncate,
showStringQuotes,
indent,
translate,
customNodeDefinitions,
Expand Down Expand Up @@ -192,6 +193,7 @@ export const ValueNodeWrapper: React.FC<ValueNodeProps> = (props) => {
handleCancel,
path,
stringTruncate,
showStringQuotes,
nodeData,
translate,
}
Expand Down
5 changes: 4 additions & 1 deletion src/ValueNodes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const StringValue: React.FC<InputProps & { value: string }> = ({
handleEdit,
handleCancel,
stringTruncate,
showStringQuotes,
nodeData,
}) => {
const { getStyles } = useTheme()
Expand Down Expand Up @@ -48,7 +49,9 @@ export const StringValue: React.FC<InputProps & { value: string }> = ({
className="jer-value-string"
style={getStyles('string', nodeData)}
>
&quot;{truncate(value, stringTruncate)}&quot;
{showStringQuotes ? '"' : ''}
{truncate(value, stringTruncate)}
{showStringQuotes ? '"' : ''}
</div>
)
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface JsonEditorProps {
searchDebounceTime?: number
keySort?: boolean | CompareFunction
showArrayIndices?: boolean
showStringQuotes?: boolean
defaultValue?: unknown
minWidth?: string | number
maxWidth?: string | number
Expand Down Expand Up @@ -144,6 +145,7 @@ export type ValueData = string | number | boolean
export interface ValueNodeProps extends BaseNodeProps {
data: ValueData
showLabel: boolean
showStringQuotes: boolean
}

export interface CustomNodeProps<T = Record<string, unknown>> extends BaseNodeProps {
Expand Down Expand Up @@ -188,6 +190,7 @@ export interface InputProps {
handleCancel: () => void
path: CollectionKey[]
stringTruncate: number
showStringQuotes: boolean
nodeData: NodeData
translate: TranslateFunction
}
Expand Down