From be73ccbf7476a0d63601c659a51f1ded4af0fd25 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Tue, 29 Sep 2020 09:32:06 +0800 Subject: [PATCH] Remove tuning Tuning is dealt with outside of the component. Fretboard only deals with which frets are fretted on each string. --- src/components/Fret/Fret.tsx | 163 +++++++++ .../Fretboard/Fretboard.stories.tsx | 21 +- src/components/Fretboard/Fretboard.tsx | 325 +++--------------- .../FretboardString/FretboardString.tsx | 25 ++ src/index.tsx | 12 +- 5 files changed, 248 insertions(+), 298 deletions(-) create mode 100644 src/components/Fret/Fret.tsx create mode 100644 src/components/FretboardString/FretboardString.tsx diff --git a/src/components/Fret/Fret.tsx b/src/components/Fret/Fret.tsx new file mode 100644 index 0000000..027d3fc --- /dev/null +++ b/src/components/Fret/Fret.tsx @@ -0,0 +1,163 @@ +import * as React from 'react' +import * as PropTypes from 'prop-types' + +const propTypes = { + fretNumber: PropTypes.number.isRequired, + totalFrets: PropTypes.number.isRequired, + firstString: PropTypes.bool, + strings: PropTypes.number.isRequired, + landmarks: PropTypes.bool, + fretted: PropTypes.bool, + capoFret: PropTypes.number, + name: PropTypes.string, +} + +type Props = PropTypes.InferProps + +const Fret: React.FC = ({ + fretNumber, + totalFrets, + children, + firstString, + strings, + landmarks = false, + fretted = false, + capoFret = 0, + name, +}) => ( +
0 ? `${(totalFrets - fretNumber) / 12 * 100 + 100}%` : '0.5rem', + height: '1rem', + flexShrink: fretNumber > 0 ? undefined : 0, + padding: 0, + borderWidth: '0 0.0625rem', + borderStyle: 'none solid', + }} + > + { + landmarks + && (fretNumber % 12 === 3 || fretNumber % 12 === 5 || fretNumber % 12 === 7 || fretNumber % 12 === 9 || fretNumber % 12 === 0) + && fretNumber > 0 + && firstString + && ( +
+
+ {fretNumber > 0 && fretNumber % 12 === 0 && ( +
+ )} +
+ ) + } +
+ {children} +
+ { + fretted + && ( +
+
+
+ {name} +
+
+
+ ) + } + { + fretNumber > 0 + && capoFret === fretNumber + && firstString + && ( +
+
+
+ ) + } +
+) + +Fret.propTypes = propTypes + +export default Fret diff --git a/src/components/Fretboard/Fretboard.stories.tsx b/src/components/Fretboard/Fretboard.stories.tsx index 08f8d66..ccbba92 100644 --- a/src/components/Fretboard/Fretboard.stories.tsx +++ b/src/components/Fretboard/Fretboard.stories.tsx @@ -9,20 +9,21 @@ export default { // you consume the story in a test. export const Default = (props?: Partial) => -export const ReducedFrets = (props?: Partial) => +export const ReducedFrets = (props?: Partial) => -export const FixedTunings = (props?: Partial) => +export const Fretted = (props?: Partial) => -export const Fretted = (props?: Partial) => +export const FrettedWithNames = (props?: Partial) => -export const Tunable = (props?: Partial) => +export const Mirror = (props?: Partial) => -export const LeftHanded = (props?: Partial) => +export const MirrorFrettedWithNames = (props?: Partial) => -export const Bass = (props?: Partial) => +export const Bass = (props?: Partial) => -export const ExtendedGuitar = (props?: Partial) => +export const ExtendedGuitar = (props?: Partial) => -// export const ExtendedBass = (props?: Partial) => - -export const WithCapo = (props?: Partial) => +export const WithCapo = (props?: Partial) => diff --git a/src/components/Fretboard/Fretboard.tsx b/src/components/Fretboard/Fretboard.tsx index 6e1b077..ba5bc8f 100644 --- a/src/components/Fretboard/Fretboard.tsx +++ b/src/components/Fretboard/Fretboard.tsx @@ -1,314 +1,83 @@ -import React, {ChangeEventHandler, FC} from 'react' +import React, {FC} from 'react' import * as PropTypes from 'prop-types' +import FretboardString from '../FretboardString/FretboardString' +import Fret from '../Fret/Fret' const propTypes = { - tunings: PropTypes.arrayOf(PropTypes.number), + strings: PropTypes.number, frets: PropTypes.number, - leftHanded: PropTypes.bool, + mirror: PropTypes.bool, fixedTunings: PropTypes.bool, displayTunings: PropTypes.bool, capoFret: PropTypes.number, fretted: PropTypes.arrayOf(PropTypes.number), + landmarks: PropTypes.bool, + frettedNames: PropTypes.arrayOf(PropTypes.string), } export type Props = PropTypes.InferProps -const PITCHES = 'C C# D D# E F F# G G# A A# B'.split(' ') - -const getPitchName = (n: number) => PITCHES[n % 12] + Math.floor(n / 12) - const Fretboard: FC = ({ - tunings = [64, 59, 55, 50, 45, 40], + strings = 6, frets = 24, - leftHanded = false, - fixedTunings = false, - displayTunings = false, + mirror = false, capoFret = 0, fretted = [], + landmarks = false, + frettedNames = [], }) => { - const [clientSide, setClientSide, ] = React.useState(false) - const [pitches, setPitches, ] = React.useState(tunings!.map(t => getPitchName(t!))) - - const updateStringPitch = (currentStringNumber: number): ChangeEventHandler => e => { - const { value } = e.target - setPitches(oldPitches => [ - ...oldPitches.slice(0, currentStringNumber), - getPitchName(Number(value)), - ...oldPitches.slice(currentStringNumber + 1), - ]) - } - - React.useEffect(() => { - setClientSide(true) - }, []) - - // React.useEffect(() => { - // if (!Array.isArray(tunings!)) { - // return - // } - // - // setPitches(() => tunings.map(t => getPitchName(t!))) - // }, [tunings]) - - if (!Array.isArray(tunings!)) { - return null - } - - const strings = tunings!.map(t => new Array(frets).fill(0).map((_, i) => t! + i + 1)) + const str: boolean[][] = new Array(strings) + .fill(null) + .map(() => new Array(frets) + .fill(null) + .map(() => false) + ) return (
- {strings!.map((stringPitches, stringNumber) => ( + {str!.map((f, stringNumber: number) => (
- { - displayTunings - && ( -
-
- - {pitches[stringNumber]} - - -
-
- ) - } -
- -
- {stringPitches.map((_, i) => ( -
- {(i % 12 === 2 || i % 12 === 4 || i % 12 === 6 || i % 12 === 8 || i % 12 === 11) && stringNumber === 0 && ( -
-
- {i % 12 === 11 && ( -
- )} -
- )} - - { - capoFret === (i + 1) && stringNumber === 0 && ( -
-
-
- ) - } -
+ ))}
))} diff --git a/src/components/FretboardString/FretboardString.tsx b/src/components/FretboardString/FretboardString.tsx new file mode 100644 index 0000000..2d3c91e --- /dev/null +++ b/src/components/FretboardString/FretboardString.tsx @@ -0,0 +1,25 @@ +import * as React from 'react' +import * as PropTypes from 'prop-types' + +const propTypes = { + stringNumber: PropTypes.number.isRequired, +} + +type Props = PropTypes.InferProps + +const FretboardString: React.FC = ({ + stringNumber, +}) => ( + +) + +FretboardString.propTypes = propTypes + +export default FretboardString diff --git a/src/index.tsx b/src/index.tsx index 38cd88a..26ef136 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,11 +1,3 @@ -import React, { FC, HTMLAttributes, ReactChild } from 'react' +import Fretboard from './components/Fretboard/Fretboard' -export interface Props extends HTMLAttributes { - children?: ReactChild -} - -// Please do not use types off of a default export module or else Storybook Docs will suffer. -// see: https://github.com/storybookjs/storybook/issues/9556 -export const Thing: FC = ({ children }) => { - return
{children || `the snozzberries taste like snozzberries`}
-} +export default Fretboard