Skip to content

Commit

Permalink
Feat: (block-editor-utils) Add RangeField (#1575)
Browse files Browse the repository at this point in the history
* Feat: (block-editor-utils) Add RangeField

* Chore: Changeset

* Deps: (block-editor-utils) Update package.json
  • Loading branch information
theodesp authored Sep 14, 2023
1 parent 38ea99f commit ee8c08e
Show file tree
Hide file tree
Showing 11 changed files with 518 additions and 15,294 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-planets-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/block-editor-utils': patch
---

Feat: Add `RangeControl` field in `block-editor-utils`.
15,644 changes: 385 additions & 15,259 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/block-editor-utils/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ module.exports = {
],
},
// Run code before each file in the suite is tested.
setupFilesAfterEnv: ['./jest.setup.ts'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
setupFiles: [
'<rootDir>/tests/global-mocks.ts',
],

testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',

Expand Down
3 changes: 1 addition & 2 deletions packages/block-editor-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"types": "dist/mjs/index.d.ts",
"peerDependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0",
"@faustwp/blocks": ">=2.0.0"
"react-dom": "^18.0.0"
},
"dependencies": {
"@wordpress/blocks": "^12.18.0",
Expand Down
27 changes: 27 additions & 0 deletions packages/block-editor-utils/src/controls/Range.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as React from 'react';
import { RangeControl } from '@wordpress/components';
import { ControlProps, RangeField } from '../types/index.js';

function Range<T extends Record<string, any>>({
config,
props,
}: ControlProps<T>) {
const onChange = (newContent: number | undefined) => {
props.setAttributes({ [config.name]: newContent });
};
const params: RangeField = {
...config,
control: 'range',
}; // TODO use satisfies operator when using TS v5
return (
<RangeControl
label={params.label}
onChange={onChange}
value={props.attributes[config.name]}
min={params?.min}
max={params?.max}
/>
);
}

export default Range;
2 changes: 2 additions & 0 deletions packages/block-editor-utils/src/controls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import NumberField from './Number.js';
import Color from './Color.js';
import Select from './Select.js';
import Radio from './Radio.js';
import Range from './Range.js';

registerControl('text', Text);
registerControl('number', NumberField);
registerControl('color', Color);
registerControl('select', Select);
registerControl('radio', Radio);
registerControl('range', Range);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ function getControlFields(
// Set default field by merging both blockAttributes meta and editorFields hints.
if (fieldConfig) {
fields.push({
...fieldConfig,
name: key,
label: fieldConfig.label ?? key,
type: fieldType,
Expand Down
37 changes: 18 additions & 19 deletions packages/block-editor-utils/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,32 @@ export interface FieldOption {
value: string;
}

export type Field = {
export type Field = BasicField | SelectableField | RangeField

type BasicField = {
name: string;
type: FieldType;
type: FieldType
control: FieldControl;
location: FieldLocation;
label?: string;
default?: unknown;
options?: FieldOption[];
};

export type FieldType =
| 'string'
| 'number'
| 'boolean'
| 'integer'
| 'object'
| 'array';
export type FieldControl =
| 'textarea'
| 'color'
| 'text'
| 'radio'
| 'select'
| 'range'
| 'number'
| 'checkbox';
export type FieldLocation = 'editor' | 'inspector';
export type SelectableField = BasicField & {
control: 'select' | 'radio';
options?: FieldOption[];
}

export type RangeField = BasicField & {
control: 'range';
min?: number;
max?: number;
}

export type FieldType = "string" | "number" | "boolean" | "integer" | "object" | "array"
export type FieldControl = "textarea" | "color" | "text" | "radio" | "select" | "range" | "number" | "checkbox"
export type FieldLocation = "editor" | "inspector"

export interface ControlProps<T extends Record<string, any>> {
config: Field;
Expand Down
49 changes: 49 additions & 0 deletions packages/block-editor-utils/tests/controls/Range.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import React from 'react';
import { screen, render } from '@testing-library/react';
import Range from '../../src/controls/Range';
import { Field } from '../../src/types';

const config: Field = {
label: 'rangeField',
name: 'rangeField',
type: 'number',
control: 'range',
location: 'inspector',
min: 0,
max: 50,
};
const props = {
setAttributes: jest.fn(),
attributes: {
rangeField: 20,
},
};

const setup = () => {
const utils = render(<Range config={config} props={props} />);
const input = screen.findAllByDisplayValue(props.attributes.rangeField!);
return {
input,
...utils,
};
};

afterEach(() => {
jest.clearAllMocks();
});

describe('RangeField', () => {
it('should mount', () => {
const { input } = setup();
expect(input).toBeTruthy();
});

it('should have correct display value from props', () => {
setup();
expect(
screen.getByRole('spinbutton', {
name: /rangefield/i,
})
).toHaveValue(props.attributes.rangeField);
});
});
13 changes: 13 additions & 0 deletions packages/block-editor-utils/tests/global-mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
26 changes: 13 additions & 13 deletions packages/block-editor-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/mjs",
"target": "es2017",
"rootDir": "src",
"jsx": "react",
"types": ["node", "jest"],
"typeRoots": ["./node_modules/@types", "./src/types"]
},
"exclude": ["node_modules", "dist"],
"include": ["src"]
}
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/mjs",
"target": "es2017",
"rootDir": "src",
"jsx": "react",
"types": ["node", "jest"],
"typeRoots": ["./node_modules/@types", "./src/types"]
},
"exclude": ["node_modules", "dist"],
"include": ["src", "./jest-setup.ts"]
}

1 comment on commit ee8c08e

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

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

Check out the recent updates to your Atlas environment:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.