-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from Souptik2001/refactor/counter-component-t…
…o-ts Migrate `Counter` component to TS
- Loading branch information
Showing
6 changed files
with
195 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Counter | ||
|
||
The `Counter` component let's you use circular progress bar with two props - `count` and `limit`. | ||
|
||
This also exposes another component named `CircularProgressBar`, which can be used when we don't want the text annotation and just the SVG illustration. | ||
|
||
## Usage | ||
|
||
```js | ||
import { Counter, CircularProgressBar } from '@10up/block-components'; | ||
|
||
function MyComponent( props ) { | ||
|
||
return ( | ||
<> | ||
<Counter | ||
count={text.length} | ||
limit={20} | ||
/> | ||
|
||
<CircularProgressBar | ||
percentage={(text.length / 20) * 100} | ||
/> | ||
</> | ||
); | ||
} | ||
``` | ||
|
||
## Props | ||
|
||
#### Counter | ||
|
||
| Name | Type | Default | isRequired | Description | | ||
| ---------------- | ---------- | ---------- | --------------------- | ---------------------------------------------------------------------- | | ||
| `count` | `number` | - | `Yes` | Current count of the counter. | | ||
| `limit` | `number` | - | `Yes` | Max limit of the counter. | | ||
|
||
#### CircularProgressBar | ||
|
||
| Name | Type | Default | isRequired | Description | | ||
| ---------------- | ---------- | ---------- | --------------------- | ---------------------------------------------------------------------- | | ||
| `percentage` | `number` | - | `Yes` | Elapsed percentage of the timer. | |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"apiVersion": 2, | ||
"name": "example/counter", | ||
"title": "Counter Example", | ||
"description": "Example Block to show the Counter in usage", | ||
"icon": "smiley", | ||
"category": "common", | ||
"example": {}, | ||
"supports": { | ||
"html": false | ||
}, | ||
"variations": [], | ||
"editorScript": "file:./index.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,59 @@ | ||
import { __ } from '@wordpress/i18n'; | ||
import { InspectorControls, useBlockProps } from '@wordpress/block-editor'; | ||
import { PanelBody, Placeholder, TextControl } from '@wordpress/components'; | ||
|
||
import { Counter } from '@10up/block-components'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
export const BlockEdit = () => { | ||
const blockProps = useBlockProps(); | ||
|
||
const [ text, setText ] = useState( '' ); | ||
|
||
return ( | ||
<> | ||
<InspectorControls> | ||
<PanelBody title={__('Counter', 'example')}> | ||
<TextControl | ||
label="Text" | ||
help="Enter some text" | ||
value={ text } | ||
onChange={ | ||
( value ) => { | ||
if ( value.length > 20 ) { | ||
return; | ||
} | ||
setText( value ); | ||
} | ||
} | ||
/> | ||
<Counter | ||
count={text.length} | ||
limit={20} | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
<div {...blockProps}> | ||
<Placeholder label={__('Counter', 'example')} instructions={__('Counter component example', 'example')}> | ||
<TextControl | ||
label="Text" | ||
help="Enter some text" | ||
value={ text } | ||
onChange={ | ||
( value ) => { | ||
if ( value.length > 20 ) { | ||
return; | ||
} | ||
setText( value ); | ||
} | ||
} | ||
/> | ||
<Counter | ||
count={text.length} | ||
limit={20} | ||
/> | ||
</Placeholder> | ||
</div> | ||
</> | ||
) | ||
} |
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,8 @@ | ||
import { registerBlockType } from '@wordpress/blocks'; | ||
import metadata from './block.json'; | ||
import { BlockEdit } from './edit'; | ||
|
||
registerBlockType( metadata, { | ||
edit: BlockEdit, | ||
save: () => null | ||
} ); |